DecodingError,
HTTPStatusError,
InvalidURL,
- NotRedirectResponse,
RequestNotRead,
ResponseClosed,
ResponseNotRead,
yield part
self.close()
- def next(self) -> "Response":
- """
- Get the next response from a redirect response.
- """
- if not self.is_redirect:
- message = (
- "Called .next(), but the response was not a redirect. "
- "Calling code should check `response.is_redirect` first."
- )
- raise NotRedirectResponse(message)
- assert self.call_next is not None
- return self.call_next()
-
def close(self) -> None:
"""
Close the response and release the connection.
yield part
await self.aclose()
- async def anext(self) -> "Response":
- """
- Get the next response from a redirect response.
- """
- if not self.is_redirect:
- raise NotRedirectResponse(
- "Called .anext(), but the response was not a redirect. "
- "Calling code should check `response.is_redirect` first."
- )
- assert self.call_next is not None
- return await self.call_next()
-
async def aclose(self) -> None:
"""
Close the response and release the connection.
f"Scheme {request.url.scheme!r} not supported."
)
- if request.url.path == "/no_redirect":
- return httpx.Response(200)
-
- elif request.url.path == "/redirect_301":
+ if request.url.path == "/redirect_301":
status_code = httpx.codes.MOVED_PERMANENTLY
content = b"<a href='https://example.org/'>here</a>"
headers = {"location": "https://example.org/"}
return httpx.Response(200, html="<html><body>Hello, world!</body></html>")
-def test_no_redirect():
- client = httpx.Client(transport=MockTransport(redirects))
- url = "https://example.com/no_redirect"
- response = client.get(url)
- assert response.status_code == 200
- with pytest.raises(httpx.NotRedirectResponse):
- response.next()
-
-
def test_redirect_301():
client = httpx.Client(transport=MockTransport(redirects))
response = client.post("https://example.org/redirect_301")
assert len(response.history) == 1
-def test_disallow_redirects():
+def test_next_request():
client = httpx.Client(transport=MockTransport(redirects))
- response = client.post("https://example.org/redirect_303", allow_redirects=False)
+ request = client.build_request("POST", "https://example.org/redirect_303")
+ response = client.send(request, allow_redirects=False)
assert response.status_code == httpx.codes.SEE_OTHER
assert response.url == "https://example.org/redirect_303"
- assert response.is_redirect is True
- assert len(response.history) == 0
+ assert response.next_request is not None
- response = response.next()
+ response = client.send(response.next_request, allow_redirects=False)
assert response.status_code == httpx.codes.OK
assert response.url == "https://example.org/"
- assert response.is_redirect is False
- assert len(response.history) == 1
+ assert response.next_request is None
-def test_next_request():
- client = httpx.Client(transport=MockTransport(redirects))
+@pytest.mark.usefixtures("async_environment")
+async def test_async_next_request():
+ client = httpx.AsyncClient(transport=MockTransport(redirects))
request = client.build_request("POST", "https://example.org/redirect_303")
- response = client.send(request, allow_redirects=False)
+ response = await client.send(request, allow_redirects=False)
assert response.status_code == httpx.codes.SEE_OTHER
assert response.url == "https://example.org/redirect_303"
assert response.next_request is not None
- response = client.send(response.next_request, allow_redirects=False)
+ response = await client.send(response.next_request, allow_redirects=False)
assert response.status_code == httpx.codes.OK
assert response.url == "https://example.org/"
assert response.next_request is None
await client.get("https://example.org/multiple_redirects?count=21")
-@pytest.mark.usefixtures("async_environment")
-async def test_async_too_many_redirects_calling_next():
- async with httpx.AsyncClient(transport=MockTransport(redirects)) as client:
- url = "https://example.org/multiple_redirects?count=21"
- response = await client.get(url, allow_redirects=False)
- with pytest.raises(httpx.TooManyRedirects):
- while response.is_redirect:
- response = await response.anext()
-
-
def test_sync_too_many_redirects():
client = httpx.Client(transport=MockTransport(redirects))
with pytest.raises(httpx.TooManyRedirects):
client.get("https://example.org/multiple_redirects?count=21")
-def test_sync_too_many_redirects_calling_next():
- client = httpx.Client(transport=MockTransport(redirects))
- url = "https://example.org/multiple_redirects?count=21"
- response = client.get(url, allow_redirects=False)
- with pytest.raises(httpx.TooManyRedirects):
- while response.is_redirect:
- response = response.next()
-
-
def test_redirect_loop():
client = httpx.Client(transport=MockTransport(redirects))
with pytest.raises(httpx.TooManyRedirects):