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