Skip to main content
https://img.shields.io/travis/pnuckowski/aioresponses.svg 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_respnses(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

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.

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.2.tar.gz (26.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aioresponses-0.1.2-py2.py3-none-any.whl (7.1 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: aioresponses-0.1.2.tar.gz
  • Upload date:
  • Size: 26.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for aioresponses-0.1.2.tar.gz
Algorithm Hash digest
SHA256 6feaba4370f4389336886cdf5d3723e6b69278d7d9b3af806c7bec4a67fcd89f
MD5 8dbfe3f011f2e2c3cf8858935ab7b8ef
BLAKE2b-256 d212c6fb3bb2851b05cbd20a9f4284ef68debe83655edb0e63f157f3bda969a6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aioresponses-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f7cefa9d310bb09ef67d4b03c64b641c3c24ba88834951e0a0df91ed6970e947
MD5 88135dd4f8116457b3b5c50c55298ec3
BLAKE2b-256 e79735e8063420e688129df5f6fe1bd4f0061ef949315a48843a787212082097

See more details on using hashes here.

Release history Release notifications | RSS feed

0.7.9

2 files

0.7.8

2 files

0.7.7

2 files

0.7.6

2 files

0.7.5

2 files

0.7.4

2 files

0.7.3

2 files

0.7.2

2 files

0.7.1

2 files

0.7.0

2 files

0.6.4

2 files

0.6.3

2 files

0.6.2

2 files

0.6.1

2 files

0.6.0

2 files

0.5.2

2 files

0.5.1

2 files

0.5.0

2 files

0.4.2

2 files

0.4.1

2 files

0.4.0

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.0

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

This release

0.1.2 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page