From: Tom Christie Date: Fri, 17 Jan 2020 09:45:37 +0000 (+0000) Subject: Fix for streaming a redirect response body with allow_redirects=False (#766) X-Git-Tag: 0.11.1~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5a63540e8ab35209849894819ac731c836fdcf27;p=thirdparty%2Fhttpx.git Fix for streaming a redirect response body with allow_redirects=False (#766) --- diff --git a/httpx/client.py b/httpx/client.py index 9eec6641..590d5d0e 100644 --- a/httpx/client.py +++ b/httpx/client.py @@ -622,7 +622,8 @@ class Client(BaseClient): if not response.is_redirect: return response - response.read() + if allow_redirects: + response.read() request = self.build_redirect_request(request, response) history = history + [response] @@ -1146,7 +1147,8 @@ class AsyncClient(BaseClient): 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] diff --git a/tests/client/test_redirects.py b/tests/client/test_redirects.py index a48e478e..eab44f01 100644 --- a/tests/client/test_redirects.py +++ b/tests/client/test_redirects.py @@ -15,6 +15,7 @@ from httpx import ( codes, ) from httpx.config import CertTypes, TimeoutTypes, VerifyTypes +from httpx.content_streams import AsyncIteratorStream from httpx.dispatch.base import AsyncDispatcher @@ -30,9 +31,16 @@ class MockDispatch(AsyncDispatcher): return Response(codes.OK, request=request) elif request.url.path == "/redirect_301": + + async def body(): + yield b"here" + 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 @@ -285,6 +293,16 @@ async def test_no_body_redirect(): 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())