From 39b23915023dde0e0298b822de3d23960a5024e6 Mon Sep 17 00:00:00 2001 From: Pandede Date: Wed, 6 Nov 2024 18:31:45 +0800 Subject: [PATCH] Fix the client sometimes would not retry (#109) --- aiohttp_retry/client.py | 2 +- aiohttp_retry/retry_options.py | 2 +- tests/test_client.py | 13 ++++++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/aiohttp_retry/client.py b/aiohttp_retry/client.py index 2c31406..42d55e1 100644 --- a/aiohttp_retry/client.py +++ b/aiohttp_retry/client.py @@ -89,7 +89,7 @@ async def _is_skip_retry(self, current_attempt: int, response: ClientResponse) - if current_attempt == self._retry_options.attempts: return True - if response.method not in self._retry_options.methods: + if response.method.upper() not in self._retry_options.methods: return True if response.status >= _MIN_SERVER_ERROR_STATUS and self._retry_options.retry_all_server_errors: diff --git a/aiohttp_retry/retry_options.py b/aiohttp_retry/retry_options.py index bbe1877..2af17fa 100644 --- a/aiohttp_retry/retry_options.py +++ b/aiohttp_retry/retry_options.py @@ -32,7 +32,7 @@ def __init__( if methods is None: methods = {"HEAD", "GET", "PUT", "DELETE", "OPTIONS", "TRACE", "POST", "CONNECT", "PATCH"} - self.methods: Iterable[str] = methods + self.methods: Iterable[str] = {method.upper() for method in methods} self.retry_all_server_errors = retry_all_server_errors self.evaluate_response_callback = evaluate_response_callback diff --git a/tests/test_client.py b/tests/test_client.py index a72e2ec..e96735c 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -349,7 +349,18 @@ async def test_list_retry_works_for_multiple_attempts(aiohttp_client: pytest_aio async def test_dont_retry_if_not_in_retry_methods(aiohttp_client: pytest_aiohttp.plugin.AiohttpClient) -> None: retry_client, test_app = await get_retry_client_and_test_app_for_test( aiohttp_client, - retry_options=ExponentialRetry(methods={"POST"}), # not "GET" + retry_options=ExponentialRetry(), # try on all methods by default + ) + + async with retry_client.get("/internal_error") as response: + assert response.status == 500 + assert test_app.counter == 3 + + await retry_client.close() + + retry_client, test_app = await get_retry_client_and_test_app_for_test( + aiohttp_client, + retry_options=ExponentialRetry(methods={"POST"}), # try on only POST method ) async with retry_client.get("/internal_error") as response: