Coverage for tests/test_starlette_exception.py: 100%

52 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1from fastapi import FastAPI, HTTPException 1abcdefg

2from fastapi.testclient import TestClient 1abcdefg

3from starlette.exceptions import HTTPException as StarletteHTTPException 1abcdefg

4 

5app = FastAPI() 1abcdefg

6 

7items = {"foo": "The Foo Wrestlers"} 1abcdefg

8 

9 

10@app.get("/items/{item_id}") 1abcdefg

11async def read_item(item_id: str): 1abcdefg

12 if item_id not in items: 1vhwixjykzlAmBn

13 raise HTTPException( 1hijklmn

14 status_code=404, 

15 detail="Item not found", 

16 headers={"X-Error": "Some custom header"}, 

17 ) 

18 return {"item": items[item_id]} 1vwxyzAB

19 

20 

21@app.get("/http-no-body-statuscode-exception") 1abcdefg

22async def no_body_status_code_exception(): 1abcdefg

23 raise HTTPException(status_code=204) 1JKLMNOP

24 

25 

26@app.get("/http-no-body-statuscode-with-detail-exception") 1abcdefg

27async def no_body_status_code_with_detail_exception(): 1abcdefg

28 raise HTTPException(status_code=204, detail="I should just disappear!") 1QRSTUVW

29 

30 

31@app.get("/starlette-items/{item_id}") 1abcdefg

32async def read_starlette_item(item_id: str): 1abcdefg

33 if item_id not in items: 1CoDpEqFrGsHtIu

34 raise StarletteHTTPException(status_code=404, detail="Item not found") 1opqrstu

35 return {"item": items[item_id]} 1CDEFGHI

36 

37 

38client = TestClient(app) 1abcdefg

39 

40 

41def test_get_item(): 1abcdefg

42 response = client.get("/items/foo") 1vwxyzAB

43 assert response.status_code == 200, response.text 1vwxyzAB

44 assert response.json() == {"item": "The Foo Wrestlers"} 1vwxyzAB

45 

46 

47def test_get_item_not_found(): 1abcdefg

48 response = client.get("/items/bar") 1hijklmn

49 assert response.status_code == 404, response.text 1hijklmn

50 assert response.headers.get("x-error") == "Some custom header" 1hijklmn

51 assert response.json() == {"detail": "Item not found"} 1hijklmn

52 

53 

54def test_get_starlette_item(): 1abcdefg

55 response = client.get("/starlette-items/foo") 1CDEFGHI

56 assert response.status_code == 200, response.text 1CDEFGHI

57 assert response.json() == {"item": "The Foo Wrestlers"} 1CDEFGHI

58 

59 

60def test_get_starlette_item_not_found(): 1abcdefg

61 response = client.get("/starlette-items/bar") 1opqrstu

62 assert response.status_code == 404, response.text 1opqrstu

63 assert response.headers.get("x-error") is None 1opqrstu

64 assert response.json() == {"detail": "Item not found"} 1opqrstu

65 

66 

67def test_no_body_status_code_exception_handlers(): 1abcdefg

68 response = client.get("/http-no-body-statuscode-exception") 1JKLMNOP

69 assert response.status_code == 204 1JKLMNOP

70 assert not response.content 1JKLMNOP

71 

72 

73def test_no_body_status_code_with_detail_exception_handlers(): 1abcdefg

74 response = client.get("/http-no-body-statuscode-with-detail-exception") 1QRSTUVW

75 assert response.status_code == 204 1QRSTUVW

76 assert not response.content 1QRSTUVW

77 

78 

79def test_openapi_schema(): 1abcdefg

80 response = client.get("/openapi.json") 1XYZ0123

81 assert response.status_code == 200, response.text 1XYZ0123

82 assert response.json() == { 1XYZ0123

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 }