@@ -211,9 +211,12 @@ def test_falcon_large_json_request(sentry_init, capture_events):
211211
212212 data = {"foo" : {"bar" : "a" * 2000 }}
213213
214+ assert_passed = False
215+
214216 class Resource :
215217 def on_post (self , req , resp ):
216- assert req .media == data
218+ nonlocal assert_passed
219+ assert_passed = req .media == data
217220 sentry_sdk .capture_message ("hi" )
218221 resp .media = "ok"
219222
@@ -225,7 +228,7 @@ def on_post(self, req, resp):
225228 client = falcon .testing .TestClient (app )
226229 response = client .simulate_post ("/" , json = data )
227230 assert response .status == falcon .HTTP_200
228-
231+ assert assert_passed
229232 (event ,) = events
230233 assert event ["_meta" ]["request" ]["data" ]["foo" ]["bar" ] == {
231234 "" : {"len" : 2000 , "rem" : [["!limit" , "x" , 1021 , 1024 ]]}
@@ -460,3 +463,48 @@ def test_span_origin(sentry_init, capture_events, make_client):
460463 (_ , event ) = events
461464
462465 assert event ["contexts" ]["trace" ]["origin" ] == "auto.http.falcon"
466+
467+
468+ def test_falcon_request_media (sentry_init ):
469+ # test_passed stores whether the test has passed.
470+ test_passed = False
471+
472+ # test_failure_reason stores the reason why the test failed
473+ # if test_passed is False. The value is meaningless when
474+ # test_passed is True.
475+ test_failure_reason = "test endpoint did not get called"
476+
477+ class SentryCaptureMiddleware :
478+ def process_request (self , _req , _resp ):
479+ # This capture message forces Falcon event processors to run
480+ # before the request handler runs
481+ sentry_sdk .capture_message ("Processing request" )
482+
483+ class RequestMediaResource :
484+ def on_post (self , req , _ ):
485+ nonlocal test_passed , test_failure_reason
486+ raw_data = req .bounded_stream .read ()
487+
488+ # If the raw_data is empty, the request body stream
489+ # has been exhausted by the SDK. Test should fail in
490+ # this case.
491+ test_passed = raw_data != b""
492+ test_failure_reason = "request body has been read"
493+
494+ sentry_init (integrations = [FalconIntegration ()])
495+
496+ try :
497+ app_class = falcon .App # Falcon ≥3.0
498+ except AttributeError :
499+ app_class = falcon .API # Falcon <3.0
500+
501+ app = app_class (middleware = [SentryCaptureMiddleware ()])
502+ app .add_route ("/read_body" , RequestMediaResource ())
503+
504+ client = falcon .testing .TestClient (app )
505+
506+ client .simulate_post ("/read_body" , json = {"foo" : "bar" })
507+
508+ # Check that simulate_post actually calls the resource, and
509+ # that the SDK does not exhaust the request body stream.
510+ assert test_passed , test_failure_reason
0 commit comments