Skip to content

Commit 748764e

Browse files
committed
test(transport): Add test for HTTP error status handling
Adds test coverage for the error handling path when HTTP requests return error status codes. GH-4568
1 parent 666ff3a commit 748764e

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

tests/test_transport.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,9 +619,9 @@ def test_record_lost_event_transaction_item(capturing_server, make_client, span_
619619
transport.record_lost_event(reason="test", item=transaction_item)
620620
client.flush()
621621

622-
(captured,) = capturing_server.captured # Should only be one envelope
622+
(captured,) = capturing_server.captured
623623
envelope = captured.envelope
624-
(item,) = envelope.items # Envelope should only have one item
624+
(item,) = envelope.items
625625

626626
assert item.type == "client_report"
627627

@@ -641,3 +641,38 @@ def test_record_lost_event_transaction_item(capturing_server, make_client, span_
641641
"reason": "test",
642642
"quantity": span_count + 1,
643643
} in discarded_events
644+
645+
646+
def test_handle_unexpected_status_invokes_handle_request_error(
647+
make_client, monkeypatch
648+
):
649+
client = make_client()
650+
transport = client.transport
651+
652+
monkeypatch.setattr(transport._worker, "submit", lambda fn: fn() or True)
653+
654+
def stub_request(method, endpoint, body=None, headers=None):
655+
class MockResponse:
656+
def __init__(self):
657+
self.status = 500 # Integer
658+
self.data = b"server error"
659+
self.headers = {}
660+
661+
def close(self):
662+
pass
663+
664+
return MockResponse()
665+
666+
monkeypatch.setattr(transport, "_request", stub_request)
667+
668+
seen = []
669+
monkeypatch.setattr(
670+
transport,
671+
"_handle_request_error",
672+
lambda envelope, loss_reason: seen.append(loss_reason),
673+
)
674+
675+
client.capture_event({"message": "test"})
676+
client.flush()
677+
678+
assert seen == ["status_500"]

0 commit comments

Comments
 (0)