1
from fastapi import FastAPI, HTTPException
2
from fastapi.testclient import TestClient
3
from starlette.exceptions import HTTPException as StarletteHTTPException
7
items = {"foo": "The Foo Wrestlers"}
10
@app.get("/items/{item_id}")
11
async def read_item(item_id: str):
12
if item_id not in items:
15
detail="Item not found",
16
headers={"X-Error": "Some custom header"},
18
return {"item": items[item_id]}
21
@app.get("/http-no-body-statuscode-exception")
22
async def no_body_status_code_exception():
23
raise HTTPException(status_code=204)
26
@app.get("/http-no-body-statuscode-with-detail-exception")
27
async def no_body_status_code_with_detail_exception():
28
raise HTTPException(status_code=204, detail="I should just disappear!")
31
@app.get("/starlette-items/{item_id}")
32
async def read_starlette_item(item_id: str):
33
if item_id not in items:
34
raise StarletteHTTPException(status_code=404, detail="Item not found")
35
return {"item": items[item_id]}
38
client = TestClient(app)
42
response = client.get("/items/foo")
43
assert response.status_code == 200, response.text
44
assert response.json() == {"item": "The Foo Wrestlers"}
47
def test_get_item_not_found():
48
response = client.get("/items/bar")
49
assert response.status_code == 404, response.text
50
assert response.headers.get("x-error") == "Some custom header"
51
assert response.json() == {"detail": "Item not found"}
54
def test_get_starlette_item():
55
response = client.get("/starlette-items/foo")
56
assert response.status_code == 200, response.text
57
assert response.json() == {"item": "The Foo Wrestlers"}
60
def test_get_starlette_item_not_found():
61
response = client.get("/starlette-items/bar")
62
assert response.status_code == 404, response.text
63
assert response.headers.get("x-error") is None
64
assert response.json() == {"detail": "Item not found"}
67
def test_no_body_status_code_exception_handlers():
68
response = client.get("/http-no-body-statuscode-exception")
69
assert response.status_code == 204
70
assert not response.content
73
def test_no_body_status_code_with_detail_exception_handlers():
74
response = client.get("/http-no-body-statuscode-with-detail-exception")
75
assert response.status_code == 204
76
assert not response.content
79
def test_openapi_schema():
80
response = client.get("/openapi.json")
81
assert response.status_code == 200, response.text
82
assert response.json() == {
84
"info": {"title": "FastAPI", "version": "0.1.0"},
86
"/http-no-body-statuscode-exception": {
88
"operationId": "no_body_status_code_exception_http_no_body_statuscode_exception_get",
91
"content": {"application/json": {"schema": {}}},
92
"description": "Successful Response",
95
"summary": "No Body Status Code Exception",
98
"/http-no-body-statuscode-with-detail-exception": {
100
"operationId": "no_body_status_code_with_detail_exception_http_no_body_statuscode_with_detail_exception_get",
103
"content": {"application/json": {"schema": {}}},
104
"description": "Successful Response",
107
"summary": "No Body Status Code With Detail Exception",
110
"/items/{item_id}": {
114
"description": "Successful Response",
115
"content": {"application/json": {"schema": {}}},
118
"description": "Validation Error",
120
"application/json": {
122
"$ref": "#/components/schemas/HTTPValidationError"
128
"summary": "Read Item",
129
"operationId": "read_item_items__item_id__get",
133
"schema": {"title": "Item Id", "type": "string"},
140
"/starlette-items/{item_id}": {
144
"description": "Successful Response",
145
"content": {"application/json": {"schema": {}}},
148
"description": "Validation Error",
150
"application/json": {
152
"$ref": "#/components/schemas/HTTPValidationError"
158
"summary": "Read Starlette Item",
159
"operationId": "read_starlette_item_starlette_items__item_id__get",
163
"schema": {"title": "Item Id", "type": "string"},
174
"title": "ValidationError",
175
"required": ["loc", "msg", "type"],
182
"anyOf": [{"type": "string"}, {"type": "integer"}]
185
"msg": {"title": "Message", "type": "string"},
186
"type": {"title": "Error Type", "type": "string"},
189
"HTTPValidationError": {
190
"title": "HTTPValidationError",
196
"items": {"$ref": "#/components/schemas/ValidationError"},