Coverage for tests/test_starlette_exception.py: 100%
52 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from fastapi import FastAPI, HTTPException 1abcde
2from fastapi.testclient import TestClient 1abcde
3from starlette.exceptions import HTTPException as StarletteHTTPException 1abcde
5app = FastAPI() 1abcde
7items = {"foo": "The Foo Wrestlers"} 1abcde
10@app.get("/items/{item_id}") 1abcde
11async def read_item(item_id: str): 1abcde
12 if item_id not in items: 1abcde
13 raise HTTPException( 1abcde
14 status_code=404,
15 detail="Item not found",
16 headers={"X-Error": "Some custom header"},
17 )
18 return {"item": items[item_id]} 1abcde
21@app.get("/http-no-body-statuscode-exception") 1abcde
22async def no_body_status_code_exception(): 1abcde
23 raise HTTPException(status_code=204) 1abcde
26@app.get("/http-no-body-statuscode-with-detail-exception") 1abcde
27async def no_body_status_code_with_detail_exception(): 1abcde
28 raise HTTPException(status_code=204, detail="I should just disappear!") 1abcde
31@app.get("/starlette-items/{item_id}") 1abcde
32async def read_starlette_item(item_id: str): 1abcde
33 if item_id not in items: 1abcde
34 raise StarletteHTTPException(status_code=404, detail="Item not found") 1abcde
35 return {"item": items[item_id]} 1abcde
38client = TestClient(app) 1abcde
41def test_get_item(): 1abcde
42 response = client.get("/items/foo") 1abcde
43 assert response.status_code == 200, response.text 1abcde
44 assert response.json() == {"item": "The Foo Wrestlers"} 1abcde
47def test_get_item_not_found(): 1abcde
48 response = client.get("/items/bar") 1abcde
49 assert response.status_code == 404, response.text 1abcde
50 assert response.headers.get("x-error") == "Some custom header" 1abcde
51 assert response.json() == {"detail": "Item not found"} 1abcde
54def test_get_starlette_item(): 1abcde
55 response = client.get("/starlette-items/foo") 1abcde
56 assert response.status_code == 200, response.text 1abcde
57 assert response.json() == {"item": "The Foo Wrestlers"} 1abcde
60def test_get_starlette_item_not_found(): 1abcde
61 response = client.get("/starlette-items/bar") 1abcde
62 assert response.status_code == 404, response.text 1abcde
63 assert response.headers.get("x-error") is None 1abcde
64 assert response.json() == {"detail": "Item not found"} 1abcde
67def test_no_body_status_code_exception_handlers(): 1abcde
68 response = client.get("/http-no-body-statuscode-exception") 1abcde
69 assert response.status_code == 204 1abcde
70 assert not response.content 1abcde
73def test_no_body_status_code_with_detail_exception_handlers(): 1abcde
74 response = client.get("/http-no-body-statuscode-with-detail-exception") 1abcde
75 assert response.status_code == 204 1abcde
76 assert not response.content 1abcde
79def test_openapi_schema(): 1abcde
80 response = client.get("/openapi.json") 1abcde
81 assert response.status_code == 200, response.text 1abcde
82 assert response.json() == { 1abcde
83 "openapi": "3.1.0",
84 "info": {"title": "FastAPI", "version": "0.1.0"},
85 "paths": {
86 "/http-no-body-statuscode-exception": {
87 "get": {
88 "operationId": "no_body_status_code_exception_http_no_body_statuscode_exception_get",
89 "responses": {
90 "200": {
91 "content": {"application/json": {"schema": {}}},
92 "description": "Successful Response",
93 }
94 },
95 "summary": "No Body Status Code Exception",
96 }
97 },
98 "/http-no-body-statuscode-with-detail-exception": {
99 "get": {
100 "operationId": "no_body_status_code_with_detail_exception_http_no_body_statuscode_with_detail_exception_get",
101 "responses": {
102 "200": {
103 "content": {"application/json": {"schema": {}}},
104 "description": "Successful Response",
105 }
106 },
107 "summary": "No Body Status Code With Detail Exception",
108 }
109 },
110 "/items/{item_id}": {
111 "get": {
112 "responses": {
113 "200": {
114 "description": "Successful Response",
115 "content": {"application/json": {"schema": {}}},
116 },
117 "422": {
118 "description": "Validation Error",
119 "content": {
120 "application/json": {
121 "schema": {
122 "$ref": "#/components/schemas/HTTPValidationError"
123 }
124 }
125 },
126 },
127 },
128 "summary": "Read Item",
129 "operationId": "read_item_items__item_id__get",
130 "parameters": [
131 {
132 "required": True,
133 "schema": {"title": "Item Id", "type": "string"},
134 "name": "item_id",
135 "in": "path",
136 }
137 ],
138 }
139 },
140 "/starlette-items/{item_id}": {
141 "get": {
142 "responses": {
143 "200": {
144 "description": "Successful Response",
145 "content": {"application/json": {"schema": {}}},
146 },
147 "422": {
148 "description": "Validation Error",
149 "content": {
150 "application/json": {
151 "schema": {
152 "$ref": "#/components/schemas/HTTPValidationError"
153 }
154 }
155 },
156 },
157 },
158 "summary": "Read Starlette Item",
159 "operationId": "read_starlette_item_starlette_items__item_id__get",
160 "parameters": [
161 {
162 "required": True,
163 "schema": {"title": "Item Id", "type": "string"},
164 "name": "item_id",
165 "in": "path",
166 }
167 ],
168 }
169 },
170 },
171 "components": {
172 "schemas": {
173 "ValidationError": {
174 "title": "ValidationError",
175 "required": ["loc", "msg", "type"],
176 "type": "object",
177 "properties": {
178 "loc": {
179 "title": "Location",
180 "type": "array",
181 "items": {
182 "anyOf": [{"type": "string"}, {"type": "integer"}]
183 },
184 },
185 "msg": {"title": "Message", "type": "string"},
186 "type": {"title": "Error Type", "type": "string"},
187 },
188 },
189 "HTTPValidationError": {
190 "title": "HTTPValidationError",
191 "type": "object",
192 "properties": {
193 "detail": {
194 "title": "Detail",
195 "type": "array",
196 "items": {"$ref": "#/components/schemas/ValidationError"},
197 }
198 },
199 },
200 }
201 },
202 }