|
24 | 24 | from conductor import Conductor, AsyncConductor, APIResponseValidationError
|
25 | 25 | from conductor._types import Omit
|
26 | 26 | from conductor._models import BaseModel, FinalRequestOptions
|
27 |
| -from conductor._exceptions import ConductorError, APIResponseValidationError |
| 27 | +from conductor._exceptions import APIStatusError, ConductorError, APIResponseValidationError |
28 | 28 | from conductor._base_client import (
|
29 | 29 | DEFAULT_TIMEOUT,
|
30 | 30 | HTTPX_DEFAULT_TIMEOUT,
|
@@ -781,6 +781,33 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
|
781 | 781 |
|
782 | 782 | assert response.http_request.headers.get("x-stainless-retry-count") == "42"
|
783 | 783 |
|
| 784 | + @pytest.mark.respx(base_url=base_url) |
| 785 | + def test_follow_redirects(self, respx_mock: MockRouter) -> None: |
| 786 | + # Test that the default follow_redirects=True allows following redirects |
| 787 | + respx_mock.post("/redirect").mock( |
| 788 | + return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"}) |
| 789 | + ) |
| 790 | + respx_mock.get("/redirected").mock(return_value=httpx.Response(200, json={"status": "ok"})) |
| 791 | + |
| 792 | + response = self.client.post("/redirect", body={"key": "value"}, cast_to=httpx.Response) |
| 793 | + assert response.status_code == 200 |
| 794 | + assert response.json() == {"status": "ok"} |
| 795 | + |
| 796 | + @pytest.mark.respx(base_url=base_url) |
| 797 | + def test_follow_redirects_disabled(self, respx_mock: MockRouter) -> None: |
| 798 | + # Test that follow_redirects=False prevents following redirects |
| 799 | + respx_mock.post("/redirect").mock( |
| 800 | + return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"}) |
| 801 | + ) |
| 802 | + |
| 803 | + with pytest.raises(APIStatusError) as exc_info: |
| 804 | + self.client.post( |
| 805 | + "/redirect", body={"key": "value"}, options={"follow_redirects": False}, cast_to=httpx.Response |
| 806 | + ) |
| 807 | + |
| 808 | + assert exc_info.value.response.status_code == 302 |
| 809 | + assert exc_info.value.response.headers["Location"] == f"{base_url}/redirected" |
| 810 | + |
784 | 811 |
|
785 | 812 | class TestAsyncConductor:
|
786 | 813 | client = AsyncConductor(base_url=base_url, api_key=api_key, _strict_response_validation=True)
|
@@ -1578,3 +1605,30 @@ async def test_main() -> None:
|
1578 | 1605 | raise AssertionError("calling get_platform using asyncify resulted in a hung process")
|
1579 | 1606 |
|
1580 | 1607 | time.sleep(0.1)
|
| 1608 | + |
| 1609 | + @pytest.mark.respx(base_url=base_url) |
| 1610 | + async def test_follow_redirects(self, respx_mock: MockRouter) -> None: |
| 1611 | + # Test that the default follow_redirects=True allows following redirects |
| 1612 | + respx_mock.post("/redirect").mock( |
| 1613 | + return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"}) |
| 1614 | + ) |
| 1615 | + respx_mock.get("/redirected").mock(return_value=httpx.Response(200, json={"status": "ok"})) |
| 1616 | + |
| 1617 | + response = await self.client.post("/redirect", body={"key": "value"}, cast_to=httpx.Response) |
| 1618 | + assert response.status_code == 200 |
| 1619 | + assert response.json() == {"status": "ok"} |
| 1620 | + |
| 1621 | + @pytest.mark.respx(base_url=base_url) |
| 1622 | + async def test_follow_redirects_disabled(self, respx_mock: MockRouter) -> None: |
| 1623 | + # Test that follow_redirects=False prevents following redirects |
| 1624 | + respx_mock.post("/redirect").mock( |
| 1625 | + return_value=httpx.Response(302, headers={"Location": f"{base_url}/redirected"}) |
| 1626 | + ) |
| 1627 | + |
| 1628 | + with pytest.raises(APIStatusError) as exc_info: |
| 1629 | + await self.client.post( |
| 1630 | + "/redirect", body={"key": "value"}, options={"follow_redirects": False}, cast_to=httpx.Response |
| 1631 | + ) |
| 1632 | + |
| 1633 | + assert exc_info.value.response.status_code == 302 |
| 1634 | + assert exc_info.value.response.headers["Location"] == f"{base_url}/redirected" |
0 commit comments