Aiohttp: Responses-like lib/example for mocking requests

Created on 7 Aug 2016  路  6Comments  路  Source: aio-libs/aiohttp

Is there any library or example of mocking requests to random url, something similar like https://github.com/getsentry/responses for https://github.com/kennethreitz/requests/.

For example I have API that trigger some facebook API:

async def send_message(sender, text):
    async with session.post(
            'https://graph.facebook.com/v2.6/me/messages/',
            params={
                'access_token': os.environ['FB_ACCESS_TOKEN']
            },
            data={
                'recipient': {'id': sender},
                'message': {'text': text}
            }) as resp:
        return await resp.json()

and I'd like to have some simple unit test for it, like:

@responses.activate
async def test_my_api():
    responses.add(responses.POST, 'https://graph.facebook.com/v2.6/me/messages/',
                  body='{"status": "ok"}', status=200,
                  content_type='application/json')

    resp = send_message(1, 'hi!')

    assert resp.status == 'ok'

sadly responses doesn't work with aiohttp

duplicate outdated

All 6 comments

Hi Eugene,

I'm using something like this for mocking requests to external services. You can see the code for building the ClientResponse object in the changes. Maybe it's not the best solution but for me it's similar of how I was mocking with the requests library. If you need further help of how it works don't hesitate to ask :).

Superseded by #980

aresponses is better, sure.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].

Was this page helpful?
0 / 5 - 0 ratings