Skip to content

Fix GraphQL integration swallowing responses #2286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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