All Projects → pnuckowski → Aioresponses

pnuckowski / Aioresponses

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

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Aioresponses

Python Mocket
a socket mock framework - for all kinds of socket animals, web-clients included
Stars: ✭ 209 (-24.82%)
Mutual labels:  asyncio, aiohttp, mocking
nala
🦁 Nala - A delightful test framework for C projects.
Stars: ✭ 58 (-79.14%)
Mutual labels:  test, mocking
dvhb-hybrid
A package to mix django and asyncio in one application
Stars: ✭ 45 (-83.81%)
Mutual labels:  aiohttp, asyncio
Web Main
🎉 Ultimate Emoji Generator
Stars: ✭ 261 (-6.12%)
Mutual labels:  asyncio, aiohttp
glQiwiApi
The ultrarapid and multifunctional wrapper over QIWI and YooMoney
Stars: ✭ 44 (-84.17%)
Mutual labels:  aiohttp, asyncio
website
PySlackers website for invites and learning resources
Stars: ✭ 61 (-78.06%)
Mutual labels:  aiohttp, asyncio
waio
Is a pretty simple and fully asynchronous framework for WhatsApp Business API written in Python 3.7 with asyncio and aiohttp.
Stars: ✭ 18 (-93.53%)
Mutual labels:  aiohttp, asyncio
jaymock-cli
Mock an API and generate fake JSON test data, right from the terminal.
Stars: ✭ 13 (-95.32%)
Mutual labels:  test, mocking
rigor
HTTP-based DSL for for validating RESTful APIs
Stars: ✭ 65 (-76.62%)
Mutual labels:  aiohttp, asyncio
aiohttp-socks
Proxy (HTTP, SOCKS) connector for aiohttp
Stars: ✭ 147 (-47.12%)
Mutual labels:  aiohttp, asyncio
aiohttp traversal
Traversal based router for aiohttp.web
Stars: ✭ 21 (-92.45%)
Mutual labels:  aiohttp, asyncio
feedsearch-crawler
Crawl sites for RSS, Atom, and JSON feeds.
Stars: ✭ 23 (-91.73%)
Mutual labels:  aiohttp, asyncio
fbemissary
A bot framework for the Facebook Messenger platform, built on asyncio and aiohttp
Stars: ✭ 30 (-89.21%)
Mutual labels:  aiohttp, asyncio
twilio mock
Mock Twilio gem for Ruby
Stars: ✭ 26 (-90.65%)
Mutual labels:  test, mocking
pyladies-courseware
Homework/task submit and review web app · based on React and Python aiohttp
Stars: ✭ 14 (-94.96%)
Mutual labels:  aiohttp, asyncio
aiohttp-jwt
aiohttp middleware and helper utils for working with JSON web token.
Stars: ✭ 70 (-74.82%)
Mutual labels:  aiohttp, asyncio
Hippolyte
HTTP Stubbing in Swift
Stars: ✭ 109 (-60.79%)
Mutual labels:  test, mocking
stream video server
demonstrates how to create video streaming server with the help of aiohttp and opencv
Stars: ✭ 15 (-94.6%)
Mutual labels:  aiohttp, asyncio
aiohttp-mako
mako template renderer for aiohttp.web
Stars: ✭ 32 (-88.49%)
Mutual labels:  aiohttp, asyncio
aiodogstatsd
An asyncio-based client for sending metrics to StatsD with support of DogStatsD extension
Stars: ✭ 26 (-90.65%)
Mutual labels:  aiohttp, asyncio

=============================== aioresponses

.. image:: https://travis-ci.org/pnuckowski/aioresponses.svg?branch=master :target: https://travis-ci.org/pnuckowski/aioresponses

.. image:: https://coveralls.io/repos/github/pnuckowski/aioresponses/badge.svg?branch=master :target: https://coveralls.io/github/pnuckowski/aioresponses?branch=master

.. image:: https://landscape.io/github/pnuckowski/aioresponses/master/landscape.svg?style=flat :target: https://landscape.io/github/pnuckowski/aioresponses/master :alt: Code Health

.. image:: https://pyup.io/repos/github/pnuckowski/aioresponses/shield.svg :target: https://pyup.io/repos/github/pnuckowski/aioresponses/ :alt: Updates

.. image:: https://img.shields.io/pypi/v/aioresponses.svg :target: https://pypi.python.org/pypi/aioresponses

.. image:: https://readthedocs.org/projects/aioresponses/badge/?version=latest :target: https://aioresponses.readthedocs.io/en/latest/?badge=latest :alt: Documentation Status

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

For requests module there are a lot of packages that help 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

.. code:: bash

$ pip install aioresponses

Supported versions

  • Python 3.5.3+
  • aiohttp>=2.0.0,<4.0.0

Usage

To mock out HTTP request use aioresponses as a method decorator or as a 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.

.. code:: python

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 a context manager

.. code:: python

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 allows to mock out any HTTP headers

.. code:: python

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'

allows to register different responses for the same url

.. code:: python

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

match URLs with regular expressions

.. code:: python

import asyncio
import aiohttp
import re
from aioresponses import aioresponses

@aioresponses()
def test_regexp_example(m):
    loop = asyncio.get_event_loop()
    session = aiohttp.ClientSession()
    pattern = re.compile(r'^http://example\.com/api\?foo=.*$')
    m.get(pattern, status=200)

    resp = loop.run_until_complete(session.get('http://example.com/api?foo=bar'))

    assert resp.status == 200

allows to passthrough to a specified list of servers

.. code:: python

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 allows to throw an exception

.. code:: python

import asyncio
from aiohttp import ClientSession
from aiohttp.http_exceptions import 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.

aioresponses allows to use callbacks to provide dynamic responses

.. code:: python

import asyncio
import aiohttp
from aioresponses import CallbackResult, aioresponses

def callback(url, **kwargs):
    return CallbackResult(status=418)

@aioresponses()
def test_callback(m, test_client):
    loop = asyncio.get_event_loop()
    session = ClientSession()
    m.get('http://example.com', callback=callback)

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

    assert resp.status == 418

aioresponses can be used in a pytest fixture

.. code:: python

import pytest
from aioresponses import aioresponses

@pytest.fixture
def mock_aioresponse():
    with aioresponses() as m:
        yield m

Features

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

License

  • Free software: MIT license

Credits

This package was created with Cookiecutter_ and the audreyr/cookiecutter-pypackage_ project template.

.. _Cookiecutter: https://github.com/audreyr/cookiecutter .. _audreyr/cookiecutter-pypackage: https://github.com/audreyr/cookiecutter-pypackage

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].