| 1 |
import importlib |
| 2 |
|
| 3 |
import pytest |
| 4 |
from fastapi.testclient import TestClient |
| 5 |
from inline_snapshot import snapshot |
| 6 |
|
| 7 |
|
| 8 |
@pytest.fixture( |
| 9 |
name="client", |
| 10 |
params=[ |
| 11 |
pytest.param("tutorial006_py310"), |
| 12 |
pytest.param("tutorial006_an_py310"), |
| 13 |
], |
| 14 |
) |
| 15 |
def get_client(request: pytest.FixtureRequest): |
| 16 |
mod = importlib.import_module(f"docs_src.dependencies.{request.param}") |
| 17 |
|
| 18 |
client = TestClient(mod.app) |
| 19 |
return client |
| 20 |
|
| 21 |
|
| 22 |
def test_get_no_headers(client: TestClient): |
| 23 |
response = client.get("/items/") |
| 24 |
assert response.status_code == 422, response.text |
| 25 |
assert response.json() == { |
| 26 |
"detail": [ |
| 27 |
{ |
| 28 |
"type": "missing", |
| 29 |
"loc": ["header", "x-token"], |
| 30 |
"msg": "Field required", |
| 31 |
"input": None, |
| 32 |
}, |
| 33 |
{ |
| 34 |
"type": "missing", |
| 35 |
"loc": ["header", "x-key"], |
| 36 |
"msg": "Field required", |
| 37 |
"input": None, |
| 38 |
}, |
| 39 |
] |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
def test_get_invalid_one_header(client: TestClient): |
| 44 |
response = client.get("/items/", headers={"X-Token": "invalid"}) |
| 45 |
assert response.status_code == 400, response.text |
| 46 |
assert response.json() == {"detail": "X-Token header invalid"} |
| 47 |
|
| 48 |
|
| 49 |
def test_get_invalid_second_header(client: TestClient): |
| 50 |
response = client.get( |
| 51 |
"/items/", headers={"X-Token": "fake-super-secret-token", "X-Key": "invalid"} |
| 52 |
) |
| 53 |
assert response.status_code == 400, response.text |
| 54 |
assert response.json() == {"detail": "X-Key header invalid"} |
| 55 |
|
| 56 |
|
| 57 |
def test_get_valid_headers(client: TestClient): |
| 58 |
response = client.get( |
| 59 |
"/items/", |
| 60 |
headers={ |
| 61 |
"X-Token": "fake-super-secret-token", |
| 62 |
"X-Key": "fake-super-secret-key", |
| 63 |
}, |
| 64 |
) |
| 65 |
assert response.status_code == 200, response.text |
| 66 |
assert response.json() == [{"item": "Foo"}, {"item": "Bar"}] |
| 67 |
|
| 68 |
|
| 69 |
def test_openapi_schema(client: TestClient): |
| 70 |
response = client.get("/openapi.json") |
| 71 |
assert response.status_code == 200, response.text |
| 72 |
assert response.json() == snapshot( |
| 73 |
{ |
| 74 |
"openapi": "3.1.0", |
| 75 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 76 |
"paths": { |
| 77 |
"/items/": { |
| 78 |
"get": { |
| 79 |
"responses": { |
| 80 |
"200": { |
| 81 |
"description": "Successful Response", |
| 82 |
"content": {"application/json": {"schema": {}}}, |
| 83 |
}, |
| 84 |
"422": { |
| 85 |
"description": "Validation Error", |
| 86 |
"content": { |
| 87 |
"application/json": { |
| 88 |
"schema": { |
| 89 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 90 |
} |
| 91 |
} |
| 92 |
}, |
| 93 |
}, |
| 94 |
}, |
| 95 |
"summary": "Read Items", |
| 96 |
"operationId": "read_items_items__get", |
| 97 |
"parameters": [ |
| 98 |
{ |
| 99 |
"required": True, |
| 100 |
"schema": {"title": "X-Token", "type": "string"}, |
| 101 |
"name": "x-token", |
| 102 |
"in": "header", |
| 103 |
}, |
| 104 |
{ |
| 105 |
"required": True, |
| 106 |
"schema": {"title": "X-Key", "type": "string"}, |
| 107 |
"name": "x-key", |
| 108 |
"in": "header", |
| 109 |
}, |
| 110 |
], |
| 111 |
} |
| 112 |
} |
| 113 |
}, |
| 114 |
"components": { |
| 115 |
"schemas": { |
| 116 |
"ValidationError": { |
| 117 |
"title": "ValidationError", |
| 118 |
"required": ["loc", "msg", "type"], |
| 119 |
"type": "object", |
| 120 |
"properties": { |
| 121 |
"loc": { |
| 122 |
"title": "Location", |
| 123 |
"type": "array", |
| 124 |
"items": { |
| 125 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 126 |
}, |
| 127 |
}, |
| 128 |
"msg": {"title": "Message", "type": "string"}, |
| 129 |
"type": {"title": "Error Type", "type": "string"}, |
| 130 |
"input": {"title": "Input"}, |
| 131 |
"ctx": {"title": "Context", "type": "object"}, |
| 132 |
}, |
| 133 |
}, |
| 134 |
"HTTPValidationError": { |
| 135 |
"title": "HTTPValidationError", |
| 136 |
"type": "object", |
| 137 |
"properties": { |
| 138 |
"detail": { |
| 139 |
"title": "Detail", |
| 140 |
"type": "array", |
| 141 |
"items": { |
| 142 |
"$ref": "#/components/schemas/ValidationError" |
| 143 |
}, |
| 144 |
} |
| 145 |
}, |
| 146 |
}, |
| 147 |
} |
| 148 |
}, |
| 149 |
} |
| 150 |
) |