|
7 | 7 | import asyncio
|
8 | 8 | import inspect
|
9 | 9 | from typing import Any, Dict, Union, cast
|
| 10 | +from unittest import mock |
10 | 11 |
|
11 | 12 | import httpx
|
12 | 13 | import pytest
|
@@ -420,6 +421,33 @@ class Model(BaseModel):
|
420 | 421 | response = client.get("/foo", cast_to=Model)
|
421 | 422 | assert isinstance(response, str) # type: ignore[unreachable]
|
422 | 423 |
|
| 424 | + @pytest.mark.parametrize( |
| 425 | + "remaining_retries,retry_after,timeout", |
| 426 | + [ |
| 427 | + [3, "20", 20], |
| 428 | + [3, "0", 2], |
| 429 | + [3, "-10", 2], |
| 430 | + [3, "60", 60], |
| 431 | + [3, "61", 2], |
| 432 | + [3, "Fri, 29 Sep 2023 16:26:57 GMT", 20], |
| 433 | + [3, "Fri, 29 Sep 2023 16:26:37 GMT", 2], |
| 434 | + [3, "Fri, 29 Sep 2023 16:26:27 GMT", 2], |
| 435 | + [3, "Fri, 29 Sep 2023 16:27:37 GMT", 60], |
| 436 | + [3, "Fri, 29 Sep 2023 16:27:38 GMT", 2], |
| 437 | + [3, "99999999999999999999999999999999999", 2], |
| 438 | + [3, "Zun, 29 Sep 2023 16:26:27 GMT", 2], |
| 439 | + [3, "", 2], |
| 440 | + ], |
| 441 | + ) |
| 442 | + @mock.patch("time.time", mock.MagicMock(return_value=1696004797)) |
| 443 | + def test_parse_retry_after_header(self, remaining_retries: int, retry_after: str, timeout: float) -> None: |
| 444 | + client = Finch(base_url=base_url, access_token=access_token, _strict_response_validation=True) |
| 445 | + |
| 446 | + headers = httpx.Headers({"retry-after": retry_after}) |
| 447 | + options = FinalRequestOptions(method="get", url="/foo", max_retries=2) |
| 448 | + calculated = client._calculate_retry_timeout(remaining_retries, options, headers) |
| 449 | + assert calculated == pytest.approx(timeout, 0.6) # pyright: ignore[reportUnknownMemberType] |
| 450 | + |
423 | 451 |
|
424 | 452 | class TestAsyncFinch:
|
425 | 453 | client = AsyncFinch(base_url=base_url, access_token=access_token, _strict_response_validation=True)
|
@@ -815,3 +843,31 @@ class Model(BaseModel):
|
815 | 843 |
|
816 | 844 | response = await client.get("/foo", cast_to=Model)
|
817 | 845 | assert isinstance(response, str) # type: ignore[unreachable]
|
| 846 | + |
| 847 | + @pytest.mark.parametrize( |
| 848 | + "remaining_retries,retry_after,timeout", |
| 849 | + [ |
| 850 | + [3, "20", 20], |
| 851 | + [3, "0", 2], |
| 852 | + [3, "-10", 2], |
| 853 | + [3, "60", 60], |
| 854 | + [3, "61", 2], |
| 855 | + [3, "Fri, 29 Sep 2023 16:26:57 GMT", 20], |
| 856 | + [3, "Fri, 29 Sep 2023 16:26:37 GMT", 2], |
| 857 | + [3, "Fri, 29 Sep 2023 16:26:27 GMT", 2], |
| 858 | + [3, "Fri, 29 Sep 2023 16:27:37 GMT", 60], |
| 859 | + [3, "Fri, 29 Sep 2023 16:27:38 GMT", 2], |
| 860 | + [3, "99999999999999999999999999999999999", 2], |
| 861 | + [3, "Zun, 29 Sep 2023 16:26:27 GMT", 2], |
| 862 | + [3, "", 2], |
| 863 | + ], |
| 864 | + ) |
| 865 | + @mock.patch("time.time", mock.MagicMock(return_value=1696004797)) |
| 866 | + @pytest.mark.asyncio |
| 867 | + async def test_parse_retry_after_header(self, remaining_retries: int, retry_after: str, timeout: float) -> None: |
| 868 | + client = AsyncFinch(base_url=base_url, access_token=access_token, _strict_response_validation=True) |
| 869 | + |
| 870 | + headers = httpx.Headers({"retry-after": retry_after}) |
| 871 | + options = FinalRequestOptions(method="get", url="/foo", max_retries=2) |
| 872 | + calculated = client._calculate_retry_timeout(remaining_retries, options, headers) |
| 873 | + assert calculated == pytest.approx(timeout, 0.6) # pyright: ignore[reportUnknownMemberType] |
0 commit comments