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
3 changes: 3 additions & 0 deletions asks/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class AsksException(Exception):


class TooManyRedirects(AsksException):
def __init__(self, response):
super().__init__("Max redirects exceeded!")
self.response = response
pass


Expand Down
6 changes: 5 additions & 1 deletion asks/request_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ async def _request_io(self, h11_request, h11_body, h11_connection):
# check redirects
if self.method != 'HEAD':
if self.max_redirects < 0:
raise TooManyRedirects
# The most recent response is already on this history stack at this point
# Pop it and set it's history to the remaining stack.
response_obj = self.history_objects.pop()
response_obj.history = self.history_objects
raise TooManyRedirects(response_obj)
response_obj = await self._redirect(response_obj)
response_obj.history = self.history_objects

Expand Down
8 changes: 5 additions & 3 deletions tests/test_anyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ async def test_http_redirect(server):
)
@curio_run
async def test_http_max_redirect_error(server):
with pytest.raises(TooManyRedirects):
await asks.get(server.http_test_url + "/redirect_max", max_redirects=1)

with pytest.raises(TooManyRedirects) as err:
r = await asks.get(server.http_test_url + "/redirect_max", max_redirects=1)
resp = err.value.response
assert resp.history[0].url == server.http_test_url + "/redirect_max"
assert len(err.value.response.history) == 1

@Server(
_TEST_LOC,
Expand Down