Code Monkey home page Code Monkey logo

aresponses's Introduction

build status

aresponses

an asyncio testing server for mocking external services

Features

  • Fast mocks using actual network connections
  • allows mocking some types of network issues
  • use regular expression matching for domain, path, or method
  • works with https requests as well (by switching them to http requests)
  • works with callables

Usage

aresponses.add(host_pattern, path_pattern, method_pattern, response)

Host, path, or method may be either strings (exact match) or regular expressions.

When a request is received the first matching response will be returned (based on the order it was received in.

Requires Python 3.6 or greater.

Example

import pytest
import aiohttp

@pytest.mark.asyncio
async def test_foo(aresponses):
    # text as response (defaults to status 200 response)
    aresponses.add('foo.com', '/', 'get', 'hi there!!')

    # custom status code response
    aresponses.add('foo.com', '/', 'get', aresponses.Response(text='error', status=500))

    # passthrough response (makes an actual network call)
    aresponses.add('httpstat.us', '/200', 'get', aresponses.passthrough)

    # custom handler response
    def my_handler(request):
        return aresponses.Response(status=200, text=str(request.url))

    aresponses.add('foo.com', '/', 'get', my_handler)

    url = 'http://foo.com'
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            text = await response.text()
            assert text == 'hi there!!'

        async with session.get(url) as response:
            text = await response.text()
            assert text == 'error'
            assert response.status == 500

        async with session.get('https://httpstat.us/200') as response:
            text = await response.text()
        assert text == '200 OK'

        async with session.get(url) as response:
            text = await response.text()
            assert text == 'http://foo.com/'
import aiohttp
import pytest
import aresponses

@pytest.mark.asyncio
async def test_foo(event_loop):
    async with aresponses.ResponsesMockServer(loop=event_loop) as arsps:
        arsps.add('foo.com', '/', 'get', 'hi there!!')
        arsps.add(arsps.ANY, '/', 'get', arsps.Response(text='hey!'))
        
        async with aiohttp.ClientSession(loop=event_loop) as session:
            async with session.get('http://foo.com') as response:
                text = await response.text()
                assert text == 'hi'
            
            async with session.get('https://google.com') as response:
                text = await response.text()
                assert text == 'hey!'
        

Contributing

Dev environment setup

  • install pyenv - Makes it easy to install specific verisons of python and switch between them.
  • install python 3.6.2 using pyenv - pyenv install 3.6.2
  • install direnv - Manages virtual environments and automatically enables them when switching directories
  • clone (forked) github repo -
  • cd to project folder - after typing direnv allow, direnv will create a virtualenvironment and switch you to it
  • pip install -r requirements.txt - install all the requirements

Submitting a feature request

  • git checkout -b my-feature-branch
  • make some cool changes
  • pytest - do the tests pass?
  • pylama - is the code linter happy?
  • create pull request

Updating package on pypi

git tag 0.1
git push --tags
python setup.py bdist_wheel
python setup.py sdist
twine upload dist/* -u username

Changelog

1.1.0

  • Added passthrough option to permit live network calls
  • Added example of using a callable as a response

1.0.0

  • Added an optional match_querystring argument that lets you match on querystring as well

Contributors

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.