diff --git a/httpx/_client.py b/httpx/_client.py index 13cd9336..84a9f869 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -980,13 +980,14 @@ class Client(BaseClient): try: for hook in self._event_hooks["response"]: hook(response) - response.history = list(history) + transport_history = response.history + response.history = list(history) + transport_history if not response.has_redirect_location: return response request = self._build_redirect_request(request, response) - history = history + [response] + history = history + transport_history + [response] if follow_redirects: response.read() @@ -1695,14 +1696,14 @@ class AsyncClient(BaseClient): try: for hook in self._event_hooks["response"]: await hook(response) - - response.history = list(history) + transport_history = response.history + response.history = list(history) + transport_history if not response.has_redirect_location: return response request = self._build_redirect_request(request, response) - history = history + [response] + history = history + transport_history + [response] if follow_redirects: await response.aread() diff --git a/tests/client/test_redirects.py b/tests/client/test_redirects.py index f6582713..13f1c81f 100644 --- a/tests/client/test_redirects.py +++ b/tests/client/test_redirects.py @@ -107,6 +107,12 @@ def redirects(request: httpx.Request) -> httpx.Response: headers = {"location": "market://details?id=42"} return httpx.Response(status_code, headers=headers) + elif request.url.path == "/redirect_303_with_history": + status_code = httpx.codes.SEE_OTHER + headers = {"location": "https://example.org/"} + history = [httpx.Response(status_code, headers=headers)] + return httpx.Response(status_code, headers=headers, history=history) + if request.method == "HEAD": return httpx.Response(200) @@ -445,3 +451,25 @@ async def test_async_invalid_redirect(): await client.get( "http://example.org/invalid_redirect", follow_redirects=True ) + + +def test_redirect_303_history(): + client = httpx.Client(transport=httpx.MockTransport(redirects)) + response = client.get( + "https://example.org/redirect_303_with_history", follow_redirects=True + ) + assert response.status_code == httpx.codes.OK + assert response.url == "https://example.org/" + assert len(response.history) == 2 + + +@pytest.mark.anyio +async def test_async_redirect_history(): + async with httpx.AsyncClient(transport=httpx.MockTransport(redirects)) as client: + request = client.build_request( + "POST", "https://example.org/redirect_303_with_history" + ) + response = await client.send(request, follow_redirects=True) + assert response.status_code == httpx.codes.OK + assert response.url == "https://example.org/" + assert len(response.history) == 2