Coverage for tests / test_tutorial / test_header_params / test_tutorial002.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("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.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, {"strange_header": None}),
28 ("/items", {"X-Header": "notvalid"}, 200, {"strange_header": None}),
29 (
30 "/items",
31 {"strange_header": "FastAPI test"},
32 200,
33 {"strange_header": "FastAPI test"},
34 ),
35 (
36 "/items",
37 {"strange-header": "Not really underscore"},
38 200,
39 {"strange_header": None},
40 ),
41 ],
42)
43def test(path, headers, expected_status, expected_response, client: TestClient): 1abdc
44 response = client.get(path, headers=headers) 1efg
45 assert response.status_code == expected_status 1efg
46 assert response.json() == expected_response 1efg
49def test_openapi_schema(client: TestClient): 1abdc
50 response = client.get("/openapi.json") 1hij
51 assert response.status_code == 200 1hij
52 assert response.json() == snapshot( 1hij
53 {
54 "openapi": "3.1.0",
55 "info": {"title": "FastAPI", "version": "0.1.0"},
56 "paths": {
57 "/items/": {
58 "get": {
59 "responses": {
60 "200": {
61 "description": "Successful Response",
62 "content": {"application/json": {"schema": {}}},
63 },
64 "422": {
65 "description": "Validation Error",
66 "content": {
67 "application/json": {
68 "schema": {
69 "$ref": "#/components/schemas/HTTPValidationError"
70 }
71 }
72 },
73 },
74 },
75 "summary": "Read Items",
76 "operationId": "read_items_items__get",
77 "parameters": [
78 {
79 "required": False,
80 "schema": {
81 "anyOf": [{"type": "string"}, {"type": "null"}],
82 "title": "Strange Header",
83 },
84 "name": "strange_header",
85 "in": "header",
86 }
87 ],
88 }
89 }
90 },
91 "components": {
92 "schemas": {
93 "ValidationError": {
94 "title": "ValidationError",
95 "required": ["loc", "msg", "type"],
96 "type": "object",
97 "properties": {
98 "loc": {
99 "title": "Location",
100 "type": "array",
101 "items": {
102 "anyOf": [{"type": "string"}, {"type": "integer"}]
103 },
104 },
105 "msg": {"title": "Message", "type": "string"},
106 "type": {"title": "Error Type", "type": "string"},
107 "input": {"title": "Input"},
108 "ctx": {"title": "Context", "type": "object"},
109 },
110 },
111 "HTTPValidationError": {
112 "title": "HTTPValidationError",
113 "type": "object",
114 "properties": {
115 "detail": {
116 "title": "Detail",
117 "type": "array",
118 "items": {
119 "$ref": "#/components/schemas/ValidationError"
120 },
121 }
122 },
123 },
124 }
125 },
126 }
127 )