Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repos:
name: isort (python)
args: ['--force-single-line-imports', '--profile', 'black']
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
rev: 7.1.1
hooks:
- id: flake8
args: [ '--max-line-length', '100', '--max-doc-length', '120' ]
Expand Down
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.x.y
-----

* Consider the `Retry-After` header when handling retries

0.25.8
------

Expand Down
6 changes: 4 additions & 2 deletions responses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
try:
from typing_extensions import Literal
except ImportError: # pragma: no cover
from typing import Literal # type: ignore # pragma: no cover
from typing import Literal # pragma: no cover

from io import BufferedReader
from io import BytesIO
Expand Down Expand Up @@ -1151,7 +1151,9 @@ def _on_request(
# first validate that current request is eligible to be retried.
# See ``urllib3.util.retry.Retry`` documentation.
if retries.is_retry(
method=response.request.method, status_code=response.status_code # type: ignore[misc]
method=response.request.method, # type: ignore[misc]
status_code=response.status_code, # type: ignore[misc]
has_retry_after="Retry-After" in response.headers, # type: ignore[misc]
):
try:
retries = retries.increment(
Expand Down
47 changes: 46 additions & 1 deletion responses/tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,51 @@ def run():
run()
assert_reset()

def test_retry_with_retry_after_header(self):
"""Validate that Retry-After header is detected and passed to retry logic"""

@responses.activate(registry=registries.OrderedRegistry)
def run():
url = "https://example.com"
# Add responses with Retry-After header
rsp1 = responses.get(
url,
body="Error",
status=429,
headers={"Retry-After": "1"},
)
rsp2 = responses.get(
url,
body="Error",
status=429,
headers={"Retry-After": "1"},
)
rsp3 = responses.get(url, body="OK", status=200)

# Create session with retry configuration for 429 status
session = requests.Session()
adapter = requests.adapters.HTTPAdapter(
max_retries=Retry(
total=4,
backoff_factor=0.1,
status_forcelist=[429],
allowed_methods=["GET"],
raise_on_status=True,
respect_retry_after_header=True,
)
)
session.mount("https://", adapter)

resp = session.get(url)

assert resp.status_code == 200
assert rsp1.call_count == 1
assert rsp2.call_count == 1
assert rsp3.call_count == 1

run()
assert_reset()


def test_request_object_attached_to_exception():
"""Validate that we attach `request` object to custom exception supplied as body"""
Expand All @@ -2729,7 +2774,7 @@ def run():
try:
requests.get(url, timeout=1)
except requests.ReadTimeout as exc:
assert type(exc.request) == requests.models.PreparedRequest
assert isinstance(exc.request, requests.models.PreparedRequest)

run()
assert_reset()
Expand Down
Loading