Skip to content

Commit

Permalink
Fixes #7: get_route_from_scope works for mounted apps
Browse files Browse the repository at this point in the history
  • Loading branch information
perdy committed Mar 11, 2019
1 parent e81ce2d commit 54405c9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 5 additions & 2 deletions starlette_api/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,17 @@ def get_route_from_scope(self, scope) -> typing.Tuple[Route, typing.Optional[typ
partial = None

for route in self.routes:
if isinstance(route, Mount):
path = scope.get("path", "")
root_path = scope.pop("root_path", "")
scope["path"] = root_path + path

match, child_scope = route.matches(scope)
if match == Match.FULL:
scope.update(child_scope)

if isinstance(route, Mount):
root_path = scope.pop("root_path")
route, mount_scope = route.app.get_route_from_scope(scope)
mount_scope["root_path"] = root_path
return route, mount_scope

return route, scope
Expand Down
5 changes: 3 additions & 2 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,15 @@ async def foo():
assert route_scope["root_path"] == ""
assert route_scope["endpoint"] == foo

def test_get_route_from_scope_mount(self, app, router, scope):
def test_get_route_from_scope_mount_view(self, app, router, scope):
@router.route("/foo/")
async def foo():
return "foo"

app.mount("/router", app=router)

scope["path"] = "/router/foo/"
scope["path"] = "/foo/"
scope["root_path"] = "/router"
scope["method"] = "GET"

route, route_scope = app.router.get_route_from_scope(scope=scope)
Expand Down

0 comments on commit 54405c9

Please sign in to comment.