Debian Codegraph @

tests/test_tutorial/test_header_params/test_tutorial001.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("tutorial001_py310", marks=needs_py310),
14 pytest.param("tutorial001_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, {"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 )
32 def test(path, headers, expected_status, expected_response, client: TestClient):
33 response = client.get(path, headers=headers)
34 assert response.status_code == expected_status
35 assert response.json() == expected_response
36
37
38 def test_openapi_schema(client: TestClient):
39 response = client.get("/openapi.json")
40 assert response.status_code == 200
41 assert response.json() == snapshot(
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 )