22
33import pytest
44import requests
5- import responses
65
76from sentry_sdk import capture_message
87from sentry_sdk .consts import SPANDATA
98from 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
1314def 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):
4181def 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