-
Notifications
You must be signed in to change notification settings - Fork 630
Expand file tree
/
Copy pathtest_httpx.py
More file actions
299 lines (265 loc) · 8.36 KB
/
Copy pathtest_httpx.py
File metadata and controls
299 lines (265 loc) · 8.36 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import asyncio
import pytest
import httpx
import responses
from sentry_sdk import capture_message, start_transaction
from sentry_sdk.consts import MATCH_ALL, SPANDATA
from sentry_sdk.integrations.httpx import HttpxIntegration
try:
from unittest import mock # python 3.3 and above
except ImportError:
import mock # python < 3.3
@pytest.mark.parametrize(
"httpx_client",
(httpx.Client(), httpx.AsyncClient()),
)
def test_crumb_capture_and_hint(sentry_init, capture_events, httpx_client):
def before_breadcrumb(crumb, hint):
crumb["data"]["extra"] = "foo"
return crumb
sentry_init(integrations=[HttpxIntegration()], before_breadcrumb=before_breadcrumb)
url = "http://example.com/"
responses.add(responses.GET, url, status=200)
with start_transaction():
events = capture_events()
if asyncio.iscoroutinefunction(httpx_client.get):
response = asyncio.get_event_loop().run_until_complete(
httpx_client.get(url)
)
else:
response = httpx_client.get(url)
assert response.status_code == 200
capture_message("Testing!")
(event,) = events
crumb = event["breadcrumbs"]["values"][0]
assert crumb["type"] == "http"
assert crumb["category"] == "httplib"
assert crumb["data"] == {
"url": url,
SPANDATA.HTTP_METHOD: "GET",
SPANDATA.HTTP_FRAGMENT: "",
SPANDATA.HTTP_QUERY: "",
SPANDATA.HTTP_STATUS_CODE: 200,
"reason": "OK",
"extra": "foo",
}
@pytest.mark.parametrize(
"httpx_client",
(httpx.Client(), httpx.AsyncClient()),
)
def test_outgoing_trace_headers(sentry_init, httpx_client):
sentry_init(traces_sample_rate=1.0, integrations=[HttpxIntegration()])
url = "http://example.com/"
responses.add(responses.GET, url, status=200)
with start_transaction(
name="/interactions/other-dogs/new-dog",
op="greeting.sniff",
trace_id="01234567890123456789012345678901",
) as transaction:
if asyncio.iscoroutinefunction(httpx_client.get):
response = asyncio.get_event_loop().run_until_complete(
httpx_client.get(url)
)
else:
response = httpx_client.get(url)
request_span = transaction._span_recorder.spans[-1]
assert response.request.headers[
"sentry-trace"
] == "{trace_id}-{parent_span_id}-{sampled}".format(
trace_id=transaction.trace_id,
parent_span_id=request_span.span_id,
sampled=1,
)
@pytest.mark.parametrize(
"httpx_client",
(httpx.Client(), httpx.AsyncClient()),
)
def test_outgoing_trace_headers_append_to_baggage(sentry_init, httpx_client):
sentry_init(
traces_sample_rate=1.0,
integrations=[HttpxIntegration()],
release="d08ebdb9309e1b004c6f52202de58a09c2268e42",
)
url = "http://example.com/"
responses.add(responses.GET, url, status=200)
with start_transaction(
name="/interactions/other-dogs/new-dog",
op="greeting.sniff",
trace_id="01234567890123456789012345678901",
) as transaction:
if asyncio.iscoroutinefunction(httpx_client.get):
response = asyncio.get_event_loop().run_until_complete(
httpx_client.get(url, headers={"baGGage": "custom=data"})
)
else:
response = httpx_client.get(url, headers={"baGGage": "custom=data"})
request_span = transaction._span_recorder.spans[-1]
assert response.request.headers[
"sentry-trace"
] == "{trace_id}-{parent_span_id}-{sampled}".format(
trace_id=transaction.trace_id,
parent_span_id=request_span.span_id,
sampled=1,
)
assert (
response.request.headers["baggage"]
== "custom=data,sentry-trace_id=01234567890123456789012345678901,sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0"
)
@pytest.mark.parametrize(
"httpx_client,trace_propagation_targets,url,trace_propagated",
[
[
httpx.Client(),
None,
"https://example.com/",
False,
],
[
httpx.Client(),
[],
"https://example.com/",
False,
],
[
httpx.Client(),
[MATCH_ALL],
"https://example.com/",
True,
],
[
httpx.Client(),
["https://example.com/"],
"https://example.com/",
True,
],
[
httpx.Client(),
["https://example.com/"],
"https://example.com",
False,
],
[
httpx.Client(),
["https://example.com"],
"https://example.com",
True,
],
[
httpx.Client(),
["https://example.com", r"https?:\/\/[\w\-]+(\.[\w\-]+)+\.net"],
"https://example.net",
False,
],
[
httpx.Client(),
["https://example.com", r"https?:\/\/[\w\-]+(\.[\w\-]+)+\.net"],
"https://good.example.net",
True,
],
[
httpx.Client(),
["https://example.com", r"https?:\/\/[\w\-]+(\.[\w\-]+)+\.net"],
"https://good.example.net/some/thing",
True,
],
[
httpx.AsyncClient(),
None,
"https://example.com/",
False,
],
[
httpx.AsyncClient(),
[],
"https://example.com/",
False,
],
[
httpx.AsyncClient(),
[MATCH_ALL],
"https://example.com/",
True,
],
[
httpx.AsyncClient(),
["https://example.com/"],
"https://example.com/",
True,
],
[
httpx.AsyncClient(),
["https://example.com/"],
"https://example.com",
False,
],
[
httpx.AsyncClient(),
["https://example.com"],
"https://example.com",
True,
],
[
httpx.AsyncClient(),
["https://example.com", r"https?:\/\/[\w\-]+(\.[\w\-]+)+\.net"],
"https://example.net",
False,
],
[
httpx.AsyncClient(),
["https://example.com", r"https?:\/\/[\w\-]+(\.[\w\-]+)+\.net"],
"https://good.example.net",
True,
],
[
httpx.AsyncClient(),
["https://example.com", r"https?:\/\/[\w\-]+(\.[\w\-]+)+\.net"],
"https://good.example.net/some/thing",
True,
],
],
)
def test_option_trace_propagation_targets(
sentry_init,
httpx_client,
httpx_mock, # this comes from pytest-httpx
trace_propagation_targets,
url,
trace_propagated,
):
httpx_mock.add_response()
sentry_init(
release="test",
trace_propagation_targets=trace_propagation_targets,
traces_sample_rate=1.0,
integrations=[HttpxIntegration()],
)
if asyncio.iscoroutinefunction(httpx_client.get):
asyncio.get_event_loop().run_until_complete(httpx_client.get(url))
else:
httpx_client.get(url)
request_headers = httpx_mock.get_request().headers
if trace_propagated:
assert "sentry-trace" in request_headers
else:
assert "sentry-trace" not in request_headers
@pytest.mark.tests_internal_exceptions
def test_omit_url_data_if_parsing_fails(sentry_init, capture_events):
sentry_init(integrations=[HttpxIntegration()])
httpx_client = httpx.Client()
url = "http://example.com"
responses.add(responses.GET, url, status=200)
events = capture_events()
with mock.patch(
"sentry_sdk.integrations.httpx.parse_url",
side_effect=ValueError,
):
response = httpx_client.get(url)
assert response.status_code == 200
capture_message("Testing!")
(event,) = events
assert event["breadcrumbs"]["values"][0]["data"] == {
SPANDATA.HTTP_METHOD: "GET",
SPANDATA.HTTP_STATUS_CODE: 200,
"reason": "OK",
# no url related data
}