Coverage for tests/test_default_response_class.py: 100%
141 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 typing import Any 1abcde
3import orjson 1abcde
4from fastapi import APIRouter, FastAPI 1abcde
5from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse 1abcde
6from fastapi.testclient import TestClient 1abcde
9class ORJSONResponse(JSONResponse): 1abcde
10 media_type = "application/x-orjson" 1abcde
12 def render(self, content: Any) -> bytes: 1abcde
13 return orjson.dumps(content) 1abcde
16class OverrideResponse(JSONResponse): 1abcde
17 media_type = "application/x-override" 1abcde
20app = FastAPI(default_response_class=ORJSONResponse) 1abcde
21router_a = APIRouter() 1abcde
22router_a_a = APIRouter() 1abcde
23router_a_b_override = APIRouter() # Overrides default class 1abcde
24router_b_override = APIRouter() # Overrides default class 1abcde
25router_b_a = APIRouter() 1abcde
26router_b_a_c_override = APIRouter() # Overrides default class again 1abcde
29@app.get("/") 1abcde
30def get_root(): 1abcde
31 return {"msg": "Hello World"} 1abcde
34@app.get("/override", response_class=PlainTextResponse) 1abcde
35def get_path_override(): 1abcde
36 return "Hello World" 1abcde
39@router_a.get("/") 1abcde
40def get_a(): 1abcde
41 return {"msg": "Hello A"} 1abcde
44@router_a.get("/override", response_class=PlainTextResponse) 1abcde
45def get_a_path_override(): 1abcde
46 return "Hello A" 1abcde
49@router_a_a.get("/") 1abcde
50def get_a_a(): 1abcde
51 return {"msg": "Hello A A"} 1abcde
54@router_a_a.get("/override", response_class=PlainTextResponse) 1abcde
55def get_a_a_path_override(): 1abcde
56 return "Hello A A" 1abcde
59@router_a_b_override.get("/") 1abcde
60def get_a_b(): 1abcde
61 return "Hello A B" 1abcde
64@router_a_b_override.get("/override", response_class=HTMLResponse) 1abcde
65def get_a_b_path_override(): 1abcde
66 return "Hello A B" 1abcde
69@router_b_override.get("/") 1abcde
70def get_b(): 1abcde
71 return "Hello B" 1abcde
74@router_b_override.get("/override", response_class=HTMLResponse) 1abcde
75def get_b_path_override(): 1abcde
76 return "Hello B" 1abcde
79@router_b_a.get("/") 1abcde
80def get_b_a(): 1abcde
81 return "Hello B A" 1abcde
84@router_b_a.get("/override", response_class=HTMLResponse) 1abcde
85def get_b_a_path_override(): 1abcde
86 return "Hello B A" 1abcde
89@router_b_a_c_override.get("/") 1abcde
90def get_b_a_c(): 1abcde
91 return "Hello B A C" 1abcde
94@router_b_a_c_override.get("/override", response_class=OverrideResponse) 1abcde
95def get_b_a_c_path_override(): 1abcde
96 return {"msg": "Hello B A C"} 1abcde
99router_b_a.include_router( 1abcde
100 router_b_a_c_override, prefix="/c", default_response_class=HTMLResponse
101)
102router_b_override.include_router(router_b_a, prefix="/a") 1abcde
103router_a.include_router(router_a_a, prefix="/a") 1abcde
104router_a.include_router( 1abcde
105 router_a_b_override, prefix="/b", default_response_class=PlainTextResponse
106)
107app.include_router(router_a, prefix="/a") 1abcde
108app.include_router( 1abcde
109 router_b_override, prefix="/b", default_response_class=PlainTextResponse
110)
113client = TestClient(app) 1abcde
115orjson_type = "application/x-orjson" 1abcde
116text_type = "text/plain; charset=utf-8" 1abcde
117html_type = "text/html; charset=utf-8" 1abcde
118override_type = "application/x-override" 1abcde
121def test_app(): 1abcde
122 with client: 1abcde
123 response = client.get("/") 1abcde
124 assert response.json() == {"msg": "Hello World"} 1abcde
125 assert response.headers["content-type"] == orjson_type 1abcde
128def test_app_override(): 1abcde
129 with client: 1abcde
130 response = client.get("/override") 1abcde
131 assert response.content == b"Hello World" 1abcde
132 assert response.headers["content-type"] == text_type 1abcde
135def test_router_a(): 1abcde
136 with client: 1abcde
137 response = client.get("/a") 1abcde
138 assert response.json() == {"msg": "Hello A"} 1abcde
139 assert response.headers["content-type"] == orjson_type 1abcde
142def test_router_a_override(): 1abcde
143 with client: 1abcde
144 response = client.get("/a/override") 1abcde
145 assert response.content == b"Hello A" 1abcde
146 assert response.headers["content-type"] == text_type 1abcde
149def test_router_a_a(): 1abcde
150 with client: 1abcde
151 response = client.get("/a/a") 1abcde
152 assert response.json() == {"msg": "Hello A A"} 1abcde
153 assert response.headers["content-type"] == orjson_type 1abcde
156def test_router_a_a_override(): 1abcde
157 with client: 1abcde
158 response = client.get("/a/a/override") 1abcde
159 assert response.content == b"Hello A A" 1abcde
160 assert response.headers["content-type"] == text_type 1abcde
163def test_router_a_b(): 1abcde
164 with client: 1abcde
165 response = client.get("/a/b") 1abcde
166 assert response.content == b"Hello A B" 1abcde
167 assert response.headers["content-type"] == text_type 1abcde
170def test_router_a_b_override(): 1abcde
171 with client: 1abcde
172 response = client.get("/a/b/override") 1abcde
173 assert response.content == b"Hello A B" 1abcde
174 assert response.headers["content-type"] == html_type 1abcde
177def test_router_b(): 1abcde
178 with client: 1abcde
179 response = client.get("/b") 1abcde
180 assert response.content == b"Hello B" 1abcde
181 assert response.headers["content-type"] == text_type 1abcde
184def test_router_b_override(): 1abcde
185 with client: 1abcde
186 response = client.get("/b/override") 1abcde
187 assert response.content == b"Hello B" 1abcde
188 assert response.headers["content-type"] == html_type 1abcde
191def test_router_b_a(): 1abcde
192 with client: 1abcde
193 response = client.get("/b/a") 1abcde
194 assert response.content == b"Hello B A" 1abcde
195 assert response.headers["content-type"] == text_type 1abcde
198def test_router_b_a_override(): 1abcde
199 with client: 1abcde
200 response = client.get("/b/a/override") 1abcde
201 assert response.content == b"Hello B A" 1abcde
202 assert response.headers["content-type"] == html_type 1abcde
205def test_router_b_a_c(): 1abcde
206 with client: 1abcde
207 response = client.get("/b/a/c") 1abcde
208 assert response.content == b"Hello B A C" 1abcde
209 assert response.headers["content-type"] == html_type 1abcde
212def test_router_b_a_c_override(): 1abcde
213 with client: 1abcde
214 response = client.get("/b/a/c/override") 1abcde
215 assert response.json() == {"msg": "Hello B A C"} 1abcde
216 assert response.headers["content-type"] == override_type 1abcde