|
24 | 24 | from finch import Finch, AsyncFinch, APIResponseValidationError |
25 | 25 | from finch._types import Omit |
26 | 26 | from finch._models import BaseModel, FinalRequestOptions |
27 | | -from finch._exceptions import APIResponseValidationError |
| 27 | +from finch._exceptions import APIStatusError, APIResponseValidationError |
28 | 28 | from finch._base_client import DEFAULT_TIMEOUT, HTTPX_DEFAULT_TIMEOUT, BaseClient, make_request_options |
29 | 29 |
|
30 | 30 | from .utils import update_env |
@@ -829,6 +829,33 @@ def retry_handler(_request: httpx.Request) -> httpx.Response: |
829 | 829 | assert response.retries_taken == failures_before_success |
830 | 830 | assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success |
831 | 831 |
|
| 832 | + @pytest.mark.respx(base_url=base_url) |
| 833 | + def test_follow_redirects(self, respx_mock: MockRouter) -> None: |
| 834 | + # Test that the default follow_redirects=True allows following redirects |
| 835 | + respx_mock.post("/redirect").mock( |
| 836 | + return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"}) |
| 837 | + ) |
| 838 | + respx_mock.get("/redirected").mock(return_value=httpx.Response(200, json={"status": "ok"})) |
| 839 | + |
| 840 | + response = self.client.post("/redirect", body={"key": "value"}, cast_to=httpx.Response) |
| 841 | + assert response.status_code == 200 |
| 842 | + assert response.json() == {"status": "ok"} |
| 843 | + |
| 844 | + @pytest.mark.respx(base_url=base_url) |
| 845 | + def test_follow_redirects_disabled(self, respx_mock: MockRouter) -> None: |
| 846 | + # Test that follow_redirects=False prevents following redirects |
| 847 | + respx_mock.post("/redirect").mock( |
| 848 | + return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"}) |
| 849 | + ) |
| 850 | + |
| 851 | + with pytest.raises(APIStatusError) as exc_info: |
| 852 | + self.client.post( |
| 853 | + "/redirect", body={"key": "value"}, options={"follow_redirects": False}, cast_to=httpx.Response |
| 854 | + ) |
| 855 | + |
| 856 | + assert exc_info.value.response.status_code == 302 |
| 857 | + assert exc_info.value.response.headers["Location"] == f"{base_url}/redirected" |
| 858 | + |
832 | 859 |
|
833 | 860 | class TestAsyncFinch: |
834 | 861 | client = AsyncFinch(base_url=base_url, access_token=access_token, _strict_response_validation=True) |
@@ -1672,3 +1699,30 @@ async def test_main() -> None: |
1672 | 1699 | raise AssertionError("calling get_platform using asyncify resulted in a hung process") |
1673 | 1700 |
|
1674 | 1701 | time.sleep(0.1) |
| 1702 | + |
| 1703 | + @pytest.mark.respx(base_url=base_url) |
| 1704 | + async def test_follow_redirects(self, respx_mock: MockRouter) -> None: |
| 1705 | + # Test that the default follow_redirects=True allows following redirects |
| 1706 | + respx_mock.post("/redirect").mock( |
| 1707 | + return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"}) |
| 1708 | + ) |
| 1709 | + respx_mock.get("/redirected").mock(return_value=httpx.Response(200, json={"status": "ok"})) |
| 1710 | + |
| 1711 | + response = await self.client.post("/redirect", body={"key": "value"}, cast_to=httpx.Response) |
| 1712 | + assert response.status_code == 200 |
| 1713 | + assert response.json() == {"status": "ok"} |
| 1714 | + |
| 1715 | + @pytest.mark.respx(base_url=base_url) |
| 1716 | + async def test_follow_redirects_disabled(self, respx_mock: MockRouter) -> None: |
| 1717 | + # Test that follow_redirects=False prevents following redirects |
| 1718 | + respx_mock.post("/redirect").mock( |
| 1719 | + return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"}) |
| 1720 | + ) |
| 1721 | + |
| 1722 | + with pytest.raises(APIStatusError) as exc_info: |
| 1723 | + await self.client.post( |
| 1724 | + "/redirect", body={"key": "value"}, options={"follow_redirects": False}, cast_to=httpx.Response |
| 1725 | + ) |
| 1726 | + |
| 1727 | + assert exc_info.value.response.status_code == 302 |
| 1728 | + assert exc_info.value.response.headers["Location"] == f"{base_url}/redirected" |
0 commit comments