Skip to content

Commit 8ba8c21

Browse files
committed
fix: Fix tests when running outside of tox, with all integrations
1 parent 78a3a39 commit 8ba8c21

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

sentry_sdk/integrations/stdlib.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def install_httplib(self):
1717
real_getresponse = self.httplib_connection_cls.getresponse
1818

1919
def putrequest(self, method, url, *args, **kwargs):
20+
rv = real_putrequest(self, method, url, *args, **kwargs)
2021
self._sentrysdk_data_dict = data = {}
22+
2123
host = self.host
2224
port = self.port
2325
default_port = self.default_port
@@ -30,13 +32,15 @@ def putrequest(self, method, url, *args, **kwargs):
3032
port != default_port and ":%s" % port or "",
3133
url,
3234
)
35+
3336
data["url"] = real_url
3437
data["method"] = method
35-
return real_putrequest(self, method, url, *args, **kwargs)
38+
return rv
3639

3740
def getresponse(self, *args, **kwargs):
3841
rv = real_getresponse(self, *args, **kwargs)
3942
data = getattr(self, "_sentrysdk_data_dict", None) or {}
43+
4044
if "status_code" not in data:
4145
data["status_code"] = rv.status
4246
data["reason"] = rv.reason

tests/integrations/django/myapp/settings.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
"""
1212

1313
import os
14-
import sentry_sdk
15-
16-
from sentry_sdk.integrations.django import DjangoIntegration
17-
1814

1915
try:
2016
# Django >= 1.10
@@ -129,7 +125,3 @@ def process_response(self, request, response):
129125
# https://docs.djangoproject.com/en/2.0/howto/static-files/
130126

131127
STATIC_URL = "/static/"
132-
133-
sentry_sdk.api._init_on_current(
134-
integrations=[DjangoIntegration()], send_default_pii=True
135-
)

tests/integrations/django/test_basic.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@
1212
except ImportError:
1313
from django.core.urlresolvers import reverse
1414

15-
from sentry_sdk import Hub, last_event_id
15+
from sentry_sdk import last_event_id
16+
from sentry_sdk.integrations.django import DjangoIntegration
1617

1718
from tests.integrations.django.myapp.wsgi import application
1819

1920

21+
@pytest.fixture(autouse=True)
22+
def init_django_sentry(sentry_init):
23+
sentry_init(integrations=[DjangoIntegration()], send_default_pii=True)
24+
25+
2026
@pytest.fixture
21-
def client(monkeypatch_test_transport):
22-
monkeypatch_test_transport(Hub.current.client)
27+
def client():
2328
return Client(application)
2429

2530

tests/integrations/stdlib/test_httplib.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
import pytest
2+
13
try:
24
from urllib.request import urlopen
35
except ImportError:
46
from urllib import urlopen
57

8+
try:
9+
from httplib import HTTPConnection
10+
except ImportError:
11+
from http.client import HTTPConnection
12+
613
from sentry_sdk import capture_message
714
from sentry_sdk.integrations.stdlib import StdlibIntegration
815

@@ -55,3 +62,44 @@ def before_breadcrumb(crumb, hint):
5562
"reason": "OK",
5663
"extra": "foo",
5764
}
65+
66+
67+
def test_httplib_misuse(sentry_init, capture_events):
68+
"""HTTPConnection.getresponse must be called after every call to
69+
HTTPConnection.request. However, if somebody does not abide by
70+
this contract, we still should handle this gracefully and not
71+
send mixed breadcrumbs.
72+
73+
Test whether our breadcrumbs are coherent when somebody uses HTTPConnection
74+
wrongly.
75+
"""
76+
77+
sentry_init()
78+
events = capture_events()
79+
80+
conn = HTTPConnection("httpbin.org", 80)
81+
conn.request("GET", "/anything/foo")
82+
83+
with pytest.raises(Exception):
84+
# This raises an exception, because we didn't call `getresponse` for
85+
# the previous request yet.
86+
#
87+
# This call should not affect our breadcrumb.
88+
conn.request("POST", "/anything/bar")
89+
90+
response = conn.getresponse()
91+
assert response._method == "GET"
92+
93+
capture_message("Testing!")
94+
95+
event, = events
96+
crumb, = event["breadcrumbs"]
97+
98+
assert crumb["type"] == "http"
99+
assert crumb["category"] == "httplib"
100+
assert crumb["data"] == {
101+
"url": "http://httpbin.org/anything/foo",
102+
"method": "GET",
103+
"status_code": 200,
104+
"reason": "OK",
105+
}

0 commit comments

Comments
 (0)