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