|
3 | 3 | Unit tests for atlassian.rest_client module |
4 | 4 | """ |
5 | 5 |
|
| 6 | +from datetime import datetime, timedelta |
| 7 | +from types import SimpleNamespace |
| 8 | + |
6 | 9 | import pytest |
| 10 | + |
7 | 11 | from .mockup import mockup_server |
8 | 12 | from atlassian.rest_client import AtlassianRestAPI |
9 | 13 |
|
@@ -369,3 +373,66 @@ def test_cloud_vs_server_behavior(self): |
369 | 373 | # Cloud flag should be different |
370 | 374 | assert cloud_api.cloud is True |
371 | 375 | assert server_api.cloud is False |
| 376 | + |
| 377 | + def test_retry_handler_clamps_retry_after(self, monkeypatch): |
| 378 | + """Ensure large Retry-After headers are clamped to max_backoff_seconds.""" |
| 379 | + captured = {} |
| 380 | + |
| 381 | + def fake_sleep(delay): |
| 382 | + captured["delay"] = delay |
| 383 | + |
| 384 | + monkeypatch.setattr("atlassian.rest_client.time.sleep", fake_sleep) |
| 385 | + |
| 386 | + api = AtlassianRestAPI( |
| 387 | + url=f"{mockup_server()}/test", |
| 388 | + retry_with_header=True, |
| 389 | + max_backoff_seconds=5, |
| 390 | + ) |
| 391 | + |
| 392 | + handler = api._retry_handler() |
| 393 | + response = SimpleNamespace(headers={"Retry-After": "99999999999"}, status_code=429) |
| 394 | + |
| 395 | + assert handler(response) is True |
| 396 | + assert captured["delay"] == 5 |
| 397 | + |
| 398 | + def test_retry_handler_parses_http_date(self, monkeypatch): |
| 399 | + """Ensure HTTP-date Retry-After headers are converted to delta seconds.""" |
| 400 | + captured = {} |
| 401 | + |
| 402 | + def fake_sleep(delay): |
| 403 | + captured["delay"] = delay |
| 404 | + |
| 405 | + monkeypatch.setattr("atlassian.rest_client.time.sleep", fake_sleep) |
| 406 | + |
| 407 | + api = AtlassianRestAPI( |
| 408 | + url=f"{mockup_server()}/test", |
| 409 | + retry_with_header=True, |
| 410 | + max_backoff_seconds=60, |
| 411 | + ) |
| 412 | + |
| 413 | + handler = api._retry_handler() |
| 414 | + future_delay = 10 |
| 415 | + future_date = datetime.utcnow().replace(tzinfo=None) + timedelta(seconds=future_delay) |
| 416 | + retry_after_value = future_date.strftime("%a, %d %b %Y %H:%M:%S GMT") |
| 417 | + response = SimpleNamespace(headers={"Retry-After": retry_after_value}, status_code=429) |
| 418 | + |
| 419 | + assert handler(response) is True |
| 420 | + assert pytest.approx(captured["delay"], rel=0.1) == future_delay |
| 421 | + |
| 422 | + def test_retry_handler_skips_invalid_header(self, monkeypatch): |
| 423 | + """Ensure invalid Retry-After headers fall back to regular logic.""" |
| 424 | + def fake_sleep(_): |
| 425 | + raise AssertionError("sleep should not be called for invalid header") |
| 426 | + |
| 427 | + monkeypatch.setattr("atlassian.rest_client.time.sleep", fake_sleep) |
| 428 | + |
| 429 | + api = AtlassianRestAPI( |
| 430 | + url=f"{mockup_server()}/test", |
| 431 | + retry_with_header=True, |
| 432 | + ) |
| 433 | + |
| 434 | + handler = api._retry_handler() |
| 435 | + response = SimpleNamespace(headers={"Retry-After": "invalid-value"}, status_code=429) |
| 436 | + |
| 437 | + # Should return False so that other retry mechanisms can take over |
| 438 | + assert handler(response) is False |
0 commit comments