Skip to content

Commit c59d998

Browse files
dispatch response hooks in replay mode
1 parent 8676173 commit c59d998

3 files changed

Lines changed: 8 additions & 18 deletions

File tree

drift/instrumentation/requests/e2e-tests/src/app.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,17 +304,9 @@ def test_streaming_iter_lines():
304304
except Exception as e:
305305
return jsonify({"error": str(e)}), 500
306306

307-
308-
309-
# ============ BUG DETECTION TESTS ============
310-
# These tests expose bugs in the requests instrumentation during REPLAY mode.
311-
# Keep only tests that expose confirmed bugs.
312-
313-
# BUG 4: Response hooks are not called in REPLAY mode
314-
# dispatch_hook() is not called when returning mock response
315307
@app.route("/test/response-hooks", methods=["GET"])
316308
def test_response_hooks():
317-
"""Test response hooks that modify response - EXPOSES BUG."""
309+
"""Test response hooks that modify response."""
318310
try:
319311
hook_called = {"value": False}
320312
hook_status = {"value": 0}

drift/instrumentation/requests/e2e-tests/src/test_requests.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ def make_request(method, endpoint, **kwargs):
7474

7575
make_request("GET", "/test/streaming-iter-lines")
7676

77-
# ============ BUG DETECTION TESTS ============
78-
# These tests expose confirmed bugs in the requests instrumentation
79-
print("\n=== Bug Detection Tests ===\n")
80-
81-
# BUG 4: Response hooks are not called in REPLAY mode
8277
make_request("GET", "/test/response-hooks")
8378

8479
print("\nAll requests completed successfully")

drift/instrumentation/requests/instrumentation.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def original_call():
132132
if sdk.mode == TuskDriftMode.REPLAY:
133133
return handle_replay_mode(
134134
replay_mode_handler=lambda: instrumentation_self._handle_replay_send(
135-
sdk, request
135+
sdk, request, **kwargs
136136
),
137137
no_op_request_handler=lambda: instrumentation_self._get_default_response(url),
138138
is_server_request=False,
@@ -264,17 +264,16 @@ def _handle_replay_send(
264264
self,
265265
sdk: TuskDrift,
266266
prepared_request: Any,
267+
**kwargs,
267268
) -> Any:
268269
"""Handle send() in REPLAY mode.
269270
270271
Similar to _handle_replay but works with PreparedRequest objects.
271272
272273
Args:
273274
sdk: TuskDrift instance
274-
session_self: Session instance
275275
prepared_request: PreparedRequest object
276-
original_send: Original Session.send method
277-
**kwargs: Additional send() kwargs
276+
**kwargs: Additional send() kwargs (timeout, verify, cert, proxies, etc.)
278277
"""
279278
method = prepared_request.method
280279
url = prepared_request.url
@@ -317,6 +316,10 @@ def _handle_replay_send(
317316
)
318317

319318
if mock_response is not None:
319+
# Dispatch response hooks (matches Session.send() behavior)
320+
# This ensures hooks registered via hooks={"response": callback} are called
321+
from requests.hooks import dispatch_hook
322+
mock_response = dispatch_hook("response", prepared_request.hooks, mock_response, **kwargs)
320323
return mock_response
321324

322325
# No mock found - raise error in REPLAY mode

0 commit comments

Comments
 (0)