Skip to content

Commit eda4db5

Browse files
committed
requests test, better test server
1 parent b10cae6 commit eda4db5

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

sentry_sdk/tracing_utils.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,24 +156,26 @@ def record_sql_queries(
156156

157157
def maybe_create_breadcrumbs_from_span(scope, span):
158158
# type: (sentry_sdk.Scope, sentry_sdk.tracing.Span) -> None
159-
160159
if span.op == OP.DB_REDIS:
161160
scope.add_breadcrumb(
162161
message=span.description, type="redis", category="redis", data=span._tags
163162
)
164163

165164
elif span.op == OP.HTTP_CLIENT:
166-
level = "info" # XXX is this correct?
165+
level = None
167166
status_code = span._data.get(SPANDATA.HTTP_STATUS_CODE)
168167
if status_code:
169168
if 500 <= status_code <= 599:
170169
level = "error"
171170
elif 400 <= status_code <= 499:
172171
level = "warning"
173172

174-
scope.add_breadcrumb(
175-
type="http", category="httplib", data=span._data, level=level
176-
)
173+
if level:
174+
scope.add_breadcrumb(
175+
type="http", category="httplib", data=span._data, level=level
176+
)
177+
else:
178+
scope.add_breadcrumb(type="http", category="httplib", data=span._data)
177179

178180
elif span.op == "subprocess":
179181
scope.add_breadcrumb(

tests/conftest.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,14 @@ def suppress_deprecation_warnings():
587587

588588
class MockServerRequestHandler(BaseHTTPRequestHandler):
589589
def do_GET(self): # noqa: N802
590-
# Process an HTTP GET request and return a response with an HTTP 200 status.
591-
self.send_response(200)
590+
# Process an HTTP GET request and return a response.
591+
# If the path ends with /status/<number>, return status code <number>.
592+
# Otherwise return a 200 response.
593+
code = 200
594+
if "/status/" in self.path:
595+
code = int(self.path[-3:])
596+
597+
self.send_response(code)
592598
self.end_headers()
593599
return
594600

tests/integrations/requests/test_requests.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,69 @@
22

33
import pytest
44
import requests
5-
import responses
65

76
from sentry_sdk import capture_message
87
from sentry_sdk.consts import SPANDATA
98
from sentry_sdk.integrations.stdlib import StdlibIntegration
10-
from tests.conftest import ApproxDict
9+
from tests.conftest import ApproxDict, create_mock_http_server
10+
11+
PORT = create_mock_http_server()
1112

1213

1314
def test_crumb_capture(sentry_init, capture_events):
1415
sentry_init(integrations=[StdlibIntegration()])
16+
events = capture_events()
17+
18+
url = f"http://localhost:{PORT}/hello-world" # noqa:E231
19+
response = requests.get(url)
20+
capture_message("Testing!")
21+
22+
(event,) = events
23+
(crumb,) = event["breadcrumbs"]["values"]
24+
assert crumb["type"] == "http"
25+
assert crumb["category"] == "httplib"
26+
assert crumb["data"] == ApproxDict(
27+
{
28+
"url": url,
29+
SPANDATA.HTTP_METHOD: "GET",
30+
SPANDATA.HTTP_FRAGMENT: "",
31+
SPANDATA.HTTP_QUERY: "",
32+
SPANDATA.HTTP_STATUS_CODE: response.status_code,
33+
"reason": response.reason,
34+
}
35+
)
36+
1537

16-
url = "http://example.com/"
17-
responses.add(responses.GET, url, status=200)
38+
@pytest.mark.parametrize(
39+
"status_code,level",
40+
[
41+
(200, None),
42+
(301, None),
43+
(403, "warning"),
44+
(405, "warning"),
45+
(500, "error"),
46+
],
47+
)
48+
def test_crumb_capture_warning(sentry_init, capture_events, status_code, level):
49+
sentry_init(integrations=[StdlibIntegration()])
1850

1951
events = capture_events()
2052

53+
url = f"http://localhost:{PORT}/status/{status_code}" # noqa:E231
2154
response = requests.get(url)
55+
2256
capture_message("Testing!")
2357

2458
(event,) = events
2559
(crumb,) = event["breadcrumbs"]["values"]
2660
assert crumb["type"] == "http"
2761
assert crumb["category"] == "httplib"
62+
63+
if level is None:
64+
assert "level" not in crumb
65+
else:
66+
assert crumb["level"] == level
67+
2868
assert crumb["data"] == ApproxDict(
2969
{
3070
"url": url,
@@ -41,11 +81,10 @@ def test_crumb_capture(sentry_init, capture_events):
4181
def test_omit_url_data_if_parsing_fails(sentry_init, capture_events):
4282
sentry_init(integrations=[StdlibIntegration()])
4383

44-
url = "https://example.com"
45-
responses.add(responses.GET, url, status=200)
46-
4784
events = capture_events()
4885

86+
url = f"http://localhost:{PORT}/ok" # noqa:E231
87+
4988
with mock.patch(
5089
"sentry_sdk.integrations.stdlib.parse_url",
5190
side_effect=ValueError,
@@ -63,7 +102,6 @@ def test_omit_url_data_if_parsing_fails(sentry_init, capture_events):
63102
# no url related data
64103
}
65104
)
66-
67105
assert "url" not in event["breadcrumbs"]["values"][0]["data"]
68106
assert SPANDATA.HTTP_FRAGMENT not in event["breadcrumbs"]["values"][0]["data"]
69107
assert SPANDATA.HTTP_QUERY not in event["breadcrumbs"]["values"][0]["data"]

0 commit comments

Comments
 (0)