Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
oschwald opened this issue Nov 20, 2023 · 6 comments
Closed

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

oschwald opened this issue Nov 20, 2023 · 6 comments

Comments

@oschwald
Copy link

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
Copy link
Owner

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 #181 and aio-libs/aiohttp#4587

@oschwald
Copy link
Author

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.

oschwald added a commit to maxmind/minfraud-api-python that referenced this issue Nov 20, 2023
@mindflayer
Copy link
Owner

mindflayer commented Nov 21, 2023

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
Copy link
Owner

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

mindflayer added a commit that referenced this issue Jan 16, 2024
Adding testcase for proving #209 was fixed
@oschwald
Copy link
Author

Awesome. Thanks!

@mindflayer
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants