Coverage for tests/test_default_response_class_router.py: 100%
135 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from fastapi import APIRouter, FastAPI 1abcde
2from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse 1abcde
3from fastapi.testclient import TestClient 1abcde
6class OverrideResponse(JSONResponse): 1abcde
7 media_type = "application/x-override" 1abcde
10app = FastAPI() 1abcde
11router_a = APIRouter() 1abcde
12router_a_a = APIRouter() 1abcde
13router_a_b_override = APIRouter() # Overrides default class 1abcde
14router_b_override = APIRouter() # Overrides default class 1abcde
15router_b_a = APIRouter() 1abcde
16router_b_a_c_override = APIRouter() # Overrides default class again 1abcde
19@app.get("/") 1abcde
20def get_root(): 1abcde
21 return {"msg": "Hello World"} 1abcde
24@app.get("/override", response_class=PlainTextResponse) 1abcde
25def get_path_override(): 1abcde
26 return "Hello World" 1abcde
29@router_a.get("/") 1abcde
30def get_a(): 1abcde
31 return {"msg": "Hello A"} 1abcde
34@router_a.get("/override", response_class=PlainTextResponse) 1abcde
35def get_a_path_override(): 1abcde
36 return "Hello A" 1abcde
39@router_a_a.get("/") 1abcde
40def get_a_a(): 1abcde
41 return {"msg": "Hello A A"} 1abcde
44@router_a_a.get("/override", response_class=PlainTextResponse) 1abcde
45def get_a_a_path_override(): 1abcde
46 return "Hello A A" 1abcde
49@router_a_b_override.get("/") 1abcde
50def get_a_b(): 1abcde
51 return "Hello A B" 1abcde
54@router_a_b_override.get("/override", response_class=HTMLResponse) 1abcde
55def get_a_b_path_override(): 1abcde
56 return "Hello A B" 1abcde
59@router_b_override.get("/") 1abcde
60def get_b(): 1abcde
61 return "Hello B" 1abcde
64@router_b_override.get("/override", response_class=HTMLResponse) 1abcde
65def get_b_path_override(): 1abcde
66 return "Hello B" 1abcde
69@router_b_a.get("/") 1abcde
70def get_b_a(): 1abcde
71 return "Hello B A" 1abcde
74@router_b_a.get("/override", response_class=HTMLResponse) 1abcde
75def get_b_a_path_override(): 1abcde
76 return "Hello B A" 1abcde
79@router_b_a_c_override.get("/") 1abcde
80def get_b_a_c(): 1abcde
81 return "Hello B A C" 1abcde
84@router_b_a_c_override.get("/override", response_class=OverrideResponse) 1abcde
85def get_b_a_c_path_override(): 1abcde
86 return {"msg": "Hello B A C"} 1abcde
89router_b_a.include_router( 1abcde
90 router_b_a_c_override, prefix="/c", default_response_class=HTMLResponse
91)
92router_b_override.include_router(router_b_a, prefix="/a") 1abcde
93router_a.include_router(router_a_a, prefix="/a") 1abcde
94router_a.include_router( 1abcde
95 router_a_b_override, prefix="/b", default_response_class=PlainTextResponse
96)
97app.include_router(router_a, prefix="/a") 1abcde
98app.include_router( 1abcde
99 router_b_override, prefix="/b", default_response_class=PlainTextResponse
100)
103client = TestClient(app) 1abcde
105json_type = "application/json" 1abcde
106text_type = "text/plain; charset=utf-8" 1abcde
107html_type = "text/html; charset=utf-8" 1abcde
108override_type = "application/x-override" 1abcde
111def test_app(): 1abcde
112 with client: 1abcde
113 response = client.get("/") 1abcde
114 assert response.json() == {"msg": "Hello World"} 1abcde
115 assert response.headers["content-type"] == json_type 1abcde
118def test_app_override(): 1abcde
119 with client: 1abcde
120 response = client.get("/override") 1abcde
121 assert response.content == b"Hello World" 1abcde
122 assert response.headers["content-type"] == text_type 1abcde
125def test_router_a(): 1abcde
126 with client: 1abcde
127 response = client.get("/a") 1abcde
128 assert response.json() == {"msg": "Hello A"} 1abcde
129 assert response.headers["content-type"] == json_type 1abcde
132def test_router_a_override(): 1abcde
133 with client: 1abcde
134 response = client.get("/a/override") 1abcde
135 assert response.content == b"Hello A" 1abcde
136 assert response.headers["content-type"] == text_type 1abcde
139def test_router_a_a(): 1abcde
140 with client: 1abcde
141 response = client.get("/a/a") 1abcde
142 assert response.json() == {"msg": "Hello A A"} 1abcde
143 assert response.headers["content-type"] == json_type 1abcde
146def test_router_a_a_override(): 1abcde
147 with client: 1abcde
148 response = client.get("/a/a/override") 1abcde
149 assert response.content == b"Hello A A" 1abcde
150 assert response.headers["content-type"] == text_type 1abcde
153def test_router_a_b(): 1abcde
154 with client: 1abcde
155 response = client.get("/a/b") 1abcde
156 assert response.content == b"Hello A B" 1abcde
157 assert response.headers["content-type"] == text_type 1abcde
160def test_router_a_b_override(): 1abcde
161 with client: 1abcde
162 response = client.get("/a/b/override") 1abcde
163 assert response.content == b"Hello A B" 1abcde
164 assert response.headers["content-type"] == html_type 1abcde
167def test_router_b(): 1abcde
168 with client: 1abcde
169 response = client.get("/b") 1abcde
170 assert response.content == b"Hello B" 1abcde
171 assert response.headers["content-type"] == text_type 1abcde
174def test_router_b_override(): 1abcde
175 with client: 1abcde
176 response = client.get("/b/override") 1abcde
177 assert response.content == b"Hello B" 1abcde
178 assert response.headers["content-type"] == html_type 1abcde
181def test_router_b_a(): 1abcde
182 with client: 1abcde
183 response = client.get("/b/a") 1abcde
184 assert response.content == b"Hello B A" 1abcde
185 assert response.headers["content-type"] == text_type 1abcde
188def test_router_b_a_override(): 1abcde
189 with client: 1abcde
190 response = client.get("/b/a/override") 1abcde
191 assert response.content == b"Hello B A" 1abcde
192 assert response.headers["content-type"] == html_type 1abcde
195def test_router_b_a_c(): 1abcde
196 with client: 1abcde
197 response = client.get("/b/a/c") 1abcde
198 assert response.content == b"Hello B A C" 1abcde
199 assert response.headers["content-type"] == html_type 1abcde
202def test_router_b_a_c_override(): 1abcde
203 with client: 1abcde
204 response = client.get("/b/a/c/override") 1abcde
205 assert response.json() == {"msg": "Hello B A C"} 1abcde
206 assert response.headers["content-type"] == override_type 1abcde