From 6641dfd667ae6dc82dc430c9b6bf2f106adc7ba0 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Tue, 1 Aug 2023 10:14:36 +0200 Subject: [PATCH 1/3] Fix GraphQL integration swallowing response --- sentry_sdk/integrations/stdlib.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sentry_sdk/integrations/stdlib.py b/sentry_sdk/integrations/stdlib.py index 43049a06a7..f8ed16d9b8 100644 --- a/sentry_sdk/integrations/stdlib.py +++ b/sentry_sdk/integrations/stdlib.py @@ -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 From 51373d0e27338d37b6dd7d981e5bde70f82acc7b Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Tue, 1 Aug 2023 12:32:55 +0200 Subject: [PATCH 2/3] add test --- tests/integrations/requests/test_requests.py | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/integrations/requests/test_requests.py b/tests/integrations/requests/test_requests.py index aecf64762d..da454f93ac 100644 --- a/tests/integrations/requests/test_requests.py +++ b/tests/integrations/requests/test_requests.py @@ -1,3 +1,4 @@ +import json import pytest import responses @@ -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()]) @@ -62,3 +67,19 @@ 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): + sentry_init(integrations=[StdlibIntegration()]) + + 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 response.json() == msg From 6dc46c4020a98162086072ad6054e5dc9a96c1be Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Tue, 1 Aug 2023 12:34:14 +0200 Subject: [PATCH 3/3] update test --- tests/integrations/requests/test_requests.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/integrations/requests/test_requests.py b/tests/integrations/requests/test_requests.py index da454f93ac..c4c15e9a8d 100644 --- a/tests/integrations/requests/test_requests.py +++ b/tests/integrations/requests/test_requests.py @@ -69,9 +69,11 @@ def test_omit_url_data_if_parsing_fails(sentry_init, capture_events): } -def test_graphql_integration_doesnt_affect_responses(sentry_init): +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 @@ -82,4 +84,5 @@ def do_POST(self): # noqa: N802 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