44from typing import Any , Dict
55
66import pytest
7- from starlite import AbstractMiddleware , Controller , LoggingConfig , Starlite , get
7+ from starlite import AbstractMiddleware , Controller , LoggingConfig , Starlite , get , post
88from starlite .middleware import LoggingMiddlewareConfig , RateLimitConfig
99from starlite .middleware .session .memory_backend import MemoryBackendConfig
1010from starlite .testing import TestClient
@@ -43,6 +43,11 @@ async def message_with_id() -> "Dict[str, Any]":
4343 capture_message ("hi" )
4444 return {"status" : "ok" }
4545
46+ @post ("/body/json" )
47+ async def body_json (data : Dict [str , Any ]) -> Dict [str , Any ]:
48+ capture_message ("hi" )
49+ return {"status" : "ok" }
50+
4651 logging_config = LoggingConfig ()
4752
4853 app = Starlite (
@@ -51,6 +56,7 @@ async def message_with_id() -> "Dict[str, Any]":
5156 custom_error ,
5257 message ,
5358 message_with_id ,
59+ body_json ,
5460 MyController ,
5561 ],
5662 debug = debug ,
@@ -559,6 +565,72 @@ async def __call__(self, scope, receive, send):
559565COOKIE_HEADER = "jwt=tokenval; theme=dark; lang=en; identity=alice"
560566
561567
568+ @pytest .mark .parametrize (
569+ "data_collection, expect_body" ,
570+ [
571+ pytest .param (None , True , id = "no_data_collection_experiment" ),
572+ pytest .param ({}, True , id = "data_collection_http_bodies_default" ),
573+ pytest .param (
574+ {"http_bodies" : ["incoming_request" ]},
575+ True ,
576+ id = "data_collection_http_bodies_incoming_request" ,
577+ ),
578+ pytest .param (
579+ {"http_bodies" : []}, False , id = "data_collection_http_bodies_empty"
580+ ),
581+ ],
582+ )
583+ def test_request_body_data_collection (
584+ sentry_init , capture_events , data_collection , expect_body
585+ ):
586+ sentry_init (
587+ traces_sample_rate = 1.0 ,
588+ integrations = [StarliteIntegration ()],
589+ _experiments = (
590+ {} if data_collection is None else {"data_collection" : data_collection }
591+ ),
592+ )
593+
594+ starlite_app = starlite_app_factory ()
595+ events = capture_events ()
596+
597+ body = {"foo" : {"bar" : "baz" , "qux" : ["1" , "2" , "3" ]}}
598+
599+ client = TestClient (starlite_app )
600+ client .post ("/body/json" , json = body )
601+
602+ (event , transaction_event ) = events
603+
604+ if expect_body :
605+ assert event ["request" ]["data" ] == body
606+ assert transaction_event ["request" ]["data" ] == body
607+ else :
608+ assert "data" not in event ["request" ]
609+ assert "data" not in transaction_event ["request" ]
610+
611+
612+ def test_request_body_data_collection_wins_over_send_default_pii (
613+ sentry_init , capture_events
614+ ):
615+ sentry_init (
616+ traces_sample_rate = 1.0 ,
617+ integrations = [StarliteIntegration ()],
618+ send_default_pii = True ,
619+ _experiments = {"data_collection" : {"http_bodies" : []}},
620+ )
621+
622+ starlite_app = starlite_app_factory ()
623+ events = capture_events ()
624+
625+ client = TestClient (starlite_app )
626+ client .post ("/body/json" , json = {"foo" : {"bar" : "baz" , "qux" : ["1" , "2" , "3" ]}})
627+
628+ (event , transaction_event ) = events
629+
630+ assert "data" not in event ["request" ]
631+ assert "data" not in transaction_event ["request" ]
632+
633+
562634@pytest .mark .parametrize (
563635 "init_kwargs, expected_cookies" ,
564636 [
0 commit comments