diff --git a/starlette/graphql.py b/starlette/graphql.py index a2eebebd..d3a64279 100644 --- a/starlette/graphql.py +++ b/starlette/graphql.py @@ -15,9 +15,9 @@ try: from graphql.error import GraphQLError except ImportError: # pragma: nocover graphene = None - AsyncioExecutor = None - format_graphql_error = None - GraphQLError = None + AsyncioExecutor = None # type: ignore + format_graphql_error = None # type: ignore + GraphQLError = None # type: ignore class GraphQLApp: diff --git a/starlette/routing.py b/starlette/routing.py index 52ddcdf2..e71b0035 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -561,18 +561,20 @@ class Router: await partial.handle(scope, receive, send) return - if scope["type"] == "http" and self.redirect_slashes: - if not scope["path"].endswith("/"): - redirect_scope = dict(scope) - redirect_scope["path"] += "/" + if scope["type"] == "http" and self.redirect_slashes and scope["path"] != "/": + redirect_scope = dict(scope) + if scope["path"].endswith("/"): + redirect_scope["path"] = redirect_scope["path"].rstrip("/") + else: + redirect_scope["path"] = redirect_scope["path"] + "/" - for route in self.routes: - match, child_scope = route.matches(redirect_scope) - if match != Match.NONE: - redirect_url = URL(scope=redirect_scope) - response = RedirectResponse(url=str(redirect_url)) - await response(scope, receive, send) - return + for route in self.routes: + match, child_scope = route.matches(redirect_scope) + if match != Match.NONE: + redirect_url = URL(scope=redirect_scope) + response = RedirectResponse(url=str(redirect_url)) + await response(scope, receive, send) + return await self.default(scope, receive, send) diff --git a/tests/test_routing.py b/tests/test_routing.py index 5347e8b1..e3de089b 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -117,6 +117,11 @@ def test_router(): assert response.status_code == 200 assert response.text == "User fixed me" + response = client.get("/users/tomchristie/") + assert response.status_code == 200 + assert response.url == "http://testserver/users/tomchristie" + assert response.text == "User tomchristie" + response = client.get("/users/nomatch") assert response.status_code == 200 assert response.text == "User nomatch"