|
3 | 3 | import re
|
4 | 4 | import pytest
|
5 | 5 | from functools import partial
|
| 6 | +from unittest.mock import patch |
6 | 7 |
|
7 | 8 | from werkzeug.test import Client
|
8 | 9 |
|
9 | 10 | from django import VERSION as DJANGO_VERSION
|
10 | 11 | from django.contrib.auth.models import User
|
11 | 12 | from django.core.management import execute_from_command_line
|
12 | 13 | from django.db.utils import OperationalError, ProgrammingError, DataError
|
| 14 | +from django.http.request import RawPostDataException |
13 | 15 |
|
14 | 16 | try:
|
15 | 17 | from django.urls import reverse
|
|
20 | 22 | from sentry_sdk._compat import PY310
|
21 | 23 | from sentry_sdk import capture_message, capture_exception
|
22 | 24 | from sentry_sdk.consts import SPANDATA
|
23 |
| -from sentry_sdk.integrations.django import DjangoIntegration, _set_db_data |
| 25 | +from sentry_sdk.integrations.django import ( |
| 26 | + DjangoIntegration, |
| 27 | + DjangoRequestExtractor, |
| 28 | + _set_db_data, |
| 29 | +) |
24 | 30 | from sentry_sdk.integrations.django.signals_handlers import _get_receiver_name
|
25 | 31 | from sentry_sdk.integrations.executing import ExecutingIntegration
|
26 | 32 | from sentry_sdk.tracing import Span
|
@@ -740,6 +746,26 @@ def test_read_request(sentry_init, client, capture_events):
|
740 | 746 | assert "data" not in event["request"]
|
741 | 747 |
|
742 | 748 |
|
| 749 | +def test_request_body_already_read(sentry_init, client, capture_events): |
| 750 | + sentry_init(integrations=[DjangoIntegration()]) |
| 751 | + |
| 752 | + events = capture_events() |
| 753 | + |
| 754 | + class MockExtractor(DjangoRequestExtractor): |
| 755 | + def raw_data(self): |
| 756 | + raise RawPostDataException |
| 757 | + |
| 758 | + with patch("sentry_sdk.integrations.django.DjangoRequestExtractor", MockExtractor): |
| 759 | + client.post( |
| 760 | + reverse("post_echo"), data=b'{"hey": 42}', content_type="application/json" |
| 761 | + ) |
| 762 | + |
| 763 | + (event,) = events |
| 764 | + |
| 765 | + assert event["message"] == "hi" |
| 766 | + assert "data" not in event["request"] |
| 767 | + |
| 768 | + |
743 | 769 | def test_template_tracing_meta(sentry_init, client, capture_events):
|
744 | 770 | sentry_init(integrations=[DjangoIntegration()])
|
745 | 771 | events = capture_events()
|
|
0 commit comments