mirror of https://github.com/tiangolo/fastapi.git
✨ 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:
parent
b7ce10079e
commit
e94c13ce74
|
|
@ -62,6 +62,7 @@ class FastAPI(Starlette):
|
||||||
servers: Optional[List[Dict[str, Union[str, Any]]]] = None,
|
servers: Optional[List[Dict[str, Union[str, Any]]]] = None,
|
||||||
dependencies: Optional[Sequence[Depends]] = None,
|
dependencies: Optional[Sequence[Depends]] = None,
|
||||||
default_response_class: Type[Response] = Default(JSONResponse),
|
default_response_class: Type[Response] = Default(JSONResponse),
|
||||||
|
redirect_slashes: bool = True,
|
||||||
docs_url: Optional[str] = "/docs",
|
docs_url: Optional[str] = "/docs",
|
||||||
redoc_url: Optional[str] = "/redoc",
|
redoc_url: Optional[str] = "/redoc",
|
||||||
swagger_ui_oauth2_redirect_url: Optional[str] = "/docs/oauth2-redirect",
|
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.dependency_overrides: Dict[Callable[..., Any], Callable[..., Any]] = {}
|
||||||
self.router: routing.APIRouter = routing.APIRouter(
|
self.router: routing.APIRouter = routing.APIRouter(
|
||||||
routes=routes,
|
routes=routes,
|
||||||
|
redirect_slashes=redirect_slashes,
|
||||||
dependency_overrides_provider=self,
|
dependency_overrides_provider=self,
|
||||||
on_startup=on_startup,
|
on_startup=on_startup,
|
||||||
on_shutdown=on_shutdown,
|
on_shutdown=on_shutdown,
|
||||||
|
|
|
||||||
|
|
@ -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…
Reference in New Issue