From 54405c9f4c0c5afde7814336320ac1c1e24c9d3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Perdiguero=20L=C3=B3pez?= Date: Mon, 11 Mar 2019 17:28:32 +0100 Subject: [PATCH] Fixes #7: get_route_from_scope works for mounted apps --- starlette_api/routing.py | 7 +++++-- tests/test_routing.py | 5 +++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/starlette_api/routing.py b/starlette_api/routing.py index 882888e9..e663dd33 100644 --- a/starlette_api/routing.py +++ b/starlette_api/routing.py @@ -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 diff --git a/tests/test_routing.py b/tests/test_routing.py index ef1b8a0e..4a887f92 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -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)