From 0e4cdb3bc740dd30c9c0d3fa71bc5d6b4e4471c4 Mon Sep 17 00:00:00 2001 From: Ricardo Madriz Date: Sun, 18 Feb 2024 12:34:38 -0600 Subject: [PATCH] Add test to demonstrate bug --- tests/test_root_path_redirects.py | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/test_root_path_redirects.py diff --git a/tests/test_root_path_redirects.py b/tests/test_root_path_redirects.py new file mode 100644 index 000000000..5a6d6cd2a --- /dev/null +++ b/tests/test_root_path_redirects.py @@ -0,0 +1,36 @@ +from fastapi import APIRouter, FastAPI +from fastapi.testclient import TestClient + + +def test_redirects_without_root_path(): + app = FastAPI() + router = APIRouter() + + @router.get("/hello/") + def hello_page() -> str: + return "Hello, World!" + + app.include_router(router) + + client = TestClient(app, base_url="http://testserver") + + response = client.get("/hello", follow_redirects=False) + assert response.status_code == 307 + assert response.headers["location"] == "http://testserver/hello/" + + +def test_redirects_with_root_path(): + app = FastAPI(root_path="/api") + router = APIRouter() + + @router.get("/hello/") + def hello_page() -> str: + return "Hello, World!" + + app.include_router(router) + + client = TestClient(app, base_url="http://testserver") + + response = client.get("/hello", follow_redirects=False) + assert response.status_code == 307 + assert response.headers["location"] == "http://testserver/api/hello/"