Coverage for tests/test_tutorial/test_header_params/test_tutorial003.py: 100%
14 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 1abcde
2from dirty_equals import IsDict 1abcde
3from fastapi.testclient import TestClient 1abcde
5from docs_src.header_params.tutorial003 import app 1abcde
7client = TestClient(app) 1abcde
10@pytest.mark.parametrize( 1abcde
11 "path,headers,expected_status,expected_response",
12 [
13 ("/items", None, 200, {"X-Token values": None}),
14 ("/items", {"x-token": "foo"}, 200, {"X-Token values": ["foo"]}),
15 (
16 "/items",
17 [("x-token", "foo"), ("x-token", "bar")],
18 200,
19 {"X-Token values": ["foo", "bar"]},
20 ),
21 ],
22)
23def test(path, headers, expected_status, expected_response): 1abcde
24 response = client.get(path, headers=headers) 1abcde
25 assert response.status_code == expected_status 1abcde
26 assert response.json() == expected_response 1abcde
29def test_openapi_schema(): 1abcde
30 response = client.get("/openapi.json") 1abcde
31 assert response.status_code == 200 1abcde
32 assert response.json() == { 1abcde
33 "openapi": "3.1.0",
34 "info": {"title": "FastAPI", "version": "0.1.0"},
35 "paths": {
36 "/items/": {
37 "get": {
38 "summary": "Read Items",
39 "operationId": "read_items_items__get",
40 "parameters": [
41 {
42 "required": False,
43 "schema": IsDict(
44 {
45 "title": "X-Token",
46 "anyOf": [
47 {"type": "array", "items": {"type": "string"}},
48 {"type": "null"},
49 ],
50 }
51 )
52 | IsDict(
53 # TODO: remove when deprecating Pydantic v1
54 {
55 "title": "X-Token",
56 "type": "array",
57 "items": {"type": "string"},
58 }
59 ),
60 "name": "x-token",
61 "in": "header",
62 }
63 ],
64 "responses": {
65 "200": {
66 "description": "Successful Response",
67 "content": {"application/json": {"schema": {}}},
68 },
69 "422": {
70 "description": "Validation Error",
71 "content": {
72 "application/json": {
73 "schema": {
74 "$ref": "#/components/schemas/HTTPValidationError"
75 }
76 }
77 },
78 },
79 },
80 }
81 }
82 },
83 "components": {
84 "schemas": {
85 "HTTPValidationError": {
86 "title": "HTTPValidationError",
87 "type": "object",
88 "properties": {
89 "detail": {
90 "title": "Detail",
91 "type": "array",
92 "items": {"$ref": "#/components/schemas/ValidationError"},
93 }
94 },
95 },
96 "ValidationError": {
97 "title": "ValidationError",
98 "required": ["loc", "msg", "type"],
99 "type": "object",
100 "properties": {
101 "loc": {
102 "title": "Location",
103 "type": "array",
104 "items": {
105 "anyOf": [{"type": "string"}, {"type": "integer"}]
106 },
107 },
108 "msg": {"title": "Message", "type": "string"},
109 "type": {"title": "Error Type", "type": "string"},
110 },
111 },
112 }
113 },
114 }