Skip to content

Instantly share code, notes, and snippets.

@matutter
Last active October 2, 2019 22:09
Show Gist options
  • Select an option

  • Save matutter/a1d7726cf0c16bf847c75eaf2b62ae46 to your computer and use it in GitHub Desktop.

Select an option

Save matutter/a1d7726cf0c16bf847c75eaf2b62ae46 to your computer and use it in GitHub Desktop.
aiohttp dynamic resource in subapp prefix
#!/usr/bin/python3.7
# pytest -s test_subapps.py
# pip install pytest aiohttp pytest-aiohttp
from aiohttp import web
import pytest
def handle_1(request):
return web.Response(text="OK 1")
def handle_2(request):
key = request.match_info.get('key')
return web.Response(text=f"OK {key}")
@pytest.fixture
def an_app():
root = web.Application()
root.router.add_get('/1', handle_1)
child = web.Application()
if False:
# This works, it makes a "<DynamicResource /child/{key}/2>"
child.router.add_get('/{key}/2', handle_2)
root.add_subapp('/child', child)
else:
# This breaks, it makes a "<PlainResource /child/{key}/2>"
child.router.add_get('/2', handle_2)
root.add_subapp('/child/{key}', child)
print()
print(root.router.routes()._routes)
return root
@pytest.fixture
async def cli(test_client, an_app):
client = await test_client(an_app)
return client
async def test_this_does_not_work(cli):
res1 = await cli.get('/1')
res2 = await cli.get('/child/abc/2')
print()
print(await res1.text())
print(await res2.text())
assert res1.status == 200
assert res2.status == 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment