Skip to main content

Mock out requests made by ClientSession from aiohttp package

Project description

https://travis-ci.org/pnuckowski/aioresponses.svg?branch=master https://coveralls.io/repos/github/pnuckowski/aioresponses/badge.svg?branch=master Code Health Updates https://img.shields.io/pypi/v/aioresponses.svg Documentation Status

Aioresponses is a helper for mock/fake web requests in python aiohttp package.

For requests module there is a lot of packages that helps us with testing (eg. httpretty, responses, requests-mock).

When it comes to testing asynchronous http requests it is a bit harder (at least at the beginning). The purpose of this package is to provide an easy way to test asynchronous http requests.

Installing

$ pip install aioresponses

Usage

To mock out http request use aioresponses as method decorator or as context manager.

Response status code, body, payload (for json response) and headers can be mocked.

Supported http methods: get, post, put, patch, delete and options.

import aiohttp
import asyncio
from aioresponses import aioresponses

@aioresponses()
def test_request(mocked):
    loop = asyncio.get_event_loop()
    mocked.get('http://example.com', status=200, body='test')
    session = aiohttp.ClientSession()
    resp = loop.run_until_complete(session.get('http://example.com'))

    assert resp.status == 200

for convenience use payload argument to mock out json response. Example below.

as context manager

import asyncio
import aiohttp
from aioresponses import aioresponses

def test_ctx():
    loop = asyncio.get_event_loop()
    session = aiohttp.ClientSession()
    with aioresponses() as m:
        m.get('http://test.example.com', payload=dict(foo='bar'))

        resp = loop.run_until_complete(session.get('http://test.example.com'))
        data = loop.run_until_complete(resp.json())

        assert dict(foo='bar') == data

aioresponses allow to mock out any HTTP headers

import asyncio
import aiohttp
from aioresponses import aioresponses

@aioresponses()
def test_http_headers(m):
    loop = asyncio.get_event_loop()
    session = aiohttp.ClientSession()
    m.post(
        'http://example.com',
        payload=dict(),
        headers=dict(connection='keep-alive'),
    )

    resp = loop.run_until_complete(session.post('http://example.com'))

    # note that we pass 'connection' but get 'Connection' (capitalized)
    # under the neath `multidict` is used to work with HTTP headers
    assert resp.headers['Connection'] == 'keep-alive'

allow to register different response for the same url

import asyncio
import aiohttp
from aioresponses import aioresponses

@aioresponses()
def test_multiple_responses(m):
    loop = asyncio.get_event_loop()
    session = aiohttp.ClientSession()
    m.get('http://example.com', status=500)
    m.get('http://example.com', status=200)

    resp1 = loop.run_until_complete(session.get('http://example.com'))
    resp2 = loop.run_until_complete(session.get('http://example.com'))

    assert resp1.status == 500
    assert resp2.status == 200

allow to passthrough to a specified list of servers

import asyncio
import aiohttp
from aioresponses import aioresponses

@aioresponses(passthrough=['http://backend'])
def test_passthrough(m, test_client):
    session = aiohttp.ClientSession()
    # this will actually perform a request
    resp = loop.run_until_complete(session.get('http://backend/api'))

aioresponses allow to throw an exception

import asyncio
from aiohttp import ClientSession, HttpProcessingError
from aioresponses import aioresponses

@aioresponses()
def test_how_to_throw_an_exception(m, test_client):
    loop = asyncio.get_event_loop()
    session = ClientSession()
    m.get('http://example.com/api', exception=HttpProcessingError('test'))

    # calling
    # loop.run_until_complete(session.get('http://example.com/api'))
    # will throw an exception.

Features

  • Easy to mock out http requests made by aiohttp.ClientSession

Disclaimer

Due to the fact that get, post, put, delete methods from aiohttp are in deprecation mode and they are NOT supported by this package.

License

  • Free software: MIT license

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aioresponses-0.1.4.tar.gz (28.2 kB view details)

Uploaded Source

Built Distribution

aioresponses-0.1.4-py2.py3-none-any.whl (8.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file aioresponses-0.1.4.tar.gz.

File metadata

File hashes

Hashes for aioresponses-0.1.4.tar.gz
Algorithm Hash digest
SHA256 44833b6bddf6e498ad3e0b2548a2d77b2eddcf63009a2eeb7bcf78277fd3d0b8
MD5 5616de05035cbd0f8c4e418f0fdc2e69
BLAKE2b-256 272987c7222967d03b72a5709c438417b2ef6330400b94ac645c6d92e852b076

See more details on using hashes here.

File details

Details for the file aioresponses-0.1.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for aioresponses-0.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bc8ad6b30afb7eae38f266b5af78104c1dcf47a638443cb253403c8f54eb5e27
MD5 881a333d308409f19ce27eb9605dbb73
BLAKE2b-256 d825d053db202d81224749f9d8154c4e1c168f733391234ca90cd42d1c7c5e56

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page