This commit is contained in:
Nicolas Des Aunais 2026-03-01 03:10:40 -08:00 committed by GitHub
commit f4eaf174a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 5 deletions

View File

@ -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()

View File

@ -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