@@ -239,37 +239,33 @@ def webhook(
239
239
request : HttpRequest ,
240
240
* ,
241
241
body : WebhookPayload [WildValue ],
242
- foo : Json [int ],
243
- bar : Json [int ] = 0 ,
242
+ non_body : Json [int ] = 0 ,
244
243
) -> Dict [str , object ]:
245
244
status = body ["totame" ]["status" ].tame (check_bool )
246
- return {"status" : status , "foo" : foo , "bar" : bar }
247
-
248
- # Simulate a paylaod that uses JSON encoding. We use the body setter to
249
- # overwrite the request body. The HostRequestMock initializer sets the
250
- # POST QueryDict, which is normally done by Django by parsing the body.
251
- data = {"foo" : 15 , "totame" : {"status" : True }}
252
- request = HostRequestMock (data )
253
- request .body = orjson .dumps (data )
245
+ return {"status" : status , "foo" : non_body }
246
+
247
+ # A normal request that uses a JSON encoded body
248
+ request = HostRequestMock ({"non_body" : 15 , "totame" : {"status" : True }})
254
249
result = call_endpoint (webhook , request )
255
- self .assertDictEqual (result , {"status" : True , "foo" : 15 , "bar" : 0 })
250
+ self .assertDictEqual (result , {"status" : True , "foo" : 15 })
256
251
252
+ # Set the body manually so that we can pass in something unusual
253
+ request = HostRequestMock ()
257
254
request .body = orjson .dumps ([])
258
255
with self .assertRaisesRegex (DjangoValidationError , "request is not a dict" ):
259
256
result = call_endpoint (webhook , request )
260
257
261
- request .body = orjson .dumps (10 )
262
- with self .assertRaisesRegex (DjangoValidationError , "request is not a dict" ):
263
- result = call_endpoint (webhook , request )
264
-
258
+ # Test for the rare case when both body and GET are used
265
259
request = HostRequestMock ()
266
- request .GET .update ({"foo " : "15" , "bar" : "10 " })
267
- request .body = orjson .dumps (data )
260
+ request .GET .update ({"non_body " : "15" })
261
+ request .body = orjson .dumps ({ "totame" : { "status" : True }} )
268
262
result = call_endpoint (webhook , request )
269
- self .assertDictEqual (result , {"status" : True , "foo" : 15 , "bar" : 10 })
263
+ self .assertDictEqual (result , {"status" : True , "foo" : 15 })
270
264
271
265
with self .assertRaisesMessage (JsonableError , "Malformed JSON" ):
272
- call_endpoint (webhook , HostRequestMock ())
266
+ request = HostRequestMock ()
267
+ request .body = b"{malformed_json"
268
+ call_endpoint (webhook , request )
273
269
274
270
with self .assertRaisesMessage (JsonableError , "Malformed payload" ):
275
271
request = HostRequestMock ()
0 commit comments