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
109 lines (78 loc) · 2.8 KB
/
test_wsgi.py
File metadata and controls
109 lines (78 loc) · 2.8 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
from werkzeug.test import Client
import pytest
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
@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["request"] == {
"env": {"SERVER_NAME": "localhost", "SERVER_PORT": "80"},
"headers": {"Content-Length": "0", "Content-Type": "", "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"