Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/httpx2/httpx2/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1054,13 +1054,18 @@ def _send_handling_redirects(
if not response.has_redirect_location:
return response

request = self._build_redirect_request(request, response)
history = history + [response]

if follow_redirects:
request = self._build_redirect_request(request, response)
history = history + [response]
response.read()
else:
response.next_request = request
# When not following redirects, building the next request is
# best-effort: a malformed 'Location' must not discard the
# response and its headers. Leave next_request as None.
try:
response.next_request = self._build_redirect_request(request, response)
except (InvalidURL, RemoteProtocolError):
pass
return response

except BaseException as exc:
Expand Down Expand Up @@ -1892,13 +1897,18 @@ async def _send_handling_redirects(
if not response.has_redirect_location:
return response

request = self._build_redirect_request(request, response)
history = history + [response]

if follow_redirects:
request = self._build_redirect_request(request, response)
history = history + [response]
await response.aread()
else:
response.next_request = request
# When not following redirects, building the next request is
# best-effort: a malformed 'Location' must not discard the
# response and its headers. Leave next_request as None.
try:
response.next_request = self._build_redirect_request(request, response)
except (InvalidURL, RemoteProtocolError):
pass
return response

except BaseException as exc:
Expand Down
20 changes: 20 additions & 0 deletions tests/httpx2/client/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,17 @@ def test_invalid_redirect() -> None:
client.get("http://example.org/invalid_redirect", follow_redirects=True)


def test_invalid_redirect_not_followed() -> None:
# A malformed 'Location' must not discard the response when we are not
# following redirects: the response and its headers are returned as-is,
# with next_request left as None.
client = httpx2.Client(transport=httpx2.MockTransport(redirects))
response = client.get("http://example.org/invalid_redirect", follow_redirects=False)
assert response.status_code == httpx2.codes.SEE_OTHER
assert response.headers["location"] == "https://😇/"
assert response.next_request is None


def test_no_scheme_redirect() -> None:
client = httpx2.Client(transport=httpx2.MockTransport(redirects))
response = client.get("https://example.org/no_scheme_redirect", follow_redirects=True)
Expand Down Expand Up @@ -458,3 +469,12 @@ async def test_async_invalid_redirect() -> None:
async with httpx2.AsyncClient(transport=httpx2.MockTransport(redirects)) as client:
with pytest.raises(httpx2.RemoteProtocolError):
await client.get("http://example.org/invalid_redirect", follow_redirects=True)


@pytest.mark.anyio
async def test_async_invalid_redirect_not_followed() -> None:
async with httpx2.AsyncClient(transport=httpx2.MockTransport(redirects)) as client:
response = await client.get("http://example.org/invalid_redirect", follow_redirects=False)
assert response.status_code == httpx2.codes.SEE_OTHER
assert response.headers["location"] == "https://😇/"
assert response.next_request is None
Loading