Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow global follow_redirects option. #2207

Merged
merged 1 commit into from
Jul 6, 2023
Merged
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: 2 additions & 1 deletion starlette/testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ def __init__(
backend_options: typing.Optional[typing.Dict[str, typing.Any]] = None,
cookies: httpx._client.CookieTypes = None,
headers: typing.Dict[str, str] = None,
follow_redirects: bool = True,
) -> None:
self.async_backend = _AsyncBackend(
backend=backend, backend_options=backend_options or {}
Expand Down Expand Up @@ -409,7 +410,7 @@ def __init__(
base_url=base_url,
headers=headers,
transport=transport,
follow_redirects=True,
follow_redirects=follow_redirects,
cookies=cookies,
)

Expand Down
25 changes: 24 additions & 1 deletion tests/test_testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.responses import JSONResponse, Response
from starlette.responses import JSONResponse, RedirectResponse, Response
from starlette.routing import Route
from starlette.testclient import TestClient
from starlette.websockets import WebSocket, WebSocketDisconnect
Expand Down Expand Up @@ -319,3 +319,26 @@ async def app(scope, receive, send):
response = client.get("/")
cookie_set = len(response.cookies) == 1
assert cookie_set == ok


def test_forward_follow_redirects(test_client_factory):
async def app(scope, receive, send):
if "/ok" in scope["path"]:
response = Response("ok")
else:
response = RedirectResponse("/ok")
await response(scope, receive, send)

client = test_client_factory(app, follow_redirects=True)
response = client.get("/")
assert response.status_code == 200


def test_forward_nofollow_redirects(test_client_factory):
async def app(scope, receive, send):
response = RedirectResponse("/ok")
await response(scope, receive, send)

client = test_client_factory(app, follow_redirects=False)
response = client.get("/")
assert response.status_code == 307