Skip to main content
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_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.3.tar.gz (26.9 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.3-py2.py3-none-any.whl (7.1 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for aioresponses-0.1.3.tar.gz
Algorithm Hash digest
SHA256 79e43849f135db109271f50c81cb29bfbfb44422dd59d0e73896e8b8fe83c70f
MD5 9c6587741544bbe3b5ccb652427f1ae8
BLAKE2b-256 b4743cb54b0fc60d10a43c7d62c5c0505d1d717c46a9cea5d2566bd46165524d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aioresponses-0.1.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7fc0bce24ba9421dc06d7947cd77aee1b308b6c94274811e97990af82fff8577
MD5 5263be05cd0d5e331cd16d7f30cc945a
BLAKE2b-256 8f5e5ecce703e726d086754ead408f227486ff4d59cf943f014328c801e557a2

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

This release

0.1.3 This release

2 files

0.1.2

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