Browse Source

Add allow disabling `redirect_slashes` at the FastAPI app level (#3432)

Co-authored-by: Denis Lisovik <[email protected]>
Co-authored-by: Sebastián Ramírez <[email protected]>
pull/9720/head
cyberlis 2 years ago
committed by GitHub
parent
commit
e94c13ce74
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      fastapi/applications.py
  2. 40
      tests/test_router_redirect_slashes.py

2
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,

40
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
Loading…
Cancel
Save