and Python 3, consequently… Try it yourself! class AioHttpEntryTestCase(TestCase): def test_https_session(self):url = ' HTTPretty.register_uri(HTTPretty.GET,url,body=json.dumps(dict(origin='127.0.0.1')),) @httprettified https://httpbin.org/ip' async def main(l): async with aiohttp.ClientSession(loop=l) as session: 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.get\_event\_loop() loop.set\_debug(True) loop.run\_until\_complete(main(loop)) Well, I forgot the import block. import json import aiohttpimport asyncioimport async_timeoutfrom unittest import TestCase from mocket.plugins.httpretty import HTTPretty, httprettified Wait! ?! What the Hell is that? We were supposed to talk about ! Mocket HTTPretty Yeah, you’re right, and more or less we are talking about it, or better, we are talking about a project born in 2013 and initially inspired by HTTPretty. Then, in 4 years and after a long period where we almost forgot it, Mocket kept growing even if for long I have been the only developer to maintain it. much water has flowed under the bridge, So, back to the example, this would be the Mocket way to do the same: import json import aiohttpimport asyncioimport async_timeoutfrom unittest import TestCase from mocket.mocket import mocketizefrom mocket.mockhttp import Entry class AioHttpEntryTestCase(TestCase): def test_https_session(self):url = ' Entry.single_register(Entry.GET,url,body=json.dumps(dict(origin='127.0.0.1')),) @mocketize https://httpbin.org/ip' async def main(l): async with aiohttp.ClientSession(loop=l) as session: 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.get\_event\_loop() loop.set\_debug(True) loop.run\_until\_complete(main(loop)) So, if you like it, I you to do one or more of the following things: encourage Use Mocket ; Contribute to Mocket ; ★ Mocket at ; https://github.com/mindflayer/python-mocket/ 👍 my PR at ; https://github.com/vinta/awesome-python/pull/956 Mocket: a socket mock framework for all kinds of socket animals, web-clients included
Share Your Thoughts