Coverage for tests/test_router_redirect_slashes.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from fastapi import APIRouter, FastAPI 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4 

5def test_redirect_slashes_enabled(): 1abcde

6 app = FastAPI() 1abcde

7 router = APIRouter() 1abcde

8 

9 @router.get("/hello/") 1abcde

10 def hello_page() -> str: 1abcde

11 return "Hello, World!" 1abcde

12 

13 app.include_router(router) 1abcde

14 

15 client = TestClient(app) 1abcde

16 

17 response = client.get("/hello/", follow_redirects=False) 1abcde

18 assert response.status_code == 200 1abcde

19 

20 response = client.get("/hello", follow_redirects=False) 1abcde

21 assert response.status_code == 307 1abcde

22 

23 

24def test_redirect_slashes_disabled(): 1abcde

25 app = FastAPI(redirect_slashes=False) 1abcde

26 router = APIRouter() 1abcde

27 

28 @router.get("/hello/") 1abcde

29 def hello_page() -> str: 1abcde

30 return "Hello, World!" 1abcde

31 

32 app.include_router(router) 1abcde

33 

34 client = TestClient(app) 1abcde

35 

36 response = client.get("/hello/", follow_redirects=False) 1abcde

37 assert response.status_code == 200 1abcde

38 

39 response = client.get("/hello", follow_redirects=False) 1abcde

40 assert response.status_code == 404 1abcde