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

Async example fails on Python 3.11 when URL is changed to HTTPS #209

Closed oschwald closed 6 months ago

oschwald commented 8 months ago

While debugging failures on our test suite with Python 3.11, I noticed that the async example in the docs fail in the same way with Python 3.11 after changing the URL to HTTPS:

import aiohttp
import asyncio

from unittest import TestCase

from mocket import mocketize
from mocket.mockhttp import Entry

class AioHttpEntryTestCase(TestCase):
    @mocketize
    def test_http_session(self):
        url = 'https://httpbin.org/ip'
        body = "asd" * 100
        Entry.single_register(Entry.GET, url, body=body, status=404)
        Entry.single_register(Entry.POST, url, body=body*2, status=201)

        async def main(l):
            async with aiohttp.ClientSession(
                loop=l, timeout=aiohttp.ClientTimeout(total=3)
            ) as session:
                async with session.get(url) as get_response:
                    assert get_response.status == 404
                    assert await get_response.text() == body

                async with session.post(url, data=body * 6) as post_response:
                    assert post_response.status == 201
                    assert await post_response.text() == body * 2

        loop = asyncio.new_event_loop()
        loop.run_until_complete(main(loop))

This fails with:

aiohttp.client_exceptions.ClientOSError: Cannot write to closing transport

It works fine if the URL is just http://httpbin.org/ip.

mindflayer commented 8 months ago

Hi @oschwald, I spent ages trying to debug that problem and eventually gave up moving to httpx as the main client for testing Mocket. See https://github.com/mindflayer/python-mocket/pull/181 and https://github.com/aio-libs/aiohttp/issues/4587

oschwald commented 8 months ago

Ah, that is too bad. I don't know if it is realistic for us to switch off of aiohttp, but I suppose we could just avoid using https in the tests. I initially thought that given the error message, maybe we were not awaiting something.

I did see the 2020 aiohttp issue while looking into this, but I figured that it was unrelated as it was marked as fixed. I missed the discussion in #181 though. Maybe a warning or something in the docs would help.

mindflayer commented 8 months ago

Definitely not worth it, but for Mocket I really needed a stable client as the main one for testing. That issue was driving me crazy, and it really seemed something only affecting aiohttp, up to the point when I started considering alternatives, especially after I realised it was plenty of projects hit by the same problem.

mindflayer commented 6 months ago

Thanks to @ento this issue is finally solved. I'll release a new version ASAP.

oschwald commented 6 months ago

Awesome. Thanks!

mindflayer commented 6 months ago

Here is the new version with the fix: https://pypi.org/project/mocket/3.12.3/