Skip to content

Commit 42d8c73

Browse files
.
1 parent fb549ba commit 42d8c73

1 file changed

Lines changed: 56 additions & 32 deletions

File tree

tests/integrations/stdlib/test_httplib.py

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import socket
23
import datetime
34
from http.client import HTTPConnection, HTTPSConnection
45
from http.server import BaseHTTPRequestHandler, HTTPServer
@@ -203,21 +204,28 @@ def test_httplib_misuse(sentry_init, capture_events, request):
203204

204205

205206
def test_outgoing_trace_headers(sentry_init, capture_events):
206-
original_send = HTTPConnection.send
207+
sentry_init(traces_sample_rate=1.0)
208+
209+
already_patched_getresponse = HTTPSConnection.getresponse
207210

208211
request_headers = {}
209212

210-
class HttpConnectionRecordingRequestHeaders(HTTPConnection):
213+
class HTTPSConnectionRecordingRequestHeaders(HTTPSConnection):
211214
def send(self, *args, **kwargs) -> None:
212215
request_str = args[0]
213216
for line in request_str.decode("utf-8").split("\r\n")[1:]:
214217
if line:
215218
key, val = line.split(": ")
216219
request_headers[key] = val
217220

218-
original_send(self, *args, **kwargs)
221+
server_sock, client_sock = socket.socketpair()
222+
server_sock.sendall(b"HTTP/1.1 200 OK\r\n\r\n")
223+
server_sock.close()
224+
self.sock = client_sock
225+
226+
def getresponse(self, *args, **kwargs):
227+
return already_patched_getresponse(self, *args, **kwargs)
219228

220-
sentry_init(traces_sample_rate=1.0)
221229
events = capture_events()
222230

223231
headers = {
@@ -237,7 +245,7 @@ def send(self, *args, **kwargs) -> None:
237245
op="greeting.sniff",
238246
trace_id="12312012123120121231201212312012",
239247
) as transaction:
240-
connection = HttpConnectionRecordingRequestHeaders("localhost", port=PORT)
248+
connection = HTTPSConnectionRecordingRequestHeaders("localhost", port=PORT)
241249
connection.request("GET", "/top-chasers")
242250
connection.getresponse()
243251

@@ -262,28 +270,35 @@ def send(self, *args, **kwargs) -> None:
262270

263271

264272
def test_outgoing_trace_headers_head_sdk(sentry_init, capture_events):
265-
original_send = HTTPConnection.send
273+
sentry_init(traces_sample_rate=0.5, release="foo")
274+
275+
already_patched_getresponse = HTTPSConnection.getresponse
266276

267277
request_headers = {}
268278

269-
class HttpConnectionRecordingRequestHeaders(HTTPConnection):
279+
class HTTPSConnectionRecordingRequestHeaders(HTTPSConnection):
270280
def send(self, *args, **kwargs) -> None:
271281
request_str = args[0]
272282
for line in request_str.decode("utf-8").split("\r\n")[1:]:
273283
if line:
274284
key, val = line.split(": ")
275285
request_headers[key] = val
276286

277-
original_send(self, *args, **kwargs)
287+
server_sock, client_sock = socket.socketpair()
288+
server_sock.sendall(b"HTTP/1.1 200 OK\r\n\r\n")
289+
server_sock.close()
290+
self.sock = client_sock
291+
292+
def getresponse(self, *args, **kwargs):
293+
return already_patched_getresponse(self, *args, **kwargs)
278294

279-
sentry_init(traces_sample_rate=0.5, release="foo")
280295
events = capture_events()
281296

282297
with mock.patch("sentry_sdk.tracing_utils.Random.randrange", return_value=250000):
283298
transaction = continue_trace({})
284299

285300
with start_transaction(transaction=transaction, name="Head SDK tx") as transaction:
286-
connection = HttpConnectionRecordingRequestHeaders("localhost", port=PORT)
301+
connection = HTTPSConnectionRecordingRequestHeaders("localhost", port=PORT)
287302
connection.request("GET", "/top-chasers")
288303
connection.getresponse()
289304

@@ -369,19 +384,33 @@ def send(self, *args, **kwargs) -> None:
369384
],
370385
)
371386
def test_option_trace_propagation_targets(
372-
sentry_init, monkeypatch, trace_propagation_targets, host, path, trace_propagated
387+
sentry_init, trace_propagation_targets, host, path, trace_propagated
373388
):
374-
# HTTPSConnection.send is passed a string containing (among other things)
375-
# the headers on the request. Mock it so we can check the headers, and also
376-
# so it doesn't try to actually talk to the internet.
377-
mock_send = mock.Mock()
378-
monkeypatch.setattr(HTTPSConnection, "send", mock_send)
379-
380389
sentry_init(
381390
trace_propagation_targets=trace_propagation_targets,
382391
traces_sample_rate=1.0,
383392
)
384393

394+
already_patched_getresponse = HTTPSConnection.getresponse
395+
396+
request_headers = {}
397+
398+
class HTTPSConnectionRecordingRequestHeaders(HTTPSConnection):
399+
def send(self, *args, **kwargs) -> None:
400+
request_str = args[0]
401+
for line in request_str.decode("utf-8").split("\r\n")[1:]:
402+
if line:
403+
key, val = line.split(": ")
404+
request_headers[key] = val
405+
406+
server_sock, client_sock = socket.socketpair()
407+
server_sock.sendall(b"HTTP/1.1 200 OK\r\n\r\n")
408+
server_sock.close()
409+
self.sock = client_sock
410+
411+
def getresponse(self, *args, **kwargs):
412+
return already_patched_getresponse(self, *args, **kwargs)
413+
385414
headers = {
386415
"baggage": (
387416
"sentry-trace_id=771a43a4192642f0b136d5159a501700, "
@@ -397,21 +426,16 @@ def test_option_trace_propagation_targets(
397426
op="greeting.sniff",
398427
trace_id="12312012123120121231201212312012",
399428
) as transaction:
400-
HTTPSConnection(host).request("GET", path)
401-
402-
(request_str,) = mock_send.call_args[0]
403-
request_headers = {}
404-
for line in request_str.decode("utf-8").split("\r\n")[1:]:
405-
if line:
406-
key, val = line.split(": ")
407-
request_headers[key] = val
408-
409-
if trace_propagated:
410-
assert "sentry-trace" in request_headers
411-
assert "baggage" in request_headers
412-
else:
413-
assert "sentry-trace" not in request_headers
414-
assert "baggage" not in request_headers
429+
connection = HTTPSConnectionRecordingRequestHeaders(host)
430+
connection.request("GET", path)
431+
connection.getresponse()
432+
433+
if trace_propagated:
434+
assert "sentry-trace" in request_headers
435+
assert "baggage" in request_headers
436+
else:
437+
assert "sentry-trace" not in request_headers
438+
assert "baggage" not in request_headers
415439

416440

417441
def test_request_source_disabled(sentry_init, capture_events):

0 commit comments

Comments
 (0)