From 6719547e1d3d5bb672258bc30fed835ddc659ce8 Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Sun, 18 Dec 2022 00:44:02 +0100 Subject: [PATCH 12/15] Removing `async-timeout` dependency. --- Pipfile | 1 - README.rst | 54 ++++++------ tests/main/test_http_aiohttp.py | 131 ++++++++++++++--------------- tests/tests38/test_http_aiohttp.py | 48 +++++------ 4 files changed, 113 insertions(+), 121 deletions(-) Index: mocket-3.10.9/README.rst =================================================================== --- mocket-3.10.9.orig/README.rst +++ mocket-3.10.9/README.rst @@ -231,7 +231,6 @@ Example: import aiohttp import asyncio - import async_timeout from unittest import TestCase from mocket.plugins.httpretty import httpretty, httprettified @@ -248,11 +247,12 @@ Example: ) async def main(l): - async with aiohttp.ClientSession(loop=l) 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"}' + 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 == 200 + assert await get_response.text() == '{"origin": "127.0.0.1"}' loop = asyncio.new_event_loop() loop.set_debug(True) @@ -277,16 +277,16 @@ Example: Entry.single_register(Entry.POST, url, body=body*2, status=201) async def main(l): - async with aiohttp.ClientSession(loop=l) 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 + 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)) @@ -302,18 +302,18 @@ Example: Entry.single_register(Entry.GET, url, body=body, status=404) Entry.single_register(Entry.POST, url, body=body * 2, status=201) - async with aiohttp.ClientSession() 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 + async with aiohttp.ClientSession( + 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 + assert Mocket.last_request().method == 'POST' + assert Mocket.last_request().body == body * 6 Works well with others 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,87 +1,84 @@ import asyncio import json -import platform from unittest import TestCase import aiohttp -import async_timeout from mocket.mocket import Mocket, mocketize from mocket.mockhttp import Entry from mocket.plugins.httpretty import HTTPretty, httprettified -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)) + +class AioHttpEntryTestCase(TestCase): + timeout = aiohttp.ClientTimeout(total=3) + + @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, timeout=self.timeout + ) 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 + 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, timeout=self.timeout + ) 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.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, timeout=self.timeout + ) as session: + 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)) Index: mocket-3.10.9/tests/tests38/test_http_aiohttp.py =================================================================== --- mocket-3.10.9.orig/tests/tests38/test_http_aiohttp.py +++ mocket-3.10.9/tests/tests38/test_http_aiohttp.py @@ -2,7 +2,6 @@ import json from unittest import IsolatedAsyncioTestCase import aiohttp -import async_timeout from mocket.async_mocket import async_mocketize from mocket.mocket import Mocket @@ -11,6 +10,8 @@ from mocket.plugins.httpretty import HTT class AioHttpEntryTestCase(IsolatedAsyncioTestCase): + timeout = aiohttp.ClientTimeout(total=3) + @async_mocketize async def test_http_session(self): url = "http://httpbin.org/ip" @@ -18,18 +19,16 @@ class AioHttpEntryTestCase(IsolatedAsync Entry.single_register(Entry.GET, url, body=body, status=404) Entry.single_register(Entry.POST, url, body=body * 2, status=201) - async with aiohttp.ClientSession() 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 + async with aiohttp.ClientSession(timeout=self.timeout) 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 + assert Mocket.last_request().method == "POST" + assert Mocket.last_request().body == body * 6 self.assertEqual(len(Mocket.request_list()), 2) @@ -40,16 +39,14 @@ class AioHttpEntryTestCase(IsolatedAsync Entry.single_register(Entry.GET, url, body=body, status=404) Entry.single_register(Entry.POST, url, body=body * 2, status=201) - async with aiohttp.ClientSession() 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 + async with aiohttp.ClientSession(timeout=self.timeout) 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 self.assertEqual(len(Mocket.request_list()), 2) @@ -62,8 +59,7 @@ class AioHttpEntryTestCase(IsolatedAsync body=json.dumps(dict(origin="127.0.0.1")), ) - async with aiohttp.ClientSession() 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"}' + async with aiohttp.ClientSession(timeout=self.timeout) as session: + async with session.get(url) as get_response: + assert get_response.status == 200 + assert await get_response.text() == '{"origin": "127.0.0.1"}'