Skip to content

Commit

Permalink
Fix overshadowing of overlapped subbaps prefixes (aio-libs#3701).
Browse files Browse the repository at this point in the history
  • Loading branch information
nikie committed Apr 22, 2019
1 parent 9504fe2 commit 679142d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/3701.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix overshadowing of overlapped subbaps prefixes.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Eric Sheng
Erich Healy
Eugene Chernyshov
Eugene Naydenov
Eugene Nikolaiev
Eugene Tolmachev
Evert Lammerts
FichteFoll
Expand Down
3 changes: 2 additions & 1 deletion aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,8 @@ def get_info(self) -> Dict[str, Any]:
'prefix': self._prefix}

async def resolve(self, request: Request) -> _Resolve:
if not request.url.raw_path.startswith(self._prefix):
if not request.url.raw_path.startswith(self._prefix + '/') and \
request.url.raw_path != self._prefix:
return None, set()
match_info = await self._app.router.resolve(request)
match_info.add_app(self._app)
Expand Down
44 changes: 44 additions & 0 deletions tests/test_urldispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1246,3 +1246,47 @@ def test_prefixed_subapp_resource_canonical(app) -> None:
subapp = web.Application()
res = subapp.add_subapp(canonical, subapp)
assert res.canonical == canonical


async def test_prefixed_subapp_overlap(app) -> None:
"""
Subapp should not overshadow other subapps with overlapping prefixes
"""
subapp1 = web.Application()
handler1 = make_handler()
subapp1.router.add_get('/a', handler1)
app.add_subapp('/s', subapp1)

subapp2 = web.Application()
handler2 = make_handler()
subapp2.router.add_get('/b', handler2)
app.add_subapp('/ss', subapp2)

match_info = await app.router.resolve(make_mocked_request('GET', '/s/a'))
assert match_info.route.handler is handler1
match_info = await app.router.resolve(make_mocked_request('GET', '/ss/b'))
assert match_info.route.handler is handler2


async def test_prefixed_subapp_empty_route(app) -> None:
subapp = web.Application()
handler = make_handler()
subapp.router.add_get('', handler)
app.add_subapp('/s', subapp)

match_info = await app.router.resolve(make_mocked_request('GET', '/s'))
assert match_info.route.handler is handler
match_info = await app.router.resolve(make_mocked_request('GET', '/s/'))
assert "<MatchInfoError 404: Not Found>" == repr(match_info)


async def test_prefixed_subapp_root_route(app) -> None:
subapp = web.Application()
handler = make_handler()
subapp.router.add_get('/', handler)
app.add_subapp('/s', subapp)

match_info = await app.router.resolve(make_mocked_request('GET', '/s/'))
assert match_info.route.handler is handler
match_info = await app.router.resolve(make_mocked_request('GET', '/s'))
assert "<MatchInfoError 404: Not Found>" == repr(match_info)

0 comments on commit 679142d

Please sign in to comment.