]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
✨ Add allow disabling `redirect_slashes` at the FastAPI app level (#3432)
authorcyberlis <cyberlis@rccraft.ru>
Thu, 22 Jun 2023 10:37:50 +0000 (13:37 +0300)
committerGitHub <noreply@github.com>
Thu, 22 Jun 2023 10:37:50 +0000 (10:37 +0000)
Co-authored-by: Denis Lisovik <ckyberlis@gmail.com>
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
fastapi/applications.py
tests/test_router_redirect_slashes.py [new file with mode: 0644]

index 298aca921d70c265cc9de95916002ac4b1955abf..9b161c5ec832fa582acf30945bf4f78d16a9b342 100644 (file)
@@ -62,6 +62,7 @@ class FastAPI(Starlette):
         servers: Optional[List[Dict[str, Union[str, Any]]]] = None,
         dependencies: Optional[Sequence[Depends]] = None,
         default_response_class: Type[Response] = Default(JSONResponse),
+        redirect_slashes: bool = True,
         docs_url: Optional[str] = "/docs",
         redoc_url: Optional[str] = "/redoc",
         swagger_ui_oauth2_redirect_url: Optional[str] = "/docs/oauth2-redirect",
@@ -127,6 +128,7 @@ class FastAPI(Starlette):
         self.dependency_overrides: Dict[Callable[..., Any], Callable[..., Any]] = {}
         self.router: routing.APIRouter = routing.APIRouter(
             routes=routes,
+            redirect_slashes=redirect_slashes,
             dependency_overrides_provider=self,
             on_startup=on_startup,
             on_shutdown=on_shutdown,
diff --git a/tests/test_router_redirect_slashes.py b/tests/test_router_redirect_slashes.py
new file mode 100644 (file)
index 0000000..086665c
--- /dev/null
@@ -0,0 +1,40 @@
+from fastapi import APIRouter, FastAPI
+from fastapi.testclient import TestClient
+
+
+def test_redirect_slashes_enabled():
+    app = FastAPI()
+    router = APIRouter()
+
+    @router.get("/hello/")
+    def hello_page() -> str:
+        return "Hello, World!"
+
+    app.include_router(router)
+
+    client = TestClient(app)
+
+    response = client.get("/hello/", follow_redirects=False)
+    assert response.status_code == 200
+
+    response = client.get("/hello", follow_redirects=False)
+    assert response.status_code == 307
+
+
+def test_redirect_slashes_disabled():
+    app = FastAPI(redirect_slashes=False)
+    router = APIRouter()
+
+    @router.get("/hello/")
+    def hello_page() -> str:
+        return "Hello, World!"
+
+    app.include_router(router)
+
+    client = TestClient(app)
+
+    response = client.get("/hello/", follow_redirects=False)
+    assert response.status_code == 200
+
+    response = client.get("/hello", follow_redirects=False)
+    assert response.status_code == 404