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

Merge URLs properly on TestClient #2376

Merged
merged 2 commits into from
Dec 16, 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
2 changes: 1 addition & 1 deletion starlette/testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def request( # type: ignore[override]
] = httpx._client.USE_CLIENT_DEFAULT,
extensions: typing.Optional[typing.Dict[str, typing.Any]] = None,
) -> httpx.Response:
url = self.base_url.join(url)
url = self._merge_url(url)
redirect = self._choose_redirect_arg(follow_redirects, allow_redirects)
return super().request(
method,
Expand Down
2 changes: 1 addition & 1 deletion tests/middleware/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_session_cookie_subpath(test_client_factory):
)
app = Starlette(routes=[Mount("/second_app", app=second_app)])
client = test_client_factory(app, base_url="http://testserver/second_app")
response = client.post("/second_app/update_session", json={"some": "data"})
response = client.post("/update_session", json={"some": "data"})
assert response.status_code == 200
cookie = response.headers["set-cookie"]
cookie_path_match = re.search(r"; path=(\S+);", cookie)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,13 @@ def homepage(request: Request) -> JSONResponse:
client = test_client_factory(app)
response = client.get("/", headers=[("x-token", "foo"), ("x-token", "bar")])
assert response.json() == {"x-token": ["foo", "bar"]}


def test_merge_url(test_client_factory: Callable[..., TestClient]):
def homepage(request: Request) -> Response:
return Response(request.url.path)

app = Starlette(routes=[Route("/api/v1/bar", endpoint=homepage)])
client = test_client_factory(app, base_url="http://testserver/api/v1/")
response = client.get("/bar")
assert response.text == "/api/v1/bar"
Comment on lines +363 to +366
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

This test fails on master, FYI.