Skip to content

Commit

Permalink
Fix GraphQL integration swallowing responses (#2286)
Browse files Browse the repository at this point in the history
  • Loading branch information
sentrivana authored Aug 1, 2023
1 parent 5dfc991 commit e918504
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion sentry_sdk/integrations/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ def getresponse(self, *args, **kwargs):
response_data = rv.read()
# once we've read() the body it can't be read() again by the
# app; save it so that it can be accessed again
rv.read = io.BytesIO(response_data).read
saved_response = io.BytesIO(response_data)
rv.read = saved_response.read
rv.fp = saved_response
try:
# py3.6+ json.loads() can deal with bytes out of the box, but
# for older version we have to explicitly decode first
Expand Down
24 changes: 24 additions & 0 deletions tests/integrations/requests/test_requests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import pytest
import responses

Expand All @@ -7,11 +8,15 @@
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations.stdlib import StdlibIntegration

from tests.conftest import MockServerRequestHandler, create_mock_http_server

try:
from unittest import mock # python 3.3 and above
except ImportError:
import mock # python < 3.3

PORT = create_mock_http_server()


def test_crumb_capture(sentry_init, capture_events):
sentry_init(integrations=[StdlibIntegration()])
Expand Down Expand Up @@ -62,3 +67,22 @@ def test_omit_url_data_if_parsing_fails(sentry_init, capture_events):
"reason": response.reason,
# no url related data
}


def test_graphql_integration_doesnt_affect_responses(sentry_init, capture_events):
sentry_init(integrations=[StdlibIntegration()])

events = capture_events()

msg = {"errors": [{"message": "some message"}]}

def do_POST(self): # noqa: N802
self.send_response(200)
self.end_headers()
self.wfile.write(json.dumps(msg).encode())

with mock.patch.object(MockServerRequestHandler, "do_POST", do_POST):
response = requests.post("http://localhost:{}".format(PORT) + "/graphql")

assert len(events) == 1
assert response.json() == msg

0 comments on commit e918504

Please sign in to comment.