@@ -850,6 +850,33 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
850850
851851 assert response .http_request .headers .get ("x-stainless-retry-count" ) == "42"
852852
853+ @pytest .mark .respx (base_url = base_url )
854+ def test_follow_redirects (self , respx_mock : MockRouter ) -> None :
855+ # Test that the default follow_redirects=True allows following redirects
856+ respx_mock .post ("/redirect" ).mock (
857+ return_value = httpx .Response (302 , headers = {"Location" : f"{ base_url } /redirected" })
858+ )
859+ respx_mock .get ("/redirected" ).mock (return_value = httpx .Response (200 , json = {"status" : "ok" }))
860+
861+ response = self .client .post ("/redirect" , body = {"key" : "value" }, cast_to = httpx .Response )
862+ assert response .status_code == 200
863+ assert response .json () == {"status" : "ok" }
864+
865+ @pytest .mark .respx (base_url = base_url )
866+ def test_follow_redirects_disabled (self , respx_mock : MockRouter ) -> None :
867+ # Test that follow_redirects=False prevents following redirects
868+ respx_mock .post ("/redirect" ).mock (
869+ return_value = httpx .Response (302 , headers = {"Location" : f"{ base_url } /redirected" })
870+ )
871+
872+ with pytest .raises (APIStatusError ) as exc_info :
873+ self .client .post (
874+ "/redirect" , body = {"key" : "value" }, options = {"follow_redirects" : False }, cast_to = httpx .Response
875+ )
876+
877+ assert exc_info .value .response .status_code == 302
878+ assert exc_info .value .response .headers ["Location" ] == f"{ base_url } /redirected"
879+
853880
854881class TestAsyncStudioSDK :
855882 client = AsyncStudioSDK (base_url = base_url , bearer_token = bearer_token , _strict_response_validation = True )
@@ -1699,3 +1726,30 @@ async def test_main() -> None:
16991726 raise AssertionError ("calling get_platform using asyncify resulted in a hung process" )
17001727
17011728 time .sleep (0.1 )
1729+
1730+ @pytest .mark .respx (base_url = base_url )
1731+ async def test_follow_redirects (self , respx_mock : MockRouter ) -> None :
1732+ # Test that the default follow_redirects=True allows following redirects
1733+ respx_mock .post ("/redirect" ).mock (
1734+ return_value = httpx .Response (302 , headers = {"Location" : f"{ base_url } /redirected" })
1735+ )
1736+ respx_mock .get ("/redirected" ).mock (return_value = httpx .Response (200 , json = {"status" : "ok" }))
1737+
1738+ response = await self .client .post ("/redirect" , body = {"key" : "value" }, cast_to = httpx .Response )
1739+ assert response .status_code == 200
1740+ assert response .json () == {"status" : "ok" }
1741+
1742+ @pytest .mark .respx (base_url = base_url )
1743+ async def test_follow_redirects_disabled (self , respx_mock : MockRouter ) -> None :
1744+ # Test that follow_redirects=False prevents following redirects
1745+ respx_mock .post ("/redirect" ).mock (
1746+ return_value = httpx .Response (302 , headers = {"Location" : f"{ base_url } /redirected" })
1747+ )
1748+
1749+ with pytest .raises (APIStatusError ) as exc_info :
1750+ await self .client .post (
1751+ "/redirect" , body = {"key" : "value" }, options = {"follow_redirects" : False }, cast_to = httpx .Response
1752+ )
1753+
1754+ assert exc_info .value .response .status_code == 302
1755+ assert exc_info .value .response .headers ["Location" ] == f"{ base_url } /redirected"
0 commit comments