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

Remove deprecated allow_redirects argument from TestClient #2808

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ filterwarnings = [
"ignore: Async generator 'starlette.requests.Request.stream' was garbage collected before it had been exhausted.*:ResourceWarning",
"ignore: path is deprecated.*:DeprecationWarning:certifi",
"ignore: Use 'content=<...>' to upload raw bytes/text content.:DeprecationWarning",
"ignore: The `allow_redirects` argument is deprecated. Use `follow_redirects` instead.:DeprecationWarning",
"ignore: 'cgi' is deprecated and slated for removal in Python 3.13:DeprecationWarning",
"ignore: You seem to already have a custom sys.excepthook handler installed. I'll skip installing Trio's custom handler, but this means MultiErrors will not show full tracebacks.:RuntimeWarning",
# TODO: This warning appeared when we bumped anyio to 4.4.0.
Expand Down
66 changes: 17 additions & 49 deletions starlette/testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import queue
import sys
import typing
import warnings
from concurrent.futures import Future
from functools import cached_property
from types import GeneratorType
Expand Down Expand Up @@ -398,6 +397,7 @@ class TestClient(httpx.Client):
__test__ = False
task: Future[None]
portal: anyio.abc.BlockingPortal | None = None
redirect: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this? There's a parameter in the init

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I forgot it there


def __init__(
self,
Expand Down Expand Up @@ -445,22 +445,6 @@ def _portal_factory(self) -> typing.Generator[anyio.abc.BlockingPortal, None, No
with anyio.from_thread.start_blocking_portal(**self.async_backend) as portal:
yield portal

def _choose_redirect_arg(
self, follow_redirects: bool | None, allow_redirects: bool | None
) -> bool | httpx._client.UseClientDefault:
redirect: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT
if allow_redirects is not None:
message = "The `allow_redirects` argument is deprecated. Use `follow_redirects` instead."
warnings.warn(message, DeprecationWarning)
redirect = allow_redirects
if follow_redirects is not None:
redirect = follow_redirects
elif allow_redirects is not None and follow_redirects is not None:
raise RuntimeError( # pragma: no cover
"Cannot use both `allow_redirects` and `follow_redirects`."
)
return redirect

def request( # type: ignore[override]
self,
method: str,
Expand All @@ -474,13 +458,11 @@ def request( # type: ignore[override]
headers: httpx._types.HeaderTypes | None = None,
cookies: httpx._types.CookieTypes | None = None,
auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
follow_redirects: bool | None = None,
allow_redirects: bool | None = None,
follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
extensions: dict[str, typing.Any] | None = None,
) -> httpx.Response:
url = self._merge_url(url)
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
return super().request(
method,
url,
Expand All @@ -492,7 +474,7 @@ def request( # type: ignore[override]
headers=headers,
cookies=cookies,
auth=auth,
follow_redirects=redirect,
follow_redirects=follow_redirects,
timeout=timeout,
extensions=extensions,
)
Expand All @@ -505,19 +487,17 @@ def get( # type: ignore[override]
headers: httpx._types.HeaderTypes | None = None,
cookies: httpx._types.CookieTypes | None = None,
auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
follow_redirects: bool | None = None,
allow_redirects: bool | None = None,
follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
extensions: dict[str, typing.Any] | None = None,
) -> httpx.Response:
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
return super().get(
url,
params=params,
headers=headers,
cookies=cookies,
auth=auth,
follow_redirects=redirect,
follow_redirects=follow_redirects,
timeout=timeout,
extensions=extensions,
)
Expand All @@ -530,19 +510,17 @@ def options( # type: ignore[override]
headers: httpx._types.HeaderTypes | None = None,
cookies: httpx._types.CookieTypes | None = None,
auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
follow_redirects: bool | None = None,
allow_redirects: bool | None = None,
follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
extensions: dict[str, typing.Any] | None = None,
) -> httpx.Response:
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
return super().options(
url,
params=params,
headers=headers,
cookies=cookies,
auth=auth,
follow_redirects=redirect,
follow_redirects=follow_redirects,
timeout=timeout,
extensions=extensions,
)
Expand All @@ -555,19 +533,17 @@ def head( # type: ignore[override]
headers: httpx._types.HeaderTypes | None = None,
cookies: httpx._types.CookieTypes | None = None,
auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
follow_redirects: bool | None = None,
allow_redirects: bool | None = None,
follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
extensions: dict[str, typing.Any] | None = None,
) -> httpx.Response:
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
return super().head(
url,
params=params,
headers=headers,
cookies=cookies,
auth=auth,
follow_redirects=redirect,
follow_redirects=follow_redirects,
timeout=timeout,
extensions=extensions,
)
Expand All @@ -584,12 +560,10 @@ def post( # type: ignore[override]
headers: httpx._types.HeaderTypes | None = None,
cookies: httpx._types.CookieTypes | None = None,
auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
follow_redirects: bool | None = None,
allow_redirects: bool | None = None,
follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
extensions: dict[str, typing.Any] | None = None,
) -> httpx.Response:
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
return super().post(
url,
content=content,
Expand All @@ -600,7 +574,7 @@ def post( # type: ignore[override]
headers=headers,
cookies=cookies,
auth=auth,
follow_redirects=redirect,
follow_redirects=follow_redirects,
timeout=timeout,
extensions=extensions,
)
Expand All @@ -617,12 +591,10 @@ def put( # type: ignore[override]
headers: httpx._types.HeaderTypes | None = None,
cookies: httpx._types.CookieTypes | None = None,
auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
follow_redirects: bool | None = None,
allow_redirects: bool | None = None,
follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
extensions: dict[str, typing.Any] | None = None,
) -> httpx.Response:
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
return super().put(
url,
content=content,
Expand All @@ -633,7 +605,7 @@ def put( # type: ignore[override]
headers=headers,
cookies=cookies,
auth=auth,
follow_redirects=redirect,
follow_redirects=follow_redirects,
timeout=timeout,
extensions=extensions,
)
Expand All @@ -650,12 +622,10 @@ def patch( # type: ignore[override]
headers: httpx._types.HeaderTypes | None = None,
cookies: httpx._types.CookieTypes | None = None,
auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
follow_redirects: bool | None = None,
allow_redirects: bool | None = None,
follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
extensions: dict[str, typing.Any] | None = None,
) -> httpx.Response:
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
return super().patch(
url,
content=content,
Expand All @@ -666,7 +636,7 @@ def patch( # type: ignore[override]
headers=headers,
cookies=cookies,
auth=auth,
follow_redirects=redirect,
follow_redirects=follow_redirects,
timeout=timeout,
extensions=extensions,
)
Expand All @@ -679,19 +649,17 @@ def delete( # type: ignore[override]
headers: httpx._types.HeaderTypes | None = None,
cookies: httpx._types.CookieTypes | None = None,
auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
follow_redirects: bool | None = None,
allow_redirects: bool | None = None,
follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT,
extensions: dict[str, typing.Any] | None = None,
) -> httpx.Response:
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
return super().delete(
url,
params=params,
headers=headers,
cookies=cookies,
auth=auth,
follow_redirects=redirect,
follow_redirects=follow_redirects,
timeout=timeout,
extensions=extensions,
)
Expand Down
8 changes: 4 additions & 4 deletions tests/middleware/test_https_redirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ def homepage(request: Request) -> PlainTextResponse:
assert response.status_code == 200

client = test_client_factory(app)
response = client.get("/", allow_redirects=False)
response = client.get("/", follow_redirects=False)
assert response.status_code == 307
assert response.headers["location"] == "https://testserver/"

client = test_client_factory(app, base_url="http://testserver:80")
response = client.get("/", allow_redirects=False)
response = client.get("/", follow_redirects=False)
assert response.status_code == 307
assert response.headers["location"] == "https://testserver/"

client = test_client_factory(app, base_url="http://testserver:443")
response = client.get("/", allow_redirects=False)
response = client.get("/", follow_redirects=False)
assert response.status_code == 307
assert response.headers["location"] == "https://testserver/"

client = test_client_factory(app, base_url="http://testserver:123")
response = client.get("/", allow_redirects=False)
response = client.get("/", follow_redirects=False)
assert response.status_code == 307
assert response.headers["location"] == "https://testserver:123/"
2 changes: 1 addition & 1 deletion tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
await response(scope, receive, send)

client: TestClient = test_client_factory(app)
response = client.request("GET", "/redirect", allow_redirects=False)
response = client.request("GET", "/redirect", follow_redirects=False)
assert response.url == "http://testserver/redirect"
assert response.headers["content-length"] == "0"

Expand Down
Loading