Coverage for tests/test_tutorial/test_header_params/test_tutorial001.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_py310 1abcdef
10@pytest.fixture( 1abcdef
11 name="client",
12 params=[
13 "tutorial001",
14 pytest.param("tutorial001_py310", marks=needs_py310),
15 "tutorial001_an",
16 pytest.param("tutorial001_an_py310", marks=needs_py310),
17 ],
18)
19def get_client(request: pytest.FixtureRequest): 1abcdef
20 mod = importlib.import_module(f"docs_src.header_params.{request.param}") 1abcdef
22 client = TestClient(mod.app) 1abcdef
23 return client 1abcdef
26@pytest.mark.parametrize( 1abcdef
27 "path,headers,expected_status,expected_response",
28 [
29 ("/items", None, 200, {"User-Agent": "testclient"}),
30 ("/items", {"X-Header": "notvalid"}, 200, {"User-Agent": "testclient"}),
31 ("/items", {"User-Agent": "FastAPI test"}, 200, {"User-Agent": "FastAPI test"}),
32 ],
33)
34def test(path, headers, expected_status, expected_response, client: TestClient): 1abcdef
35 response = client.get(path, headers=headers) 1ghijkl
36 assert response.status_code == expected_status 1ghijkl
37 assert response.json() == expected_response 1ghijkl
40def test_openapi_schema(client: TestClient): 1abcdef
41 response = client.get("/openapi.json") 1mnopqr
42 assert response.status_code == 200 1mnopqr
43 assert response.json() == { 1mnopqr
44 "openapi": "3.1.0",
45 "info": {"title": "FastAPI", "version": "0.1.0"},
46 "paths": {
47 "/items/": {
48 "get": {
49 "responses": {
50 "200": {
51 "description": "Successful Response",
52 "content": {"application/json": {"schema": {}}},
53 },
54 "422": {
55 "description": "Validation Error",
56 "content": {
57 "application/json": {
58 "schema": {
59 "$ref": "#/components/schemas/HTTPValidationError"
60 }
61 }
62 },
63 },
64 },
65 "summary": "Read Items",
66 "operationId": "read_items_items__get",
67 "parameters": [
68 {
69 "required": False,
70 "schema": IsDict(
71 {
72 "anyOf": [{"type": "string"}, {"type": "null"}],
73 "title": "User-Agent",
74 }
75 )
76 | IsDict(
77 # TODO: remove when deprecating Pydantic v1
78 {"title": "User-Agent", "type": "string"}
79 ),
80 "name": "user-agent",
81 "in": "header",
82 }
83 ],
84 }
85 }
86 },
87 "components": {
88 "schemas": {
89 "ValidationError": {
90 "title": "ValidationError",
91 "required": ["loc", "msg", "type"],
92 "type": "object",
93 "properties": {
94 "loc": {
95 "title": "Location",
96 "type": "array",
97 "items": {
98 "anyOf": [{"type": "string"}, {"type": "integer"}]
99 },
100 },
101 "msg": {"title": "Message", "type": "string"},
102 "type": {"title": "Error Type", "type": "string"},
103 },
104 },
105 "HTTPValidationError": {
106 "title": "HTTPValidationError",
107 "type": "object",
108 "properties": {
109 "detail": {
110 "title": "Detail",
111 "type": "array",
112 "items": {"$ref": "#/components/schemas/ValidationError"},
113 }
114 },
115 },
116 }
117 },
118 }