| 1 |
from fastapi import Depends, FastAPI, Query |
| 2 |
from fastapi.testclient import TestClient |
| 3 |
from inline_snapshot import snapshot |
| 4 |
|
| 5 |
app = FastAPI() |
| 6 |
|
| 7 |
|
| 8 |
def _get_client_key(client_id: str = Query(...)) -> str: |
| 9 |
return f"{client_id}_key" |
| 10 |
|
| 11 |
|
| 12 |
def _get_client_tag(client_id: str | None = Query(None)) -> str | None: |
| 13 |
if client_id is None: |
| 14 |
return None |
| 15 |
return f"{client_id}_tag" |
| 16 |
|
| 17 |
|
| 18 |
@app.get("/foo") |
| 19 |
def foo_handler( |
| 20 |
client_key: str = Depends(_get_client_key), |
| 21 |
client_tag: str | None = Depends(_get_client_tag), |
| 22 |
): |
| 23 |
return {"client_id": client_key, "client_tag": client_tag} |
| 24 |
|
| 25 |
|
| 26 |
client = TestClient(app) |
| 27 |
|
| 28 |
|
| 29 |
def test_get_invalid(): |
| 30 |
response = client.get("/foo") |
| 31 |
assert response.status_code == 422 |
| 32 |
|
| 33 |
|
| 34 |
def test_get_valid(): |
| 35 |
response = client.get("/foo", params={"client_id": "bar"}) |
| 36 |
assert response.status_code == 200 |
| 37 |
assert response.json() == {"client_id": "bar_key", "client_tag": "bar_tag"} |
| 38 |
|
| 39 |
|
| 40 |
def test_openapi_schema(): |
| 41 |
response = client.get("/openapi.json") |
| 42 |
assert response.status_code == 200, response.text |
| 43 |
assert response.json() == snapshot( |
| 44 |
{ |
| 45 |
"components": { |
| 46 |
"schemas": { |
| 47 |
"HTTPValidationError": { |
| 48 |
"properties": { |
| 49 |
"detail": { |
| 50 |
"items": { |
| 51 |
"$ref": "#/components/schemas/ValidationError" |
| 52 |
}, |
| 53 |
"title": "Detail", |
| 54 |
"type": "array", |
| 55 |
} |
| 56 |
}, |
| 57 |
"title": "HTTPValidationError", |
| 58 |
"type": "object", |
| 59 |
}, |
| 60 |
"ValidationError": { |
| 61 |
"properties": { |
| 62 |
"ctx": {"title": "Context", "type": "object"}, |
| 63 |
"input": {"title": "Input"}, |
| 64 |
"loc": { |
| 65 |
"items": { |
| 66 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 67 |
}, |
| 68 |
"title": "Location", |
| 69 |
"type": "array", |
| 70 |
}, |
| 71 |
"msg": {"title": "Message", "type": "string"}, |
| 72 |
"type": {"title": "Error Type", "type": "string"}, |
| 73 |
}, |
| 74 |
"required": ["loc", "msg", "type"], |
| 75 |
"title": "ValidationError", |
| 76 |
"type": "object", |
| 77 |
}, |
| 78 |
} |
| 79 |
}, |
| 80 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 81 |
"openapi": "3.1.0", |
| 82 |
"paths": { |
| 83 |
"/foo": { |
| 84 |
"get": { |
| 85 |
"operationId": "foo_handler_foo_get", |
| 86 |
"parameters": [ |
| 87 |
{ |
| 88 |
"in": "query", |
| 89 |
"name": "client_id", |
| 90 |
"required": True, |
| 91 |
"schema": {"title": "Client Id", "type": "string"}, |
| 92 |
}, |
| 93 |
], |
| 94 |
"responses": { |
| 95 |
"200": { |
| 96 |
"content": {"application/json": {"schema": {}}}, |
| 97 |
"description": "Successful Response", |
| 98 |
}, |
| 99 |
"422": { |
| 100 |
"content": { |
| 101 |
"application/json": { |
| 102 |
"schema": { |
| 103 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 104 |
} |
| 105 |
} |
| 106 |
}, |
| 107 |
"description": "Validation Error", |
| 108 |
}, |
| 109 |
}, |
| 110 |
"summary": "Foo Handler", |
| 111 |
} |
| 112 |
} |
| 113 |
}, |
| 114 |
} |
| 115 |
) |