Debian Codegraph @

tests/test_tutorial/test_header_param_models/test_tutorial001.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("tutorial001_py310", marks=needs_py310),
15 pytest.param("tutorial001_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_defaults(client: TestClient):
47 response = client.get("/items/", headers=[("save-data", "true")])
48 assert response.status_code == 200
49 assert response.json() == {
50 "host": "testserver",
51 "save_data": True,
52 "if_modified_since": None,
53 "traceparent": None,
54 "x_tag": [],
55 }
56
57
58 def test_header_param_model_invalid(client: TestClient):
59 response = client.get("/items/")
60 assert response.status_code == 422
61 assert response.json() == snapshot(
62 {
63 "detail": [
64 {
65 "type": "missing",
66 "loc": ["header", "save_data"],
67 "msg": "Field required",
68 "input": {
69 "x_tag": [],
70 "host": "testserver",
71 "accept": "*/*",
72 "accept-encoding": IsOneOf(
73 "gzip, deflate", "gzip, deflate, zstd"
74 ),
75 "connection": "keep-alive",
76 "user-agent": "testclient",
77 },
78 }
79 ]
80 }
81 )
82
83
84 def test_header_param_model_extra(client: TestClient):
85 response = client.get(
86 "/items/", headers=[("save-data", "true"), ("tool", "plumbus")]
87 )
88 assert response.status_code == 200, response.text
89 assert response.json() == snapshot(
90 {
91 "host": "testserver",
92 "save_data": True,
93 "if_modified_since": None,
94 "traceparent": None,
95 "x_tag": [],
96 }
97 )
98
99
100 def test_openapi_schema(client: TestClient):
101 response = client.get("/openapi.json")
102 assert response.status_code == 200, response.text
103 assert response.json() == snapshot(
104 {
105 "openapi": "3.1.0",
106 "info": {"title": "FastAPI", "version": "0.1.0"},
107 "paths": {
108 "/items/": {
109 "get": {
110 "summary": "Read Items",
111 "operationId": "read_items_items__get",
112 "parameters": [
113 {
114 "name": "host",
115 "in": "header",
116 "required": True,
117 "schema": {"type": "string", "title": "Host"},
118 },
119 {
120 "name": "save-data",
121 "in": "header",
122 "required": True,
123 "schema": {"type": "boolean", "title": "Save Data"},
124 },
125 {
126 "name": "if-modified-since",
127 "in": "header",
128 "required": False,
129 "schema": {
130 "anyOf": [{"type": "string"}, {"type": "null"}],
131 "title": "If Modified Since",
132 },
133 },
134 {
135 "name": "traceparent",
136 "in": "header",
137 "required": False,
138 "schema": {
139 "anyOf": [{"type": "string"}, {"type": "null"}],
140 "title": "Traceparent",
141 },
142 },
143 {
144 "name": "x-tag",
145 "in": "header",
146 "required": False,
147 "schema": {
148 "type": "array",
149 "items": {"type": "string"},
150 "default": [],
151 "title": "X Tag",
152 },
153 },
154 ],
155 "responses": {
156 "200": {
157 "description": "Successful Response",
158 "content": {"application/json": {"schema": {}}},
159 },
160 "422": {
161 "description": "Validation Error",
162 "content": {
163 "application/json": {
164 "schema": {
165 "$ref": "#/components/schemas/HTTPValidationError"
166 }
167 }
168 },
169 },
170 },
171 }
172 }
173 },
174 "components": {
175 "schemas": {
176 "HTTPValidationError": {
177 "properties": {
178 "detail": {
179 "items": {
180 "$ref": "#/components/schemas/ValidationError"
181 },
182 "type": "array",
183 "title": "Detail",
184 }
185 },
186 "type": "object",
187 "title": "HTTPValidationError",
188 },
189 "ValidationError": {
190 "properties": {
191 "ctx": {"title": "Context", "type": "object"},
192 "input": {"title": "Input"},
193 "loc": {
194 "items": {
195 "anyOf": [{"type": "string"}, {"type": "integer"}]
196 },
197 "type": "array",
198 "title": "Location",
199 },
200 "msg": {"type": "string", "title": "Message"},
201 "type": {"type": "string", "title": "Error Type"},
202 },
203 "type": "object",
204 "required": ["loc", "msg", "type"],
205 "title": "ValidationError",
206 },
207 }
208 },
209 }
210 )