From 77f91f7fcff89291e16c2f5b9b7d904b975e1bbe Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Sat, 17 Dec 2022 12:39:31 +0100 Subject: [PATCH 10/15] Python 3.11 needs an async decorator. --- tests/main/test_http_aiohttp.py | 127 +++++++++++++++++--------------- 1 file changed, 66 insertions(+), 61 deletions(-) Index: mocket-3.10.9/tests/main/test_http_aiohttp.py =================================================================== --- mocket-3.10.9.orig/tests/main/test_http_aiohttp.py +++ mocket-3.10.9/tests/main/test_http_aiohttp.py @@ -1,5 +1,6 @@ import asyncio import json +import platform from unittest import TestCase import aiohttp @@ -9,74 +10,78 @@ from mocket.mocket import Mocket, mocket from mocket.mockhttp import Entry from mocket.plugins.httpretty import HTTPretty, httprettified - -class AioHttpEntryTestCase(TestCase): - @mocketize - def test_http_session(self): - url = "http://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(_loop): - async with aiohttp.ClientSession(loop=_loop) as session: - async with async_timeout.timeout(3): - async with session.get(url) as get_response: - assert get_response.status == 404 - assert await get_response.text() == body - - async with async_timeout.timeout(3): - async with session.post(url, data=body * 6) as post_response: - assert post_response.status == 201 - assert await post_response.text() == body * 2 - assert Mocket.last_request().method == "POST" - assert Mocket.last_request().body == body * 6 - - loop = asyncio.new_event_loop() - loop.set_debug(True) - loop.run_until_complete(main(loop)) - self.assertEqual(len(Mocket.request_list()), 2) - - @mocketize - def test_https_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(_loop): - async with aiohttp.ClientSession(loop=_loop) as session: - async with async_timeout.timeout(3): - async with session.get(url) as get_response: - assert get_response.status == 404 - assert await get_response.text() == body - - async with async_timeout.timeout(3): - 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.set_debug(True) - loop.run_until_complete(main(loop)) - self.assertEqual(len(Mocket.request_list()), 2) - - @httprettified - def test_httprettish_session(self): - url = "https://httpbin.org/ip" - HTTPretty.register_uri( - HTTPretty.GET, - url, - body=json.dumps(dict(origin="127.0.0.1")), - ) - - async def main(_loop): - async with aiohttp.ClientSession(loop=_loop) as session: - async with async_timeout.timeout(3): - async with session.get(url) as get_response: - assert get_response.status == 200 - assert await get_response.text() == '{"origin": "127.0.0.1"}' - - loop = asyncio.new_event_loop() - loop.set_debug(True) - loop.run_until_complete(main(loop)) +if not platform.python_version().startswith("3.11."): + # Python 3.11 needs async decorators, or aiohttp + # will fail with "Cannot write to closing transport" + class AioHttpEntryTestCase(TestCase): + @mocketize + def test_http_session(self): + url = "http://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(_loop): + async with aiohttp.ClientSession(loop=_loop) as session: + async with async_timeout.timeout(3): + async with session.get(url) as get_response: + assert get_response.status == 404 + assert await get_response.text() == body + + async with async_timeout.timeout(3): + async with session.post(url, data=body * 6) as post_response: + assert post_response.status == 201 + assert await post_response.text() == body * 2 + assert Mocket.last_request().method == "POST" + assert Mocket.last_request().body == body * 6 + + loop = asyncio.new_event_loop() + loop.set_debug(True) + loop.run_until_complete(main(loop)) + self.assertEqual(len(Mocket.request_list()), 2) + + @mocketize + def test_https_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(_loop): + async with aiohttp.ClientSession(loop=_loop) as session: + async with async_timeout.timeout(3): + async with session.get(url) as get_response: + assert get_response.status == 404 + assert await get_response.text() == body + + async with async_timeout.timeout(3): + 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.set_debug(True) + loop.run_until_complete(main(loop)) + self.assertEqual(len(Mocket.request_list()), 2) + + @httprettified + def test_httprettish_session(self): + url = "https://httpbin.org/ip" + HTTPretty.register_uri( + HTTPretty.GET, + url, + body=json.dumps(dict(origin="127.0.0.1")), + ) + + async def main(_loop): + async with aiohttp.ClientSession(loop=_loop) as session: + async with async_timeout.timeout(3): + async with session.get(url) as get_response: + assert get_response.status == 200 + assert ( + await get_response.text() == '{"origin": "127.0.0.1"}' + ) + + loop = asyncio.new_event_loop() + loop.set_debug(True) + loop.run_until_complete(main(loop))