Coverage for tests / test_tutorial / test_header_params / test_tutorial003.py: 100%
19 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 ...utils import needs_py310 1abdc
10@pytest.fixture( 1abdc
11 name="client",
12 params=[
13 pytest.param("tutorial003_py310", marks=needs_py310),
14 pytest.param("tutorial003_an_py310", marks=needs_py310),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abdc
18 mod = importlib.import_module(f"docs_src.header_params.{request.param}") 1abc
20 client = TestClient(mod.app) 1abc
21 return client 1abc
24@pytest.mark.parametrize( 1abdc
25 "path,headers,expected_status,expected_response",
26 [
27 ("/items", None, 200, {"X-Token values": None}),
28 ("/items", {"x-token": "foo"}, 200, {"X-Token values": ["foo"]}),
29 (
30 "/items",
31 [("x-token", "foo"), ("x-token", "bar")],
32 200,
33 {"X-Token values": ["foo", "bar"]},
34 ),
35 ],
36)
37def test(path, headers, expected_status, expected_response, client: TestClient): 1abdc
38 response = client.get(path, headers=headers) 1efg
39 assert response.status_code == expected_status 1efg
40 assert response.json() == expected_response 1efg
43def test_openapi_schema(client: TestClient): 1abdc
44 response = client.get("/openapi.json") 1hij
45 assert response.status_code == 200 1hij
46 assert response.json() == snapshot( 1hij
47 {
48 "openapi": "3.1.0",
49 "info": {"title": "FastAPI", "version": "0.1.0"},
50 "paths": {
51 "/items/": {
52 "get": {
53 "summary": "Read Items",
54 "operationId": "read_items_items__get",
55 "parameters": [
56 {
57 "required": False,
58 "schema": {
59 "title": "X-Token",
60 "anyOf": [
61 {"type": "array", "items": {"type": "string"}},
62 {"type": "null"},
63 ],
64 },
65 "name": "x-token",
66 "in": "header",
67 }
68 ],
69 "responses": {
70 "200": {
71 "description": "Successful Response",
72 "content": {"application/json": {"schema": {}}},
73 },
74 "422": {
75 "description": "Validation Error",
76 "content": {
77 "application/json": {
78 "schema": {
79 "$ref": "#/components/schemas/HTTPValidationError"
80 }
81 }
82 },
83 },
84 },
85 }
86 }
87 },
88 "components": {
89 "schemas": {
90 "HTTPValidationError": {
91 "title": "HTTPValidationError",
92 "type": "object",
93 "properties": {
94 "detail": {
95 "title": "Detail",
96 "type": "array",
97 "items": {
98 "$ref": "#/components/schemas/ValidationError"
99 },
100 }
101 },
102 },
103 "ValidationError": {
104 "title": "ValidationError",
105 "required": ["loc", "msg", "type"],
106 "type": "object",
107 "properties": {
108 "loc": {
109 "title": "Location",
110 "type": "array",
111 "items": {
112 "anyOf": [{"type": "string"}, {"type": "integer"}]
113 },
114 },
115 "msg": {"title": "Message", "type": "string"},
116 "type": {"title": "Error Type", "type": "string"},
117 "input": {"title": "Input"},
118 "ctx": {"title": "Context", "type": "object"},
119 },
120 },
121 }
122 },
123 }
124 )