From 360bf57341fd6ede6cb2fa51a05669a3bb249209 Mon Sep 17 00:00:00 2001 From: Erica Pisani Date: Fri, 14 Aug 2026 17:28:28 -0400 Subject: [PATCH] test(sanic): Add tests for data_collection gating of request body Refs PY-2419 Refs #6283 --- tests/integrations/sanic/test_sanic.py | 80 ++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/tests/integrations/sanic/test_sanic.py b/tests/integrations/sanic/test_sanic.py index 0bc6133744..03e68a82e1 100644 --- a/tests/integrations/sanic/test_sanic.py +++ b/tests/integrations/sanic/test_sanic.py @@ -794,3 +794,83 @@ def test_url_query_data_collection_event_processor( assert "query_string" not in event["request"] else: assert event["request"]["query_string"] == expected_query + + +@pytest.mark.parametrize( + "data_collection, expect_body", + [ + pytest.param({}, True, id="data_collection_http_bodies_default"), + pytest.param( + {"http_bodies": ["incoming_request"]}, + True, + id="data_collection_http_bodies_incoming_request", + ), + pytest.param( + {"http_bodies": ["outgoing_request"]}, + False, + id="data_collection_http_bodies_outgoing_request_only", + ), + pytest.param( + {"http_bodies": []}, False, id="data_collection_http_bodies_empty" + ), + ], +) +def test_request_body_data_collection_event_processor( + sentry_init, app, capture_events, data_collection, expect_body +): + sentry_init( + integrations=[SanicIntegration()], + _experiments={"data_collection": data_collection}, + ) + + data = {"hey": 42} + + @app.route("/body", methods=["POST"]) + def body_handler(request): + capture_message("hi") + return response.text("ok") + + events = capture_events() + + c = get_client(app) + with c as client: + _, http_response = client.post("/body", json=data) + assert http_response.status == 200 + + (event,) = events + + if expect_body: + assert event["request"]["data"] == data + else: + assert "data" not in event["request"] + + +def test_oversized_request_body_not_annotated_data_collection( + sentry_init, app, capture_events +): + """ + The gating happens before the size check, so an oversized body is dropped + outright instead of being reported as removed because of the size limit. + """ + sentry_init( + integrations=[SanicIntegration()], + max_request_body_size="small", + _experiments={"data_collection": {"http_bodies": []}}, + ) + + @app.route("/oversized", methods=["POST"]) + def oversized_handler(request): + capture_message("hi") + return response.text("ok") + + events = capture_events() + + c = get_client(app) + with c as client: + _, http_response = client.post("/oversized", data="a" * 2000) + assert http_response.status == 200 + + (event,) = events + + assert "data" not in event["request"] + assert "data" not in event.get("_meta", {}).get("request", {})