aiohttp testing library
Project description
Async Responses
Async Responses is a library providing an easy way to mock aiohttp responses inspired by aioresponses.
Installation
Library is available on PyPi, you can simply install it using pip
.
$ pip install async-responses
Usage
As an instance
ar = AsyncResponses()
ar.get(...)
As a context manager
with AsyncResponses() as ar:
ar.get(...)
With dict as handler
Passing dict as handler
argument to async-responses would result in it being
returned as a JSON payload.
import aiohttp
from async_responses import AsyncResponses
async def test_response():
ar = AsyncResponses()
payload = {'status': 'ok'}
ar.get('http://mock.url', '/v1/status', handler=payload)
async with aiohttp.ClientSession() as session:
response = await session.get('http://mock.url/v1/status')
assert await response.json() == payload
With exception as handler
Calling Async Responses with an exception as handler
argument would result in
it being raised.
import aiohttp
from async_responses import AsyncResponses
import pytest
async def test_response():
ar = AsyncResponses()
ar.get('http://mock.url', '/v1/status', handler=ZeroDivisionError)
with pytest.raises(ZeroDivisionError):
async with aiohttp.ClientSession() as session:
await session.get('http://mock.url/v1/status')
With string as handler
import aiohttp
from async_responses import AsyncResponses
async def test_response():
ar = AsyncResponses()
payload = 'ok'
ar.get('http://mock.url', '/v1/status', handler=payload)
async with aiohttp.ClientSession() as session:
response = await session.get('http://mock.url/v1/status')
assert await response.text() == payload
With callable as handler
import aiohttp
from async_responses import AsyncResponses
async def test_response():
def handler(data, **kwargs):
return {'status': 'ok'}
ar = AsyncResponses()
ar.get('http://mock.url', '/v1/status', handler=payload)
async with aiohttp.ClientSession() as session:
response = await session.get('http://mock.url/v1/status')
assert await response.json() == {'status': 'ok'}
With a custom status code
import aiohttp
from async_responses import AsyncResponses
async def test_response():
payload = {'status': 'not good'}
ar = AsyncResponses()
ar.get('http://mock.url', '/v1/status', handler=payload, status=500)
async with aiohttp.ClientSession() as session:
response = await session.get('http://mock.url/v1/status')
assert response.status == 500
assert await response.json() == payload
With a custom response class
async-responses will make use of a response class passed as an argument to ClientSession, so you can use things like custom JSON serializer.
import aiohttp
async def test_response():
class CustomResponse(aiohttp.ClientResponse):
async def json(self, *args, **kwargs):
return {'hello': 'world'}
ar = AsyncResponses()
ar.get('http://mock.url', '/v1/status', handler='')
async with aiohttp.ClientSession(response_class=CustomResponse) as session:
response = await session.get('http://mock.url/v1/status')
assert await response.json() == {'hello': 'world'}
assert isinstance(response, CustomResponse)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file async-responses-1.0.0_2.tar.gz
.
File metadata
- Download URL: async-responses-1.0.0_2.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.44.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7669ea9765aba429406f61ec196b7cf016448e1285dc3adb8431d7182ce92055 |
|
MD5 | 95b50f5e5824e17cd709a75e3ab15171 |
|
BLAKE2b-256 | f34d42303987b6c9a9e861d1d9b219289cc61e387613dd84af2f0228ab0a29a4 |
File details
Details for the file async_responses-1.0.0_2-py3-none-any.whl
.
File metadata
- Download URL: async_responses-1.0.0_2-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags:
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.44.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6c9fe6f38b634c63eb79b6148a31e9a852f0ab77790d7549205aa89fa7f90a7 |
|
MD5 | b329a6e4ec944f4640dba276f83558af |
|
BLAKE2b-256 | 2446ab394d59d743220a681b77d231b3daee055af4200bc795ff124531bf017f |