Coverage for tests/test_param_include_in_schema.py: 100%
46 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 Optional 1abcde
3import pytest 1abcde
4from fastapi import Cookie, FastAPI, Header, Path, Query 1abcde
5from fastapi.testclient import TestClient 1abcde
7app = FastAPI() 1abcde
10@app.get("/hidden_cookie") 1abcde
11async def hidden_cookie( 1abcde
12 hidden_cookie: Optional[str] = Cookie(default=None, include_in_schema=False),
13):
14 return {"hidden_cookie": hidden_cookie} 1abcde
17@app.get("/hidden_header") 1abcde
18async def hidden_header( 1abcde
19 hidden_header: Optional[str] = Header(default=None, include_in_schema=False),
20):
21 return {"hidden_header": hidden_header} 1abcde
24@app.get("/hidden_path/{hidden_path}") 1abcde
25async def hidden_path(hidden_path: str = Path(include_in_schema=False)): 1abcde
26 return {"hidden_path": hidden_path} 1abcde
29@app.get("/hidden_query") 1abcde
30async def hidden_query( 1abcde
31 hidden_query: Optional[str] = Query(default=None, include_in_schema=False),
32):
33 return {"hidden_query": hidden_query} 1abcde
36openapi_schema = { 1abcde
37 "openapi": "3.1.0",
38 "info": {"title": "FastAPI", "version": "0.1.0"},
39 "paths": {
40 "/hidden_cookie": {
41 "get": {
42 "summary": "Hidden Cookie",
43 "operationId": "hidden_cookie_hidden_cookie_get",
44 "responses": {
45 "200": {
46 "description": "Successful Response",
47 "content": {"application/json": {"schema": {}}},
48 },
49 "422": {
50 "description": "Validation Error",
51 "content": {
52 "application/json": {
53 "schema": {
54 "$ref": "#/components/schemas/HTTPValidationError"
55 }
56 }
57 },
58 },
59 },
60 }
61 },
62 "/hidden_header": {
63 "get": {
64 "summary": "Hidden Header",
65 "operationId": "hidden_header_hidden_header_get",
66 "responses": {
67 "200": {
68 "description": "Successful Response",
69 "content": {"application/json": {"schema": {}}},
70 },
71 "422": {
72 "description": "Validation Error",
73 "content": {
74 "application/json": {
75 "schema": {
76 "$ref": "#/components/schemas/HTTPValidationError"
77 }
78 }
79 },
80 },
81 },
82 }
83 },
84 "/hidden_path/{hidden_path}": {
85 "get": {
86 "summary": "Hidden Path",
87 "operationId": "hidden_path_hidden_path__hidden_path__get",
88 "responses": {
89 "200": {
90 "description": "Successful Response",
91 "content": {"application/json": {"schema": {}}},
92 },
93 "422": {
94 "description": "Validation Error",
95 "content": {
96 "application/json": {
97 "schema": {
98 "$ref": "#/components/schemas/HTTPValidationError"
99 }
100 }
101 },
102 },
103 },
104 }
105 },
106 "/hidden_query": {
107 "get": {
108 "summary": "Hidden Query",
109 "operationId": "hidden_query_hidden_query_get",
110 "responses": {
111 "200": {
112 "description": "Successful Response",
113 "content": {"application/json": {"schema": {}}},
114 },
115 "422": {
116 "description": "Validation Error",
117 "content": {
118 "application/json": {
119 "schema": {
120 "$ref": "#/components/schemas/HTTPValidationError"
121 }
122 }
123 },
124 },
125 },
126 }
127 },
128 },
129 "components": {
130 "schemas": {
131 "HTTPValidationError": {
132 "title": "HTTPValidationError",
133 "type": "object",
134 "properties": {
135 "detail": {
136 "title": "Detail",
137 "type": "array",
138 "items": {"$ref": "#/components/schemas/ValidationError"},
139 }
140 },
141 },
142 "ValidationError": {
143 "title": "ValidationError",
144 "required": ["loc", "msg", "type"],
145 "type": "object",
146 "properties": {
147 "loc": {
148 "title": "Location",
149 "type": "array",
150 "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
151 },
152 "msg": {"title": "Message", "type": "string"},
153 "type": {"title": "Error Type", "type": "string"},
154 },
155 },
156 }
157 },
158}
161def test_openapi_schema(): 1abcde
162 client = TestClient(app) 1abcde
163 response = client.get("/openapi.json") 1abcde
164 assert response.status_code == 200 1abcde
165 assert response.json() == openapi_schema 1abcde
168@pytest.mark.parametrize( 1abcde
169 "path,cookies,expected_status,expected_response",
170 [
171 (
172 "/hidden_cookie",
173 {},
174 200,
175 {"hidden_cookie": None},
176 ),
177 (
178 "/hidden_cookie",
179 {"hidden_cookie": "somevalue"},
180 200,
181 {"hidden_cookie": "somevalue"},
182 ),
183 ],
184)
185def test_hidden_cookie(path, cookies, expected_status, expected_response): 1abcde
186 client = TestClient(app, cookies=cookies) 1abcde
187 response = client.get(path) 1abcde
188 assert response.status_code == expected_status 1abcde
189 assert response.json() == expected_response 1abcde
192@pytest.mark.parametrize( 1abcde
193 "path,headers,expected_status,expected_response",
194 [
195 (
196 "/hidden_header",
197 {},
198 200,
199 {"hidden_header": None},
200 ),
201 (
202 "/hidden_header",
203 {"Hidden-Header": "somevalue"},
204 200,
205 {"hidden_header": "somevalue"},
206 ),
207 ],
208)
209def test_hidden_header(path, headers, expected_status, expected_response): 1abcde
210 client = TestClient(app) 1abcde
211 response = client.get(path, headers=headers) 1abcde
212 assert response.status_code == expected_status 1abcde
213 assert response.json() == expected_response 1abcde
216def test_hidden_path(): 1abcde
217 client = TestClient(app) 1abcde
218 response = client.get("/hidden_path/hidden_path") 1abcde
219 assert response.status_code == 200 1abcde
220 assert response.json() == {"hidden_path": "hidden_path"} 1abcde
223@pytest.mark.parametrize( 1abcde
224 "path,expected_status,expected_response",
225 [
226 (
227 "/hidden_query",
228 200,
229 {"hidden_query": None},
230 ),
231 (
232 "/hidden_query?hidden_query=somevalue",
233 200,
234 {"hidden_query": "somevalue"},
235 ),
236 ],
237)
238def test_hidden_query(path, expected_status, expected_response): 1abcde
239 client = TestClient(app) 1abcde
240 response = client.get(path) 1abcde
241 assert response.status_code == expected_status 1abcde
242 assert response.json() == expected_response 1abcde