Debian Codegraph @

tests/test_tutorial/test_header_params/test_tutorial002.py

raw
1 import importlib
2
3 import pytest
4 from fastapi.testclient import TestClient
5 from inline_snapshot import snapshot
6
7 from ...utils import needs_py310
8
9
10 @pytest.fixture(
11 name="client",
12 params=[
13 pytest.param("tutorial002_py310", marks=needs_py310),
14 pytest.param("tutorial002_an_py310", marks=needs_py310),
15 ],
16 )
17 def get_client(request: pytest.FixtureRequest):
18 mod = importlib.import_module(f"docs_src.header_params.{request.param}")
19
20 client = TestClient(mod.app)
21 return client
22
23
24 @pytest.mark.parametrize(
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 )
43 def test(path, headers, expected_status, expected_response, client: TestClient):
44 response = client.get(path, headers=headers)
45 assert response.status_code == expected_status
46 assert response.json() == expected_response
47
48
49 def test_openapi_schema(client: TestClient):
50 response = client.get("/openapi.json")
51 assert response.status_code == 200
52 assert response.json() == snapshot(
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 )