Coverage for tests / test_custom_swagger_ui_redirect.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from fastapi import FastAPI 1abcd

2from fastapi.testclient import TestClient 1abcd

3 

4swagger_ui_oauth2_redirect_url = "/docs/redirect" 1abcd

5 

6app = FastAPI(swagger_ui_oauth2_redirect_url=swagger_ui_oauth2_redirect_url) 1abcd

7 

8 

9@app.get("/items/") 1abcd

10async def read_items(): 1abcd

11 return {"id": "foo"} 1klm

12 

13 

14client = TestClient(app) 1abcd

15 

16 

17def test_swagger_ui(): 1abcd

18 response = client.get("/docs") 1efg

19 assert response.status_code == 200, response.text 1efg

20 assert response.headers["content-type"] == "text/html; charset=utf-8" 1efg

21 assert "swagger-ui-dist" in response.text 1efg

22 print(client.base_url) 1efg

23 assert ( 1ef

24 f"oauth2RedirectUrl: window.location.origin + '{swagger_ui_oauth2_redirect_url}'" 

25 in response.text 

26 ) 

27 

28 

29def test_swagger_ui_oauth2_redirect(): 1abcd

30 response = client.get(swagger_ui_oauth2_redirect_url) 1hij

31 assert response.status_code == 200, response.text 1hij

32 assert response.headers["content-type"] == "text/html; charset=utf-8" 1hij

33 assert "window.opener.swaggerUIRedirectOauth2" in response.text 1hij

34 

35 

36def test_response(): 1abcd

37 response = client.get("/items/") 1klm

38 assert response.json() == {"id": "foo"} 1klm