Skip to content

Commit e918504

Browse files
authored
Fix GraphQL integration swallowing responses (#2286)
1 parent 5dfc991 commit e918504

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

sentry_sdk/integrations/stdlib.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ def getresponse(self, *args, **kwargs):
185185
response_data = rv.read()
186186
# once we've read() the body it can't be read() again by the
187187
# app; save it so that it can be accessed again
188-
rv.read = io.BytesIO(response_data).read
188+
saved_response = io.BytesIO(response_data)
189+
rv.read = saved_response.read
190+
rv.fp = saved_response
189191
try:
190192
# py3.6+ json.loads() can deal with bytes out of the box, but
191193
# for older version we have to explicitly decode first

tests/integrations/requests/test_requests.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import pytest
23
import responses
34

@@ -7,11 +8,15 @@
78
from sentry_sdk.consts import SPANDATA
89
from sentry_sdk.integrations.stdlib import StdlibIntegration
910

11+
from tests.conftest import MockServerRequestHandler, create_mock_http_server
12+
1013
try:
1114
from unittest import mock # python 3.3 and above
1215
except ImportError:
1316
import mock # python < 3.3
1417

18+
PORT = create_mock_http_server()
19+
1520

1621
def test_crumb_capture(sentry_init, capture_events):
1722
sentry_init(integrations=[StdlibIntegration()])
@@ -62,3 +67,22 @@ def test_omit_url_data_if_parsing_fails(sentry_init, capture_events):
6267
"reason": response.reason,
6368
# no url related data
6469
}
70+
71+
72+
def test_graphql_integration_doesnt_affect_responses(sentry_init, capture_events):
73+
sentry_init(integrations=[StdlibIntegration()])
74+
75+
events = capture_events()
76+
77+
msg = {"errors": [{"message": "some message"}]}
78+
79+
def do_POST(self): # noqa: N802
80+
self.send_response(200)
81+
self.end_headers()
82+
self.wfile.write(json.dumps(msg).encode())
83+
84+
with mock.patch.object(MockServerRequestHandler, "do_POST", do_POST):
85+
response = requests.post("http://localhost:{}".format(PORT) + "/graphql")
86+
87+
assert len(events) == 1
88+
assert response.json() == msg

0 commit comments

Comments
 (0)