Debian Codegraph @

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