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