1
+ import base64
1
2
import json
3
+ import os
2
4
3
5
import django
4
6
import pytest
@@ -370,16 +372,105 @@ async def test_trace_from_headers_if_performance_disabled(sentry_init, capture_e
370
372
assert error_event ["contexts" ]["trace" ]["trace_id" ] == trace_id
371
373
372
374
375
+ PICTURE = os .path .join (os .path .dirname (os .path .abspath (__file__ )), "image.png" )
376
+ BODY_FORM = """--fd721ef49ea403a6\r \n Content-Disposition: form-data; name="username"\r \n \r \n Jane\r \n --fd721ef49ea403a6\r \n Content-Disposition: form-data; name="password"\r \n \r \n hello123\r \n --fd721ef49ea403a6\r \n Content-Disposition: form-data; name="photo"; filename="image.png"\r \n Content-Type: image/png\r \n Content-Transfer-Encoding: base64\r \n \r \n {{image_data}}\r \n --fd721ef49ea403a6--\r \n """ .replace (
377
+ "{{image_data}}" , base64 .b64encode (open (PICTURE , "rb" ).read ()).decode ("utf-8" )
378
+ ).encode (
379
+ "utf-8"
380
+ )
381
+ BODY_FORM_CONTENT_LENGTH = str (len (BODY_FORM )).encode ("utf-8" )
382
+
383
+
373
384
@pytest .mark .parametrize ("application" , APPS )
374
385
@pytest .mark .parametrize (
375
- "body,expected_return_data " ,
386
+ "send_default_pii,method,headers,url_name, body,expected_data " ,
376
387
[
377
388
(
389
+ True ,
390
+ "POST" ,
391
+ [(b"content-type" , b"text/plain" )],
392
+ "post_echo_async" ,
393
+ b"" ,
394
+ None ,
395
+ ),
396
+ (
397
+ True ,
398
+ "POST" ,
399
+ [(b"content-type" , b"text/plain" )],
400
+ "post_echo_async" ,
401
+ b"some raw text body" ,
402
+ "" ,
403
+ ),
404
+ (
405
+ True ,
406
+ "POST" ,
407
+ [(b"content-type" , b"application/json" )],
408
+ "post_echo_async" ,
378
409
b'{"username":"xyz","password":"xyz"}' ,
379
410
{"username" : "xyz" , "password" : "xyz" },
380
411
),
381
- (b"hello" , "" ),
382
- (b"" , None ),
412
+ (
413
+ True ,
414
+ "POST" ,
415
+ [(b"content-type" , b"application/xml" )],
416
+ "post_echo_async" ,
417
+ b'<?xml version="1.0" encoding="UTF-8"?><root></root>' ,
418
+ "" ,
419
+ ),
420
+ (
421
+ True ,
422
+ "POST" ,
423
+ [
424
+ (b"content-type" , b"multipart/form-data; boundary=fd721ef49ea403a6" ),
425
+ (b"content-length" , BODY_FORM_CONTENT_LENGTH ),
426
+ ],
427
+ "post_echo_async" ,
428
+ BODY_FORM ,
429
+ {"password" : "hello123" , "photo" : "" , "username" : "Jane" },
430
+ ),
431
+ (
432
+ False ,
433
+ "POST" ,
434
+ [(b"content-type" , b"text/plain" )],
435
+ "post_echo_async" ,
436
+ b"" ,
437
+ None ,
438
+ ),
439
+ (
440
+ False ,
441
+ "POST" ,
442
+ [(b"content-type" , b"text/plain" )],
443
+ "post_echo_async" ,
444
+ b"some raw text body" ,
445
+ "" ,
446
+ ),
447
+ (
448
+ False ,
449
+ "POST" ,
450
+ [(b"content-type" , b"application/json" )],
451
+ "post_echo_async" ,
452
+ b'{"username":"xyz","password":"xyz"}' ,
453
+ {"username" : "xyz" , "password" : "[Filtered]" },
454
+ ),
455
+ (
456
+ False ,
457
+ "POST" ,
458
+ [(b"content-type" , b"application/xml" )],
459
+ "post_echo_async" ,
460
+ b'<?xml version="1.0" encoding="UTF-8"?><root></root>' ,
461
+ "" ,
462
+ ),
463
+ (
464
+ False ,
465
+ "POST" ,
466
+ [
467
+ (b"content-type" , b"multipart/form-data; boundary=fd721ef49ea403a6" ),
468
+ (b"content-length" , BODY_FORM_CONTENT_LENGTH ),
469
+ ],
470
+ "post_echo_async" ,
471
+ BODY_FORM ,
472
+ {"password" : "[Filtered]" , "photo" : "" , "username" : "Jane" },
473
+ ),
383
474
],
384
475
)
385
476
@pytest .mark .asyncio
@@ -388,28 +479,42 @@ async def test_trace_from_headers_if_performance_disabled(sentry_init, capture_e
388
479
django .VERSION < (3 , 1 ), reason = "async views have been introduced in Django 3.1"
389
480
)
390
481
async def test_asgi_request_body (
391
- sentry_init , capture_envelopes , application , body , expected_return_data
482
+ sentry_init ,
483
+ capture_envelopes ,
484
+ application ,
485
+ send_default_pii ,
486
+ method ,
487
+ headers ,
488
+ url_name ,
489
+ body ,
490
+ expected_data ,
392
491
):
393
- sentry_init (integrations = [DjangoIntegration ()], send_default_pii = True )
492
+ sentry_init (
493
+ send_default_pii = send_default_pii ,
494
+ integrations = [
495
+ DjangoIntegration (),
496
+ ],
497
+ )
394
498
395
499
envelopes = capture_envelopes ()
396
500
397
501
comm = HttpCommunicator (
398
502
application ,
399
- method = "POST" ,
400
- path = reverse ("post_echo_async" ),
503
+ method = method ,
504
+ headers = headers ,
505
+ path = reverse (url_name ),
401
506
body = body ,
402
- headers = [(b"content-type" , b"application/json" )],
403
507
)
404
508
response = await comm .get_response ()
405
-
406
509
assert response ["status" ] == 200
510
+
511
+ await comm .wait ()
407
512
assert response ["body" ] == body
408
513
409
514
(envelope ,) = envelopes
410
515
event = envelope .get_event ()
411
516
412
- if expected_return_data is not None :
413
- assert event ["request" ]["data" ] == expected_return_data
517
+ if expected_data is not None :
518
+ assert event ["request" ]["data" ] == expected_data
414
519
else :
415
520
assert "data" not in event ["request" ]
0 commit comments