mindflayer / python-mocket

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

Can't make a mixture of unmocked and mocked HTTPS requests using aiohttp #215

Closed ento closed 6 months ago

ento commented 6 months ago

In a project I'm working with, a mocketized test would fail if gets run after a test that makes a real HTTPS request. Both use aiohttp.

Minimal reproducible example (this fails without prerequisite fixes found in #213)

from unittest import IsolatedAsyncioTestCase

from mocket.mocket import Mocketizer

try:
    import aiohttp

    ENABLE_TEST_CLASS = True
except ImportError:
    ENABLE_TEST_CLASS = False

if ENABLE_TEST_CLASS:
    class AioHttpsEntryTestCase(IsolatedAsyncioTestCase):
        timeout = aiohttp.ClientTimeout(total=3)
        target_url = "https://httpbin.localhost/anything/"

        @pytest.mark.skipif('os.getenv("SKIP_TRUE_HTTP", False)')
        async def test_mocked_https_request_after_unmocked_https_request(self):
            async with aiohttp.ClientSession(timeout=self.timeout) as session:
                response = await session.get(self.target_url + "real", ssl=False)
                assert response.status == 200

            async with Mocketizer(None):
                Entry.single_register(Entry.GET, self.target_url + "mocked", status=404)
                async with aiohttp.ClientSession(timeout=self.timeout) as session:
                    response = await session.get(self.target_url + "mocked", ssl=False)
                    assert response.status == 404
                    self.assertEqual(len(Mocket.request_list()), 1)

This fails like so:

    async def read(self) -> _T:
        if not self._buffer and not self._eof:
            assert not self._waiter
            self._waiter = self._loop.create_future()
            try:
>               await self._waiter
E               aiohttp.client_exceptions.ClientOSError: [Errno 1] [SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] decryption failed or bad record mac (_ssl.c:2580)

.devenv/state/venv/lib/python3.11/site-packages/aiohttp/streams.py:622: ClientOSError

Versions used: aiohttp: 3.9.1 mocket: cdd2eb8bf1e868df358d1ae3580f3dbdcf466b75

mindflayer commented 6 months ago

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