| 1 |
from fastapi import APIRouter, FastAPI |
| 2 |
from fastapi.testclient import TestClient |
| 3 |
from inline_snapshot import snapshot |
| 4 |
from pydantic import BaseModel |
| 5 |
|
| 6 |
|
| 7 |
class ResponseModel(BaseModel): |
| 8 |
message: str |
| 9 |
|
| 10 |
|
| 11 |
app = FastAPI() |
| 12 |
router = APIRouter() |
| 13 |
|
| 14 |
|
| 15 |
@router.get("/a", responses={501: {"description": "Error 1"}}) |
| 16 |
async def a(): |
| 17 |
return "a" |
| 18 |
|
| 19 |
|
| 20 |
@router.get( |
| 21 |
"/b", |
| 22 |
responses={ |
| 23 |
502: {"description": "Error 2"}, |
| 24 |
"4XX": {"description": "Error with range, upper"}, |
| 25 |
}, |
| 26 |
) |
| 27 |
async def b(): |
| 28 |
return "b" |
| 29 |
|
| 30 |
|
| 31 |
@router.get( |
| 32 |
"/c", |
| 33 |
responses={ |
| 34 |
"400": {"description": "Error with str"}, |
| 35 |
"5xx": {"description": "Error with range, lower"}, |
| 36 |
"default": {"description": "A default response"}, |
| 37 |
}, |
| 38 |
) |
| 39 |
async def c(): |
| 40 |
return "c" |
| 41 |
|
| 42 |
|
| 43 |
@router.get( |
| 44 |
"/d", |
| 45 |
responses={ |
| 46 |
"400": {"description": "Error with str"}, |
| 47 |
"5XX": {"model": ResponseModel}, |
| 48 |
"default": {"model": ResponseModel}, |
| 49 |
}, |
| 50 |
) |
| 51 |
async def d(): |
| 52 |
return "d" |
| 53 |
|
| 54 |
|
| 55 |
app.include_router(router) |
| 56 |
|
| 57 |
|
| 58 |
client = TestClient(app) |
| 59 |
|
| 60 |
|
| 61 |
def test_a(): |
| 62 |
response = client.get("/a") |
| 63 |
assert response.status_code == 200, response.text |
| 64 |
assert response.json() == "a" |
| 65 |
|
| 66 |
|
| 67 |
def test_b(): |
| 68 |
response = client.get("/b") |
| 69 |
assert response.status_code == 200, response.text |
| 70 |
assert response.json() == "b" |
| 71 |
|
| 72 |
|
| 73 |
def test_c(): |
| 74 |
response = client.get("/c") |
| 75 |
assert response.status_code == 200, response.text |
| 76 |
assert response.json() == "c" |
| 77 |
|
| 78 |
|
| 79 |
def test_d(): |
| 80 |
response = client.get("/d") |
| 81 |
assert response.status_code == 200, response.text |
| 82 |
assert response.json() == "d" |
| 83 |
|
| 84 |
|
| 85 |
def test_openapi_schema(): |
| 86 |
response = client.get("/openapi.json") |
| 87 |
assert response.status_code == 200, response.text |
| 88 |
assert response.json() == snapshot( |
| 89 |
{ |
| 90 |
"openapi": "3.1.0", |
| 91 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 92 |
"paths": { |
| 93 |
"/a": { |
| 94 |
"get": { |
| 95 |
"responses": { |
| 96 |
"501": {"description": "Error 1"}, |
| 97 |
"200": { |
| 98 |
"description": "Successful Response", |
| 99 |
"content": {"application/json": {"schema": {}}}, |
| 100 |
}, |
| 101 |
}, |
| 102 |
"summary": "A", |
| 103 |
"operationId": "a_a_get", |
| 104 |
} |
| 105 |
}, |
| 106 |
"/b": { |
| 107 |
"get": { |
| 108 |
"responses": { |
| 109 |
"502": {"description": "Error 2"}, |
| 110 |
"4XX": {"description": "Error with range, upper"}, |
| 111 |
"200": { |
| 112 |
"description": "Successful Response", |
| 113 |
"content": {"application/json": {"schema": {}}}, |
| 114 |
}, |
| 115 |
}, |
| 116 |
"summary": "B", |
| 117 |
"operationId": "b_b_get", |
| 118 |
} |
| 119 |
}, |
| 120 |
"/c": { |
| 121 |
"get": { |
| 122 |
"responses": { |
| 123 |
"400": {"description": "Error with str"}, |
| 124 |
"5XX": {"description": "Error with range, lower"}, |
| 125 |
"200": { |
| 126 |
"description": "Successful Response", |
| 127 |
"content": {"application/json": {"schema": {}}}, |
| 128 |
}, |
| 129 |
"default": {"description": "A default response"}, |
| 130 |
}, |
| 131 |
"summary": "C", |
| 132 |
"operationId": "c_c_get", |
| 133 |
} |
| 134 |
}, |
| 135 |
"/d": { |
| 136 |
"get": { |
| 137 |
"responses": { |
| 138 |
"400": {"description": "Error with str"}, |
| 139 |
"5XX": { |
| 140 |
"description": "Server Error", |
| 141 |
"content": { |
| 142 |
"application/json": { |
| 143 |
"schema": { |
| 144 |
"$ref": "#/components/schemas/ResponseModel" |
| 145 |
} |
| 146 |
} |
| 147 |
}, |
| 148 |
}, |
| 149 |
"200": { |
| 150 |
"description": "Successful Response", |
| 151 |
"content": {"application/json": {"schema": {}}}, |
| 152 |
}, |
| 153 |
"default": { |
| 154 |
"description": "Default Response", |
| 155 |
"content": { |
| 156 |
"application/json": { |
| 157 |
"schema": { |
| 158 |
"$ref": "#/components/schemas/ResponseModel" |
| 159 |
} |
| 160 |
} |
| 161 |
}, |
| 162 |
}, |
| 163 |
}, |
| 164 |
"summary": "D", |
| 165 |
"operationId": "d_d_get", |
| 166 |
} |
| 167 |
}, |
| 168 |
}, |
| 169 |
"components": { |
| 170 |
"schemas": { |
| 171 |
"ResponseModel": { |
| 172 |
"title": "ResponseModel", |
| 173 |
"required": ["message"], |
| 174 |
"type": "object", |
| 175 |
"properties": { |
| 176 |
"message": {"title": "Message", "type": "string"} |
| 177 |
}, |
| 178 |
} |
| 179 |
} |
| 180 |
}, |
| 181 |
} |
| 182 |
) |