Coverage for tests / test_tutorial / test_cookie_param_models / test_tutorial002.py: 100%
39 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import importlib 1abdc
3import pytest 1abdc
4from fastapi.testclient import TestClient 1abdc
5from inline_snapshot import snapshot 1abdc
7from tests.utils import needs_py310 1abdc
10@pytest.fixture( 1abdc
11 name="client",
12 params=[
13 pytest.param("tutorial002_py310", marks=[needs_py310]),
14 pytest.param("tutorial002_an_py310", marks=[needs_py310]),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abdc
18 mod = importlib.import_module(f"docs_src.cookie_param_models.{request.param}") 1abc
20 client = TestClient(mod.app) 1abc
21 return client 1abc
24def test_cookie_param_model(client: TestClient): 1abdc
25 with client as c: 1efg
26 c.cookies.set("session_id", "123") 1efg
27 c.cookies.set("fatebook_tracker", "456") 1efg
28 c.cookies.set("googall_tracker", "789") 1efg
29 response = c.get("/items/") 1efg
30 assert response.status_code == 200 1efg
31 assert response.json() == { 1efg
32 "session_id": "123",
33 "fatebook_tracker": "456",
34 "googall_tracker": "789",
35 }
38def test_cookie_param_model_defaults(client: TestClient): 1abdc
39 with client as c: 1klm
40 c.cookies.set("session_id", "123") 1klm
41 response = c.get("/items/") 1klm
42 assert response.status_code == 200 1klm
43 assert response.json() == { 1klm
44 "session_id": "123",
45 "fatebook_tracker": None,
46 "googall_tracker": None,
47 }
50def test_cookie_param_model_invalid(client: TestClient): 1abdc
51 response = client.get("/items/") 1nop
52 assert response.status_code == 422 1nop
53 assert response.json() == { 1nop
54 "detail": [
55 {
56 "type": "missing",
57 "loc": ["cookie", "session_id"],
58 "msg": "Field required",
59 "input": {},
60 }
61 ]
62 }
65def test_cookie_param_model_extra(client: TestClient): 1abdc
66 with client as c: 1hij
67 c.cookies.set("session_id", "123") 1hij
68 c.cookies.set("extra", "track-me-here-too") 1hij
69 response = c.get("/items/") 1hij
70 assert response.status_code == 422 1hij
71 assert response.json() == snapshot( 1hij
72 {
73 "detail": [
74 {
75 "type": "extra_forbidden",
76 "loc": ["cookie", "extra"],
77 "msg": "Extra inputs are not permitted",
78 "input": "track-me-here-too",
79 }
80 ]
81 }
82 )
85def test_openapi_schema(client: TestClient): 1abdc
86 response = client.get("/openapi.json") 1qrs
87 assert response.status_code == 200, response.text 1qrs
88 assert response.json() == snapshot( 1qrs
89 {
90 "openapi": "3.1.0",
91 "info": {"title": "FastAPI", "version": "0.1.0"},
92 "paths": {
93 "/items/": {
94 "get": {
95 "summary": "Read Items",
96 "operationId": "read_items_items__get",
97 "parameters": [
98 {
99 "name": "session_id",
100 "in": "cookie",
101 "required": True,
102 "schema": {"type": "string", "title": "Session Id"},
103 },
104 {
105 "name": "fatebook_tracker",
106 "in": "cookie",
107 "required": False,
108 "schema": {
109 "anyOf": [
110 {"type": "string"},
111 {"type": "null"},
112 ],
113 "title": "Fatebook Tracker",
114 },
115 },
116 {
117 "name": "googall_tracker",
118 "in": "cookie",
119 "required": False,
120 "schema": {
121 "anyOf": [{"type": "string"}, {"type": "null"}],
122 "title": "Googall Tracker",
123 },
124 },
125 ],
126 "responses": {
127 "200": {
128 "description": "Successful Response",
129 "content": {"application/json": {"schema": {}}},
130 },
131 "422": {
132 "description": "Validation Error",
133 "content": {
134 "application/json": {
135 "schema": {
136 "$ref": "#/components/schemas/HTTPValidationError"
137 }
138 }
139 },
140 },
141 },
142 }
143 }
144 },
145 "components": {
146 "schemas": {
147 "HTTPValidationError": {
148 "properties": {
149 "detail": {
150 "items": {
151 "$ref": "#/components/schemas/ValidationError"
152 },
153 "type": "array",
154 "title": "Detail",
155 }
156 },
157 "type": "object",
158 "title": "HTTPValidationError",
159 },
160 "ValidationError": {
161 "properties": {
162 "ctx": {"title": "Context", "type": "object"},
163 "input": {"title": "Input"},
164 "loc": {
165 "items": {
166 "anyOf": [{"type": "string"}, {"type": "integer"}]
167 },
168 "type": "array",
169 "title": "Location",
170 },
171 "msg": {"type": "string", "title": "Message"},
172 "type": {"type": "string", "title": "Error Type"},
173 },
174 "type": "object",
175 "required": ["loc", "msg", "type"],
176 "title": "ValidationError",
177 },
178 }
179 },
180 }
181 )