forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_wsgi.py
More file actions
203 lines (151 loc) · 5.7 KB
/
Copy pathtest_wsgi.py
File metadata and controls
203 lines (151 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from werkzeug.test import Client
import pytest
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
try:
from unittest import mock # python 3.3 and above
except ImportError:
import mock # python < 3.3
@pytest.fixture
def crashing_app():
def app(environ, start_response):
1 / 0
return app
class IterableApp(object):
def __init__(self, iterable):
self.iterable = iterable
def __call__(self, environ, start_response):
return self.iterable
class ExitingIterable(object):
def __init__(self, exc_func):
self._exc_func = exc_func
def __iter__(self):
return self
def __next__(self):
raise self._exc_func()
def next(self):
return type(self).__next__(self)
def test_basic(sentry_init, crashing_app, capture_events):
sentry_init(send_default_pii=True)
app = SentryWsgiMiddleware(crashing_app)
client = Client(app)
events = capture_events()
with pytest.raises(ZeroDivisionError):
client.get("/")
(event,) = events
assert event["transaction"] == "generic WSGI request"
assert event["request"] == {
"env": {"SERVER_NAME": "localhost", "SERVER_PORT": "80"},
"headers": {"Host": "localhost"},
"method": "GET",
"query_string": "",
"url": "http://localhost/",
}
@pytest.fixture(params=[0, None])
def test_systemexit_zero_is_ignored(sentry_init, capture_events, request):
zero_code = request.param
sentry_init(send_default_pii=True)
iterable = ExitingIterable(lambda: SystemExit(zero_code))
app = SentryWsgiMiddleware(IterableApp(iterable))
client = Client(app)
events = capture_events()
with pytest.raises(SystemExit):
client.get("/")
assert len(events) == 0
@pytest.fixture(params=["", "foo", 1, 2])
def test_systemexit_nonzero_is_captured(sentry_init, capture_events, request):
nonzero_code = request.param
sentry_init(send_default_pii=True)
iterable = ExitingIterable(lambda: SystemExit(nonzero_code))
app = SentryWsgiMiddleware(IterableApp(iterable))
client = Client(app)
events = capture_events()
with pytest.raises(SystemExit):
client.get("/")
(event,) = events
assert "exception" in event
exc = event["exception"]["values"][-1]
assert exc["type"] == "SystemExit"
assert exc["value"] == nonzero_code
assert event["level"] == "error"
def test_keyboard_interrupt_is_captured(sentry_init, capture_events):
sentry_init(send_default_pii=True)
iterable = ExitingIterable(lambda: KeyboardInterrupt())
app = SentryWsgiMiddleware(IterableApp(iterable))
client = Client(app)
events = capture_events()
with pytest.raises(KeyboardInterrupt):
client.get("/")
(event,) = events
assert "exception" in event
exc = event["exception"]["values"][-1]
assert exc["type"] == "KeyboardInterrupt"
assert exc["value"] == ""
assert event["level"] == "error"
def test_transaction_with_error(
sentry_init, crashing_app, capture_events, DictionaryContaining # noqa:N803
):
def dogpark(environ, start_response):
raise Exception("Fetch aborted. The ball was not returned.")
sentry_init(send_default_pii=True, traces_sample_rate=1.0)
app = SentryWsgiMiddleware(dogpark)
client = Client(app)
events = capture_events()
with pytest.raises(Exception):
client.get("http://dogs.are.great/sit/stay/rollover/")
error_event, envelope = events
assert error_event["transaction"] == "generic WSGI request"
assert error_event["contexts"]["trace"]["op"] == "http.server"
assert error_event["exception"]["values"][0]["type"] == "Exception"
assert (
error_event["exception"]["values"][0]["value"]
== "Fetch aborted. The ball was not returned."
)
assert envelope["type"] == "transaction"
# event trace context is a subset of envelope trace context
assert envelope["contexts"]["trace"] == DictionaryContaining(
error_event["contexts"]["trace"]
)
assert envelope["contexts"]["trace"]["status"] == "internal_error"
assert envelope["transaction"] == error_event["transaction"]
assert envelope["request"] == error_event["request"]
def test_transaction_no_error(
sentry_init, capture_events, DictionaryContaining # noqa:N803
):
def dogpark(environ, start_response):
start_response("200 OK", [])
return ["Go get the ball! Good dog!"]
sentry_init(send_default_pii=True, traces_sample_rate=1.0)
app = SentryWsgiMiddleware(dogpark)
client = Client(app)
events = capture_events()
client.get("/dogs/are/great/")
envelope = events[0]
assert envelope["type"] == "transaction"
assert envelope["transaction"] == "generic WSGI request"
assert envelope["contexts"]["trace"]["op"] == "http.server"
assert envelope["request"] == DictionaryContaining(
{"method": "GET", "url": "http://localhost/dogs/are/great/"}
)
def test_traces_sampler_gets_correct_values_in_sampling_context(
sentry_init, DictionaryContaining, ObjectDescribedBy # noqa:N803
):
def app(environ, start_response):
start_response("200 OK", [])
return ["Go get the ball! Good dog!"]
traces_sampler = mock.Mock(return_value=True)
sentry_init(send_default_pii=True, traces_sampler=traces_sampler)
app = SentryWsgiMiddleware(app)
client = Client(app)
client.get("/dogs/are/great/")
traces_sampler.assert_any_call(
DictionaryContaining(
{
"wsgi_environ": DictionaryContaining(
{
"PATH_INFO": "/dogs/are/great/",
"REQUEST_METHOD": "GET",
},
),
}
)
)