forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sanic.py
More file actions
171 lines (126 loc) · 4.31 KB
/
Copy pathtest_sanic.py
File metadata and controls
171 lines (126 loc) · 4.31 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
import sys
import random
import asyncio
import pytest
from sentry_sdk import capture_message, configure_scope
from sentry_sdk.integrations.sanic import SanicIntegration
from sanic import Sanic, request, response
from sanic.exceptions import abort
@pytest.fixture
def app():
app = Sanic(__name__)
@app.route("/message")
def hi(request):
capture_message("hi")
return response.text("ok")
return app
def test_request_data(sentry_init, app, capture_events):
sentry_init(integrations=[SanicIntegration()])
events = capture_events()
request, response = app.test_client.get("/message?foo=bar")
assert response.status == 200
event, = events
assert event["transaction"] == "hi"
assert event["request"]["env"] == {"REMOTE_ADDR": ""}
assert set(event["request"]["headers"]) == {
"accept",
"accept-encoding",
"host",
"user-agent",
}
assert event["request"]["query_string"] == "foo=bar"
assert event["request"]["url"].endswith("/message")
assert event["request"]["method"] == "GET"
# Assert that state is not leaked
events.clear()
capture_message("foo")
event, = events
assert "request" not in event
assert "transaction" not in event
def test_errors(sentry_init, app, capture_events):
sentry_init(integrations=[SanicIntegration()])
events = capture_events()
@app.route("/error")
def myerror(request):
raise ValueError("oh no")
request, response = app.test_client.get("/error")
assert response.status == 500
event, = events
assert event["transaction"] == "myerror"
exception, = event["exception"]["values"]
assert exception["type"] == "ValueError"
assert exception["value"] == "oh no"
assert any(
frame["filename"].endswith("test_sanic.py")
for frame in exception["stacktrace"]["frames"]
)
def test_bad_request_not_captured(sentry_init, app, capture_events):
sentry_init(integrations=[SanicIntegration()])
events = capture_events()
@app.route("/")
def index(request):
abort(400)
request, response = app.test_client.get("/")
assert response.status == 400
assert not events
def test_error_in_errorhandler(sentry_init, app, capture_events):
sentry_init(integrations=[SanicIntegration()])
events = capture_events()
@app.route("/error")
def myerror(request):
raise ValueError("oh no")
@app.exception(ValueError)
def myhandler(request, exception):
1 / 0
request, response = app.test_client.get("/error")
assert response.status == 500
event1, event2 = events
exception, = event1["exception"]["values"]
assert exception["type"] == "ValueError"
assert any(
frame["filename"].endswith("test_sanic.py")
for frame in exception["stacktrace"]["frames"]
)
exception = event2["exception"]["values"][-1]
assert exception["type"] == "ZeroDivisionError"
assert any(
frame["filename"].endswith("test_sanic.py")
for frame in exception["stacktrace"]["frames"]
)
def test_concurrency(sentry_init, app):
sentry_init(integrations=[SanicIntegration()])
@app.route("/context-check/<i>")
async def context_check(request, i):
with configure_scope() as scope:
scope.set_tag("i", i)
await asyncio.sleep(random.random())
with configure_scope() as scope:
assert scope._tags["i"] == i
return response.text("ok")
async def task(i):
responses = []
await app.handle_request(
request.Request(
url_bytes="http://localhost/context-check/{i}".format(i=i).encode(
"ascii"
),
headers={},
version="1.1",
method="GET",
transport=None,
),
write_callback=responses.append,
stream_callback=responses.append,
)
r, = responses
assert r.status == 200
async def runner():
await asyncio.gather(*(task(i) for i in range(1000)))
if sys.version_info < (3, 7):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(runner())
else:
asyncio.run(runner())
with configure_scope() as scope:
assert not scope._tags