Skip to content
Myr

Fix for streaming a redirect response body with allow_redirects=False (#766)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by GitHub · parent be82404 · Browse files · 2 files+23 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+23 added−3 removed

  • tests:+19 added−1 removed
  • other:+4 added−2 removed

2 files+23 added−3 removed

modified:httpx/client.py+4 added-2 removed
620 unmodified lines
621622 if not response.is_redirect:623 return response624625 if allow_redirects:625 response.read()626 response.read()627 request = self.build_redirect_request(request, response)628 history = history + [response]629630 if not allow_redirects:
515 unmodified lines
11461147 if not response.is_redirect:1148 return response11491150 if allow_redirects:1149 await response.aread()1151 await response.aread()1152 request = self.build_redirect_request(request, response)1153 history = history + [response]11541155 if not allow_redirects:
312 unmodified lines
modified:tests/client/test_redirects.py+19 added-1 removed
13 unmodified lines
14 TooManyRedirects,15 codes,16)17from httpx.config import CertTypes, TimeoutTypes, VerifyTypes18from httpx.content_streams import AsyncIteratorStream19from httpx.dispatch.base import AsyncDispatcher202122class MockDispatch(AsyncDispatcher):
7 unmodified lines
30 if request.url.path == "/no_redirect":31 return Response(codes.OK, request=request)3233 elif request.url.path == "/redirect_301":3435 async def body():36 yield b"<a href='https://example.org/'>here</a>"3738 status_code = codes.MOVED_PERMANENTLY39 headers = {"location": "https://example.org/"}35 return Response(status_code, headers=headers, request=request)40 stream = AsyncIteratorStream(aiterator=body())41 return Response(42 status_code, stream=stream, headers=headers, request=request43 )4445 elif request.url.path == "/redirect_302":46 status_code = codes.FOUND47 headers = {"location": "https://example.org/"}
245 unmodified lines
293 assert "content-length" not in response.json()["headers"]294295296@pytest.mark.usefixtures("async_environment")297async def test_can_stream_if_no_redirect():298 client = AsyncClient(dispatch=MockDispatch())299 url = "https://example.org/redirect_301"300 async with client.stream("GET", url, allow_redirects=False) as response:301 assert not response.is_closed302 assert response.status_code == codes.MOVED_PERMANENTLY303 assert response.headers["location"] == "https://example.org/"304305306@pytest.mark.usefixtures("async_environment")307async def test_cannot_redirect_streaming_body():308 client = AsyncClient(dispatch=MockDispatch())309 url = "https://example.org/redirect_body"310
82 unmodified lines