Debian Codegraph @

tests/test_tutorial/test_header_param_models/test_tutorial003.py

raw
1 import importlib
2
3 import pytest
4 from dirty_equals import IsOneOf
5 from fastapi.testclient import TestClient
6 from inline_snapshot import snapshot
7
8 from tests.utils import needs_py310
9
10
11 @pytest.fixture(
12 name="client",
13 params=[
14 pytest.param("tutorial003_py310", marks=needs_py310),
15 pytest.param("tutorial003_an_py310", marks=needs_py310),
16 ],
17 )
18 def get_client(request: pytest.FixtureRequest):
19 mod = importlib.import_module(f"docs_src.header_param_models.{request.param}")
20
21 client = TestClient(mod.app)
22 return client
23
24
25 def test_header_param_model(client: TestClient):
26 response = client.get(
27 "/items/",
28 headers=[
29 ("save_data", "true"),
30 ("if_modified_since", "yesterday"),
31 ("traceparent", "123"),
32 ("x_tag", "one"),
33 ("x_tag", "two"),
34 ],
35 )
36 assert response.status_code == 200
37 assert response.json() == {
38 "host": "testserver",
39 "save_data": True,
40 "if_modified_since": "yesterday",
41 "traceparent": "123",
42 "x_tag": ["one", "two"],
43 }
44
45
46 def test_header_param_model_no_underscore(client: TestClient):
47 response = client.get(
48 "/items/",
49 headers=[
50 ("save-data", "true"),
51 ("if-modified-since", "yesterday"),
52 ("traceparent", "123"),
53 ("x-tag", "one"),
54 ("x-tag", "two"),
55 ],
56 )
57 assert response.status_code == 422
58 assert response.json() == snapshot(
59 {
60 "detail": [
61 {
62 "type": "missing",
63 "loc": ["header", "save_data"],
64 "msg": "Field required",
65 "input": {
66 "host": "testserver",
67 "traceparent": "123",
68 "x_tag": [],
69 "accept": "*/*",
70 "accept-encoding": IsOneOf(
71 "gzip, deflate", "gzip, deflate, zstd"
72 ),
73 "connection": "keep-alive",
74 "user-agent": "testclient",
75 "save-data": "true",
76 "if-modified-since": "yesterday",
77 "x-tag": ["one", "two"],
78 },
79 }
80 ]
81 }
82 )
83
84
85 def test_header_param_model_defaults(client: TestClient):
86 response = client.get("/items/", headers=[("save_data", "true")])
87 assert response.status_code == 200
88 assert response.json() == {
89 "host": "testserver",
90 "save_data": True,
91 "if_modified_since": None,
92 "traceparent": None,
93 "x_tag": [],
94 }
95
96
97 def test_header_param_model_invalid(client: TestClient):
98 response = client.get("/items/")
99 assert response.status_code == 422
100 assert response.json() == snapshot(
101 {
102 "detail": [
103 {
104 "type": "missing",
105 "loc": ["header", "save_data"],
106 "msg": "Field required",
107 "input": {
108 "x_tag": [],
109 "host": "testserver",
110 "accept": "*/*",
111 "accept-encoding": IsOneOf(
112 "gzip, deflate", "gzip, deflate, zstd"
113 ),
114 "connection": "keep-alive",
115 "user-agent": "testclient",
116 },
117 }
118 ]
119 }
120 )
121
122
123 def test_header_param_model_extra(client: TestClient):
124 response = client.get(
125 "/items/", headers=[("save_data", "true"), ("tool", "plumbus")]
126 )
127 assert response.status_code == 200, response.text
128 assert response.json() == snapshot(
129 {
130 "host": "testserver",
131 "save_data": True,
132 "if_modified_since": None,
133 "traceparent": None,
134 "x_tag": [],
135 }
136 )
137
138
139 def test_openapi_schema(client: TestClient):
140 response = client.get("/openapi.json")
141 assert response.status_code == 200, response.text
142 assert response.json() == snapshot(
143 {
144 "openapi": "3.1.0",
145 "info": {"title": "FastAPI", "version": "0.1.0"},
146 "paths": {
147 "/items/": {
148 "get": {
149 "summary": "Read Items",
150 "operationId": "read_items_items__get",
151 "parameters": [
152 {
153 "name": "host",
154 "in": "header",
155 "required": True,
156 "schema": {"type": "string", "title": "Host"},
157 },
158 {
159 "name": "save_data",
160 "in": "header",
161 "required": True,
162 "schema": {"type": "boolean", "title": "Save Data"},
163 },
164 {
165 "name": "if_modified_since",
166 "in": "header",
167 "required": False,
168 "schema": {
169 "anyOf": [{"type": "string"}, {"type": "null"}],
170 "title": "If Modified Since",
171 },
172 },
173 {
174 "name": "traceparent",
175 "in": "header",
176 "required": False,
177 "schema": {
178 "anyOf": [{"type": "string"}, {"type": "null"}],
179 "title": "Traceparent",
180 },
181 },
182 {
183 "name": "x_tag",
184 "in": "header",
185 "required": False,
186 "schema": {
187 "type": "array",
188 "items": {"type": "string"},
189 "default": [],
190 "title": "X Tag",
191 },
192 },
193 ],
194 "responses": {
195 "200": {
196 "description": "Successful Response",
197 "content": {"application/json": {"schema": {}}},
198 },
199 "422": {
200 "description": "Validation Error",
201 "content": {
202 "application/json": {
203 "schema": {
204 "$ref": "#/components/schemas/HTTPValidationError"
205 }
206 }
207 },
208 },
209 },
210 }
211 }
212 },
213 "components": {
214 "schemas": {
215 "HTTPValidationError": {
216 "properties": {
217 "detail": {
218 "items": {
219 "$ref": "#/components/schemas/ValidationError"
220 },
221 "type": "array",
222 "title": "Detail",
223 }
224 },
225 "type": "object",
226 "title": "HTTPValidationError",
227 },
228 "ValidationError": {
229 "properties": {
230 "ctx": {"title": "Context", "type": "object"},
231 "input": {"title": "Input"},
232 "loc": {
233 "items": {
234 "anyOf": [{"type": "string"}, {"type": "integer"}]
235 },
236 "type": "array",
237 "title": "Location",
238 },
239 "msg": {"type": "string", "title": "Message"},
240 "type": {"type": "string", "title": "Error Type"},
241 },
242 "type": "object",
243 "required": ["loc", "msg", "type"],
244 "title": "ValidationError",
245 },
246 }
247 },
248 }
249 )