if not response.is_redirect:
return response
- response.read()
+ if allow_redirects:
+ response.read()
request = self.build_redirect_request(request, response)
history = history + [response]
if not response.is_redirect:
return response
- await response.aread()
+ if allow_redirects:
+ await response.aread()
request = self.build_redirect_request(request, response)
history = history + [response]
codes,
)
from httpx.config import CertTypes, TimeoutTypes, VerifyTypes
+from httpx.content_streams import AsyncIteratorStream
from httpx.dispatch.base import AsyncDispatcher
return Response(codes.OK, request=request)
elif request.url.path == "/redirect_301":
+
+ async def body():
+ yield b"<a href='https://example.org/'>here</a>"
+
status_code = codes.MOVED_PERMANENTLY
headers = {"location": "https://example.org/"}
- return Response(status_code, headers=headers, request=request)
+ stream = AsyncIteratorStream(aiterator=body())
+ return Response(
+ status_code, stream=stream, headers=headers, request=request
+ )
elif request.url.path == "/redirect_302":
status_code = codes.FOUND
assert "content-length" not in response.json()["headers"]
+@pytest.mark.usefixtures("async_environment")
+async def test_can_stream_if_no_redirect():
+ client = AsyncClient(dispatch=MockDispatch())
+ url = "https://example.org/redirect_301"
+ async with client.stream("GET", url, allow_redirects=False) as response:
+ assert not response.is_closed
+ assert response.status_code == codes.MOVED_PERMANENTLY
+ assert response.headers["location"] == "https://example.org/"
+
+
@pytest.mark.usefixtures("async_environment")
async def test_cannot_redirect_streaming_body():
client = AsyncClient(dispatch=MockDispatch())