pnuckowski / aioresponses

Aioresponses is a helper for mock/fake web requests in python aiohttp package.
MIT License
517 stars 87 forks source link

Can i used aioresponse for server side in test code? #158

Open kimeuichan opened 4 years ago

kimeuichan commented 4 years ago

In my case, use aiohttp in server side..

requests_mock using like this in test case, It's working on server side mock.

with requests_mock.Mocker() as mock:
   mock.get('/', text='text')

but aioresponses is not working on server side. It's working only test case.. can i used aioresponses in test case for mock server side request??

my server app is falcon and I write test case like this.

def test_main(client):
    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

        resp = client.simulate_get('/')

        assert resp.status == falcon.HTTP_OK

    session.close()

In my server code

import asyncio

import aiohttp
import falcon

class MainResource:
    def on_get(self, req, resp):
        data = {
            'abcd': 'abcd',
        }

        resp.media = data

        session = aiohttp.ClientSession()
        loop = asyncio.get_event_loop()
        resp = loop.run_until_complete(session.get('http://test.example.com'))

api = falcon.API()
api.add_route('/', MainResource())
pnuckowski commented 4 years ago

Hello @kimeuichan Try with this

with aioresponses(passthrough=['http://127.0.0.1:']) as m: