mirror of
https://github.com/fastapi/fastapi.git
synced 2026-04-11 02:18:56 -04:00
✨ Add allow disabling redirect_slashes at the FastAPI app level (#3432)
Co-authored-by: Denis Lisovik <ckyberlis@gmail.com> Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
@@ -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
Normal file
40
tests/test_router_redirect_slashes.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user