From 33103243f5c33714a437536577d76820fe491a06 Mon Sep 17 00:00:00 2001 From: Nicolas Des Aunais Date: Fri, 29 Mar 2024 15:52:04 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20maintain=20transport=20response?= =?UTF-8?q?=20history?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- httpx/_client.py | 11 ++++++----- tests/client/test_redirects.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/httpx/_client.py b/httpx/_client.py index e2c6702e..b5198c84 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() @@ -1727,14 +1728,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..03cba5a5 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 From 58fb2e9be9a90df663508c94ec95ebcab2d8d92b Mon Sep 17 00:00:00 2001 From: Nicolas Des Aunais Date: Fri, 29 Mar 2024 15:56:24 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8E=A8=20fix=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/client/test_redirects.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/client/test_redirects.py b/tests/client/test_redirects.py index 03cba5a5..13f1c81f 100644 --- a/tests/client/test_redirects.py +++ b/tests/client/test_redirects.py @@ -111,7 +111,7 @@ def redirects(request: httpx.Request) -> httpx.Response: 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]) + return httpx.Response(status_code, headers=headers, history=history) if request.method == "HEAD": return httpx.Response(200)