Skip to content

Commit 945394d

Browse files
merge
2 parents a3f7747 + fb549ba commit 945394d

1 file changed

Lines changed: 39 additions & 33 deletions

File tree

tests/integrations/stdlib/test_httplib.py

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,20 @@ def test_httplib_misuse(sentry_init, capture_events, request):
202202
)
203203

204204

205-
def test_outgoing_trace_headers(sentry_init, capture_events, monkeypatch):
206-
# HTTPSConnection.send is passed a string containing (among other things)
207-
# the headers on the request. Mock it so we can check the headers, and also
208-
# so it doesn't try to actually talk to the internet.
209-
mock_send = mock.Mock()
210-
monkeypatch.setattr(HTTPSConnection, "send", mock_send)
205+
def test_outgoing_trace_headers(sentry_init, capture_events):
206+
original_send = HTTPConnection.send
207+
208+
request_headers = {}
209+
210+
class HttpConnectionRecordingRequestHeaders(HTTPConnection):
211+
def send(self, *args, **kwargs) -> None:
212+
request_str = args[0]
213+
for line in request_str.decode("utf-8").split("\r\n")[1:]:
214+
if line:
215+
key, val = line.split(": ")
216+
request_headers[key] = val
217+
218+
original_send(self, *args, **kwargs)
211219

212220
sentry_init(traces_sample_rate=1.0)
213221
events = capture_events()
@@ -229,20 +237,15 @@ def test_outgoing_trace_headers(sentry_init, capture_events, monkeypatch):
229237
op="greeting.sniff",
230238
trace_id="12312012123120121231201212312012",
231239
) as transaction:
232-
HTTPSConnection("www.squirrelchasers.com").request("GET", "/top-chasers")
233-
234-
(request_str,) = mock_send.call_args[0]
235-
request_headers = {}
236-
for line in request_str.decode("utf-8").split("\r\n")[1:]:
237-
if line:
238-
key, val = line.split(": ")
239-
request_headers[key] = val
240+
connection = HttpConnectionRecordingRequestHeaders("localhost", port=PORT)
241+
connection.request("GET", "/top-chasers")
242+
connection.getresponse()
240243

241244
(event,) = events
242245
request_span = event["spans"][-1]
243246
expected_sentry_trace = "{trace_id}-{parent_span_id}-{sampled}".format(
244-
trace_id=transaction.trace_id,
245-
parent_span_id=request_span.span_id,
247+
trace_id=event["contexts"]["trace"]["trace_id"],
248+
parent_span_id=request_span["span_id"],
246249
sampled=1,
247250
)
248251
assert request_headers["sentry-trace"] == expected_sentry_trace
@@ -258,12 +261,20 @@ def test_outgoing_trace_headers(sentry_init, capture_events, monkeypatch):
258261
assert request_headers["baggage"] == expected_outgoing_baggage
259262

260263

261-
def test_outgoing_trace_headers_head_sdk(sentry_init, capture_events, monkeypatch):
262-
# HTTPSConnection.send is passed a string containing (among other things)
263-
# the headers on the request. Mock it so we can check the headers, and also
264-
# so it doesn't try to actually talk to the internet.
265-
mock_send = mock.Mock()
266-
monkeypatch.setattr(HTTPSConnection, "send", mock_send)
264+
def test_outgoing_trace_headers_head_sdk(sentry_init, capture_events):
265+
original_send = HTTPConnection.send
266+
267+
request_headers = {}
268+
269+
class HttpConnectionRecordingRequestHeaders(HTTPConnection):
270+
def send(self, *args, **kwargs) -> None:
271+
request_str = args[0]
272+
for line in request_str.decode("utf-8").split("\r\n")[1:]:
273+
if line:
274+
key, val = line.split(": ")
275+
request_headers[key] = val
276+
277+
original_send(self, *args, **kwargs)
267278

268279
sentry_init(traces_sample_rate=0.5, release="foo")
269280
events = capture_events()
@@ -272,23 +283,18 @@ def test_outgoing_trace_headers_head_sdk(sentry_init, capture_events, monkeypatc
272283
transaction = continue_trace({})
273284

274285
with start_transaction(transaction=transaction, name="Head SDK tx") as transaction:
275-
connection = HTTPSConnection("www.squirrelchasers.com")
286+
connection = HttpConnectionRecordingRequestHeaders("localhost", port=PORT)
276287
connection.request("GET", "/top-chasers")
277-
278-
(request_str,) = mock_send.call_args[0]
279-
request_headers = {}
280-
for line in request_str.decode("utf-8").split("\r\n")[1:]:
281-
if line:
282-
key, val = line.split(": ")
283-
request_headers[key] = val
288+
connection.getresponse()
284289

285290
(event,) = events
286-
request_span = event["spans"][0]
291+
request_span = event["spans"][-1]
287292
expected_sentry_trace = "{trace_id}-{parent_span_id}-{sampled}".format(
288-
trace_id=transaction.trace_id,
289-
parent_span_id=request_span.span_id,
293+
trace_id=event["contexts"]["trace"]["trace_id"],
294+
parent_span_id=request_span["span_id"],
290295
sampled=1,
291296
)
297+
292298
assert request_headers["sentry-trace"] == expected_sentry_trace
293299

294300
expected_outgoing_baggage = (

0 commit comments

Comments
 (0)