@@ -173,25 +173,91 @@ def test_post_body(path, body, expected_status, expected_response):
173
173
174
174
175
175
def test_post_broken_body ():
176
- response = client .post ("/items/" , data = {"name" : "Foo" , "price" : 50.5 })
176
+ response = client .post (
177
+ "/items/" ,
178
+ headers = {"content-type" : "application/json" },
179
+ data = "{some broken json}" ,
180
+ )
177
181
assert response .status_code == 422 , response .text
178
182
assert response .json () == {
179
183
"detail" : [
180
184
{
185
+ "loc" : ["body" , 1 ],
186
+ "msg" : "Expecting property name enclosed in double quotes: line 1 column 2 (char 1)" ,
187
+ "type" : "value_error.jsondecode" ,
181
188
"ctx" : {
182
- "colno" : 1 ,
183
- "doc" : "name=Foo&price=50.5" ,
189
+ "msg" : "Expecting property name enclosed in double quotes" ,
190
+ "doc" : "{some broken json}" ,
191
+ "pos" : 1 ,
184
192
"lineno" : 1 ,
185
- "msg" : "Expecting value" ,
186
- "pos" : 0 ,
193
+ "colno" : 2 ,
187
194
},
188
- "loc" : ["body" , 0 ],
189
- "msg" : "Expecting value: line 1 column 1 (char 0)" ,
190
- "type" : "value_error.jsondecode" ,
191
195
}
192
196
]
193
197
}
198
+
199
+
200
+ def test_post_form_for_json ():
201
+ response = client .post ("/items/" , data = {"name" : "Foo" , "price" : 50.5 })
202
+ assert response .status_code == 422 , response .text
203
+ assert response .json () == {
204
+ "detail" : [
205
+ {
206
+ "loc" : ["body" ],
207
+ "msg" : "value is not a valid dict" ,
208
+ "type" : "type_error.dict" ,
209
+ }
210
+ ]
211
+ }
212
+
213
+
214
+ def test_explicit_content_type ():
215
+ response = client .post (
216
+ "/items/" ,
217
+ data = '{"name": "Foo", "price": 50.5}' ,
218
+ headers = {"Content-Type" : "application/json" },
219
+ )
220
+ assert response .status_code == 200 , response .text
221
+
222
+
223
+ def test_geo_json ():
224
+ response = client .post (
225
+ "/items/" ,
226
+ data = '{"name": "Foo", "price": 50.5}' ,
227
+ headers = {"Content-Type" : "application/geo+json" },
228
+ )
229
+ assert response .status_code == 200 , response .text
230
+
231
+
232
+ def test_wrong_headers ():
233
+ data = '{"name": "Foo", "price": 50.5}'
234
+ invalid_dict = {
235
+ "detail" : [
236
+ {
237
+ "loc" : ["body" ],
238
+ "msg" : "value is not a valid dict" ,
239
+ "type" : "type_error.dict" ,
240
+ }
241
+ ]
242
+ }
243
+
244
+ response = client .post ("/items/" , data = data , headers = {"Content-Type" : "text/plain" })
245
+ assert response .status_code == 422 , response .text
246
+ assert response .json () == invalid_dict
247
+
248
+ response = client .post (
249
+ "/items/" , data = data , headers = {"Content-Type" : "application/geo+json-seq" }
250
+ )
251
+ assert response .status_code == 422 , response .text
252
+ assert response .json () == invalid_dict
253
+ response = client .post (
254
+ "/items/" , data = data , headers = {"Content-Type" : "application/not-really-json" }
255
+ )
256
+ assert response .status_code == 422 , response .text
257
+ assert response .json () == invalid_dict
258
+
259
+
260
+ def test_other_exceptions ():
194
261
with patch ("json.loads" , side_effect = Exception ):
195
262
response = client .post ("/items/" , json = {"test" : "test2" })
196
263
assert response .status_code == 400 , response .text
197
- assert response .json () == {"detail" : "There was an error parsing the body" }
0 commit comments