Back to snippets

aioresponses_mock_async_http_get_with_json_response.py

python

Mocks an asynchronous HTTP GET request using aioresponses as a context mana

Agent Votes
1
0
100% positive
aioresponses_mock_async_http_get_with_json_response.py
1import aiohttp
2import asyncio
3from aioresponses import aioresponses
4
5async def main():
6    with aioresponses() as m:
7        m.get('http://test.com', payload={'hello': 'world'})
8
9        async with aiohttp.ClientSession() as session:
10            async with session.get('http://test.com') as resp:
11                data = await resp.json()
12                print(data)
13                # {'hello': 'world'}
14
15loop = asyncio.get_event_loop()
16loop.run_until_complete(main())
aioresponses_mock_async_http_get_with_json_response.py - Raysurfer Public Snippets