Coverage for tests/test_tutorial/test_header_params/test_tutorial002.py: 100%
19 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1import importlib 1abcdef
3import pytest 1abcdef
4from dirty_equals import IsDict 1abcdef
5from fastapi.testclient import TestClient 1abcdef
7from ...utils import needs_py39, needs_py310 1abcdef
10@pytest.fixture( 1abcdef
11 name="client",
12 params=[
13 "tutorial002",
14 pytest.param("tutorial002_py310", marks=needs_py310),
15 "tutorial002_an",
16 pytest.param("tutorial002_an_py39", marks=needs_py39),
17 pytest.param("tutorial002_an_py310", marks=needs_py310),
18 ],
19)
20def get_client(request: pytest.FixtureRequest): 1abcdef
21 mod = importlib.import_module(f"docs_src.header_params.{request.param}") 1abcdef
23 client = TestClient(mod.app) 1abcdef
24 return client 1abcdef
27@pytest.mark.parametrize( 1abcdef
28 "path,headers,expected_status,expected_response",
29 [
30 ("/items", None, 200, {"strange_header": None}),
31 ("/items", {"X-Header": "notvalid"}, 200, {"strange_header": None}),
32 (
33 "/items",
34 {"strange_header": "FastAPI test"},
35 200,
36 {"strange_header": "FastAPI test"},
37 ),
38 (
39 "/items",
40 {"strange-header": "Not really underscore"},
41 200,
42 {"strange_header": None},
43 ),
44 ],
45)
46def test(path, headers, expected_status, expected_response, client: TestClient): 1abcdef
47 response = client.get(path, headers=headers) 1ghijkl
48 assert response.status_code == expected_status 1ghijkl
49 assert response.json() == expected_response 1ghijkl
52def test_openapi_schema(client: TestClient): 1abcdef
53 response = client.get("/openapi.json") 1mnopqr
54 assert response.status_code == 200 1mnopqr
55 assert response.json() == { 1mnopqr
56 "openapi": "3.1.0",
57 "info": {"title": "FastAPI", "version": "0.1.0"},
58 "paths": {
59 "/items/": {
60 "get": {
61 "responses": {
62 "200": {
63 "description": "Successful Response",
64 "content": {"application/json": {"schema": {}}},
65 },
66 "422": {
67 "description": "Validation Error",
68 "content": {
69 "application/json": {
70 "schema": {
71 "$ref": "#/components/schemas/HTTPValidationError"
72 }
73 }
74 },
75 },
76 },
77 "summary": "Read Items",
78 "operationId": "read_items_items__get",
79 "parameters": [
80 {
81 "required": False,
82 "schema": IsDict(
83 {
84 "anyOf": [{"type": "string"}, {"type": "null"}],
85 "title": "Strange Header",
86 }
87 )
88 | IsDict(
89 # TODO: remove when deprecating Pydantic v1
90 {"title": "Strange Header", "type": "string"}
91 ),
92 "name": "strange_header",
93 "in": "header",
94 }
95 ],
96 }
97 }
98 },
99 "components": {
100 "schemas": {
101 "ValidationError": {
102 "title": "ValidationError",
103 "required": ["loc", "msg", "type"],
104 "type": "object",
105 "properties": {
106 "loc": {
107 "title": "Location",
108 "type": "array",
109 "items": {
110 "anyOf": [{"type": "string"}, {"type": "integer"}]
111 },
112 },
113 "msg": {"title": "Message", "type": "string"},
114 "type": {"title": "Error Type", "type": "string"},
115 },
116 },
117 "HTTPValidationError": {
118 "title": "HTTPValidationError",
119 "type": "object",
120 "properties": {
121 "detail": {
122 "title": "Detail",
123 "type": "array",
124 "items": {"$ref": "#/components/schemas/ValidationError"},
125 }
126 },
127 },
128 }
129 },
130 }