Last active
July 11, 2020 21:00
-
-
Save ricardodani/a6bd58ee398cbd5f8eb82eb382cd0658 to your computer and use it in GitHub Desktop.
fakeserver_aiohttp.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
import aiohttp | |
from aiohttp import web | |
def create_magic_app(): | |
app = web.Application() | |
return app | |
@pytest.fixture | |
def magic_server(loop, test_server): | |
app = create_magic_app() | |
server = loop.run_until_complete(test_server(app)) | |
server.url = f'http://localhost:{server.port}' | |
return server | |
async def foobar(endpoint): | |
""" | |
This is the function to test | |
""" | |
async with aiohttp.ClientSession() as session: | |
async with session.get(endpoint) as r: | |
t = await r.text() | |
return 'bar' | |
async def test_index(request): | |
return web.Response(text='hello') | |
async def test_foobar(magic_server): | |
# FIXME: the following won't work yet, would need someway to freeze the app on first request | |
# or add endpoints after freezing | |
# add_get, add_post, add_route work like app.router.* methods, but allow responses as arguments | |
magic_server.add_get('/', test_index) # will call test_index and return result | |
magic_server.add_post('/whatever/', web.HTTPConflict()) # will just return 409 | |
v = await foobar(server.url) | |
assert v == 'bar' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment