mindflayer / python-mocket

a socket mock framework - for all kinds of socket animals, web-clients included
BSD 3-Clause "New" or "Revised" License
281 stars 42 forks source link

Can't make an HTTPS request with 'no verify' mode using aiohttp when Mocket is active #216

Closed ento closed 8 months ago

ento commented 8 months ago

Calling aiohttp's API with ssl=False fails:

from mocket.async_mocket import async_mocketize
from mocket.mockhttp import Entry

try:
    import aiohttp

    ENABLE_TEST_CLASS = True
except ImportError:
    ENABLE_TEST_CLASS = False

if ENABLE_TEST_CLASS:
    class AioHttpsEntryTestCase(IsolatedAsyncioTestCase):
        @async_mocketize
        async def test_no_verify(self):
            Entry.single_register(Entry.GET, self.target_url, status=404)

            async with aiohttp.ClientSession(timeout=self.timeout) as session:
                async with session.get(self.target_url, ssl=False) as get_response:
                    assert get_response.status == 404

This fails like so:

verified = False

    @staticmethod
    @functools.lru_cache(None)
    def _make_ssl_context(verified: bool) -> SSLContext:
        if verified:
            return ssl.create_default_context()
        else:
            sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
            sslcontext.options |= ssl.OP_NO_SSLv2
            sslcontext.options |= ssl.OP_NO_SSLv3
            sslcontext.check_hostname = False
            sslcontext.verify_mode = ssl.CERT_NONE
            try:
                sslcontext.options |= ssl.OP_NO_COMPRESSION
            except AttributeError as attr_err:
                warnings.warn(
                    "{!s}: The Python interpreter is compiled "
                    "against OpenSSL < 1.0.0. Ref: "
                    "https://docs.python.org/3/library/ssl.html"
                    "#ssl.OP_NO_COMPRESSION".format(attr_err),
                )
>           sslcontext.set_default_verify_paths()
E           TypeError: 'NoneType' object is not callable

.devenv/state/venv/lib/python3.11/site-packages/aiohttp/connector.py:936: TypeError

Versions used: aiohttp: 3.9.1 mocket: cdd2eb8bf1e868df358d1ae3580f3dbdcf466b75

mindflayer commented 8 months ago

Here is the new version which fixes this issue: https://pypi.org/project/mocket/3.12.3/