From e94c13ce74990eb682aead3fd976df45beee93d6 Mon Sep 17 00:00:00 2001 From: cyberlis Date: Thu, 22 Jun 2023 13:37:50 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20allow=20disabling=20`redirect?= =?UTF-8?q?=5Fslashes`=20at=20the=20FastAPI=20app=20level=20(#3432)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Denis Lisovik Co-authored-by: Sebastián Ramírez --- fastapi/applications.py | 2 ++ tests/test_router_redirect_slashes.py | 40 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 tests/test_router_redirect_slashes.py diff --git a/fastapi/applications.py b/fastapi/applications.py index 298aca921..9b161c5ec 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -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 index 000000000..086665c04 --- /dev/null +++ b/tests/test_router_redirect_slashes.py @@ -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