Skip to content

Commit

Permalink
More clear documentation of max_redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
dvolodin7 committed Mar 20, 2024
1 parent 3139cfe commit 56065ae
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/gufo/http/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ class HttpClient(object):
user_agent: Default user agent.
Args:
max_redirects: Maximal amount of redirects. Use `None`
to disable redirect processing.
max_redirects: Set up redirects policy:
* **None**: Disable automatic redirect processing.
* **0**: Deny redirects. Will raise `RedirectError`
on 3xx response.
* **>0**: Follow redirects automatically. Will raise
`RedirectError` when redirects limit exceeded.
compression: Acceptable compression methods,
must be a combination of `DEFLATE`, `GZIP`, `BROTLI`.
Set to `None` to disable compression support.
Expand Down
10 changes: 8 additions & 2 deletions src/gufo/http/sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ class HttpClient(object):
user_agent: Default user agent.
Args:
max_redirects: Maximal amount of redirects. Use `None`
to disable redirect processing.
max_redirects: Set up redirects policy:
* **None**: Disable automatic redirect processing.
* **0**: Deny redirects. Will raise `RedirectError`
on 3xx response.
* **>0**: Follow redirects automatically. Will raise
`RedirectError` when redirects limit exceeded.
compression: Acceptable compression methods,
must be a combination of `DEFLATE`, `GZIP`, `BROTLI`.
Set to `None` to disable compression support.
Expand Down
9 changes: 9 additions & 0 deletions tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,15 @@ async def inner() -> None:
asyncio.run(inner())


def test_no_redirect_forbidden(httpd: Httpd) -> None:
async def inner() -> None:
async with HttpClient(max_redirects=0) as client:
with pytest.raises(RedirectError):
await client.get(f"{httpd.prefix}/redirect/root")

asyncio.run(inner())


@pytest.mark.parametrize("x", [HttpError, RedirectError])
def test_redirect_to_loop(httpd: Httpd, x: Type[BaseException]) -> None:
async def inner() -> None:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ def test_no_redirect_to_root(httpd: Httpd) -> None:
assert resp.status == 302


def test_redirects_forbidden(httpd: Httpd) -> None:
with HttpClient(max_redirects=0) as client, pytest.raises(RedirectError):
client.get(f"{httpd.prefix}/redirect/root")


@pytest.mark.parametrize("x", [HttpError, RedirectError])
def test_redirect_to_loop(httpd: Httpd, x: Type[BaseException]) -> None:
with HttpClient() as client, pytest.raises(x):
Expand Down

0 comments on commit 56065ae

Please sign in to comment.