Skip to content

Commit 02aaabd

Browse files
committed
.
1 parent 0ff06c6 commit 02aaabd

File tree

6 files changed

+164
-1
lines changed

6 files changed

+164
-1
lines changed

tests/integrations/aiohttp/test_aiohttp.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,36 @@ async def hello(request):
123123
assert request["data"] == json.dumps(body)
124124

125125

126+
@pytest.mark.asyncio
127+
async def test_request_body_not_included_when_disabled(
128+
sentry_init, aiohttp_client, capture_events
129+
):
130+
"""Test that request body is not captured when include_request_bodies=False"""
131+
sentry_init(
132+
integrations=[AioHttpIntegration()],
133+
include_request_bodies=False,
134+
send_default_pii=True,
135+
)
136+
137+
async def hello(request):
138+
await request.read() # this populates request._read_bytes so that we can actually test include_request_bodies
139+
1 / 0
140+
141+
app = web.Application()
142+
app.router.add_post("/", hello)
143+
144+
events = capture_events()
145+
146+
client = await aiohttp_client(app)
147+
resp = await client.post("/", data={"key": "value"})
148+
assert resp.status == 500
149+
150+
assert len(events) == 1
151+
event = events[0]
152+
153+
assert not event["request"]["data"]
154+
155+
126156
@pytest.mark.asyncio
127157
async def test_403_not_captured(sentry_init, aiohttp_client, capture_events):
128158
sentry_init(integrations=[AioHttpIntegration()])

tests/integrations/bottle/test_bottle.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def index():
255255
def test_json_not_truncated(sentry_init, capture_events, app, get_client):
256256
sentry_init(integrations=[BottleIntegration()])
257257

258-
data = {"key{}".format(i): "value{}".format(i) for i in range(10**4)}
258+
data = {"key{}".format(i): "value{}".format(i) for i in range(1000)}
259259

260260
@app.route("/", method="POST")
261261
def index():
@@ -277,6 +277,32 @@ def index():
277277
assert event["request"]["data"] == data
278278

279279

280+
def test_request_body_not_included_when_disabled(
281+
sentry_init, capture_events, app, get_client
282+
):
283+
"""Test that request body is not captured when include_request_bodies=False"""
284+
sentry_init(
285+
integrations=[BottleIntegration()],
286+
include_request_bodies=False,
287+
send_default_pii=True,
288+
)
289+
290+
@app.route("/", method="POST")
291+
def index():
292+
1 / 0
293+
294+
events = capture_events()
295+
296+
client = get_client()
297+
with pytest.raises(ZeroDivisionError):
298+
client.post("/", json={"key": "value"})
299+
300+
assert len(events) == 1
301+
event = events[0]
302+
303+
assert not event["request"]["data"]
304+
305+
280306
@pytest.mark.parametrize(
281307
"integrations",
282308
[

tests/integrations/django/test_basic.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,30 @@ def raw_data(self):
809809
assert "data" not in event["request"]
810810

811811

812+
def test_request_body_not_included_when_disabled(sentry_init, client, capture_events):
813+
"""Test that request body is not captured when include_request_bodies=False"""
814+
sentry_init(
815+
integrations=[DjangoIntegration()],
816+
include_request_bodies=False,
817+
send_default_pii=True,
818+
)
819+
events = capture_events()
820+
821+
content, status, headers = unpack_werkzeug_response(
822+
client.post(
823+
reverse("post_echo"),
824+
data=b'{"key": "value"}',
825+
content_type="application/json",
826+
)
827+
)
828+
assert status.lower() == "200 ok"
829+
830+
(event,) = events
831+
832+
assert event["message"] == "hi"
833+
assert not event["request"]["data"]
834+
835+
812836
def test_template_tracing_meta(sentry_init, client, capture_events):
813837
sentry_init(integrations=[DjangoIntegration()])
814838
events = capture_events()

tests/integrations/fastapi/test_fastapi.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,33 @@ async def _error(request: Request):
248248
assert event["request"]["headers"]["authorization"] == "[Filtered]"
249249

250250

251+
@pytest.mark.asyncio
252+
async def test_request_body_not_included_when_disabled(sentry_init, capture_events):
253+
"""Test that request body is not captured when include_request_bodies=False"""
254+
sentry_init(
255+
integrations=[StarletteIntegration(), FastApiIntegration()],
256+
include_request_bodies=False,
257+
send_default_pii=True,
258+
)
259+
260+
app = FastAPI()
261+
262+
@app.post("/test")
263+
async def _test():
264+
capture_message("Oh no!")
265+
return "ok"
266+
267+
events = capture_events()
268+
269+
client = TestClient(app)
270+
client.post("/test", json={"key": "value"})
271+
272+
assert len(events) == 1
273+
(event,) = events
274+
275+
assert not event["request"]["data"]
276+
277+
251278
def test_response_status_code_ok_in_transaction_context(sentry_init, capture_envelopes):
252279
"""
253280
Tests that the response status code is added to the transaction "response" context.

tests/integrations/flask/test_flask.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,32 @@ def index():
477477
assert event["request"]["data"] == data
478478

479479

480+
def test_flask_request_body_not_included_when_disabled(
481+
sentry_init, capture_events, app
482+
):
483+
"""Test that request body is not captured when include_request_bodies=False"""
484+
sentry_init(
485+
integrations=[flask_sentry.FlaskIntegration()],
486+
include_request_bodies=False,
487+
send_default_pii=True,
488+
)
489+
490+
@app.route("/", methods=["POST"])
491+
def index():
492+
capture_message("test message")
493+
return "ok"
494+
495+
events = capture_events()
496+
497+
client = app.test_client()
498+
response = client.post("/", content_type="application/json", data={"key": "value"})
499+
assert response.status_code == 200
500+
501+
(event,) = events
502+
503+
assert not event["request"]["data"]
504+
505+
480506
@pytest.mark.parametrize(
481507
"integrations",
482508
[

tests/integrations/tornado/test_tornado.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,36 @@ def post(self):
297297
assert event["request"]["data"] == {"foo": {"bar": 42}}
298298

299299

300+
def test_request_body_not_included_when_disabled(
301+
tornado_testcase, sentry_init, capture_events
302+
):
303+
"""Test that request body is not captured when include_request_bodies=False"""
304+
sentry_init(
305+
integrations=[TornadoIntegration()],
306+
include_request_bodies=False,
307+
send_default_pii=True,
308+
)
309+
events = capture_events()
310+
311+
class TestHandler(RequestHandler):
312+
def post(self):
313+
raise ValueError("test error")
314+
315+
client = tornado_testcase(Application([(r"/test", TestHandler)]))
316+
317+
response = client.fetch(
318+
"/test",
319+
method="POST",
320+
headers={"Content-Type": "application/json"},
321+
body=b'{"key": "value"}',
322+
)
323+
324+
assert response.code == 500
325+
(event,) = events
326+
327+
assert not event["request"]["data"]
328+
329+
300330
def test_error_has_new_trace_context_performance_enabled(
301331
tornado_testcase, sentry_init, capture_events
302332
):

0 commit comments

Comments
 (0)