Coverage for tests / test_additional_responses_router.py: 100%
42 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1from fastapi import APIRouter, FastAPI 1abcd
2from fastapi.testclient import TestClient 1abcd
3from inline_snapshot import snapshot 1abcd
4from pydantic import BaseModel 1abcd
7class ResponseModel(BaseModel): 1abcd
8 message: str 1abcd
11app = FastAPI() 1abcd
12router = APIRouter() 1abcd
15@router.get("/a", responses={501: {"description": "Error 1"}}) 1abcd
16async def a(): 1abcd
17 return "a" 1efg
20@router.get( 1abcd
21 "/b",
22 responses={
23 502: {"description": "Error 2"},
24 "4XX": {"description": "Error with range, upper"},
25 },
26)
27async def b(): 1abcd
28 return "b" 1hij
31@router.get( 1abcd
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)
39async def c(): 1abcd
40 return "c" 1klm
43@router.get( 1abcd
44 "/d",
45 responses={
46 "400": {"description": "Error with str"},
47 "5XX": {"model": ResponseModel},
48 "default": {"model": ResponseModel},
49 },
50)
51async def d(): 1abcd
52 return "d" 1nop
55app.include_router(router) 1abcd
58client = TestClient(app) 1abcd
61def test_a(): 1abcd
62 response = client.get("/a") 1efg
63 assert response.status_code == 200, response.text 1efg
64 assert response.json() == "a" 1efg
67def test_b(): 1abcd
68 response = client.get("/b") 1hij
69 assert response.status_code == 200, response.text 1hij
70 assert response.json() == "b" 1hij
73def test_c(): 1abcd
74 response = client.get("/c") 1klm
75 assert response.status_code == 200, response.text 1klm
76 assert response.json() == "c" 1klm
79def test_d(): 1abcd
80 response = client.get("/d") 1nop
81 assert response.status_code == 200, response.text 1nop
82 assert response.json() == "d" 1nop
85def test_openapi_schema(): 1abcd
86 response = client.get("/openapi.json") 1qrs
87 assert response.status_code == 200, response.text 1qrs
88 assert response.json() == snapshot( 1qrs
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 )