3
from fastapi import FastAPI
4
from fastapi.responses import JSONResponse
5
from fastapi.testclient import TestClient
6
from pydantic import BaseModel
11
class JsonApiResponse(JSONResponse):
12
media_type = "application/vnd.api+json"
15
class Error(BaseModel):
20
class JsonApiError(BaseModel):
21
errors: typing.List[Error]
27
response_class=JsonApiResponse,
28
responses={500: {"description": "Error", "model": JsonApiError}},
34
@app.get("/b", responses={204: {"description": "No Content"}})
39
client = TestClient(app)
42
def test_get_response():
43
response = client.get("/a")
44
assert response.status_code == 204, response.text
45
assert "content-length" not in response.headers
46
assert response.content == b""
49
def test_openapi_schema():
50
response = client.get("/openapi.json")
51
assert response.status_code == 200, response.text
52
assert response.json() == {
54
"info": {"title": "FastAPI", "version": "0.1.0"},
60
"description": "Error",
62
"application/vnd.api+json": {
64
"$ref": "#/components/schemas/JsonApiError"
69
"204": {"description": "Successful Response"},
72
"operationId": "a_a_get",
78
"204": {"description": "No Content"},
80
"description": "Successful Response",
81
"content": {"application/json": {"schema": {}}},
85
"operationId": "b_b_get",
93
"required": ["status", "title"],
96
"status": {"title": "Status", "type": "string"},
97
"title": {"title": "Title", "type": "string"},
101
"title": "JsonApiError",
102
"required": ["errors"],
108
"items": {"$ref": "#/components/schemas/Error"},