From efa03ebd64b9a1927cb6d0da61784b400fa5a0de Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Sat, 16 Dec 2023 14:55:20 +0100 Subject: [PATCH] Merge URLs properly on TestClient (#2376) --- starlette/testclient.py | 2 +- tests/middleware/test_session.py | 2 +- tests/test_testclient.py | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/starlette/testclient.py b/starlette/testclient.py index 60ef322d1..04ac90a05 100644 --- a/starlette/testclient.py +++ b/starlette/testclient.py @@ -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, diff --git a/tests/middleware/test_session.py b/tests/middleware/test_session.py index 8500f7b54..dc1094117 100644 --- a/tests/middleware/test_session.py +++ b/tests/middleware/test_session.py @@ -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) diff --git a/tests/test_testclient.py b/tests/test_testclient.py index 03f27e28d..44bcd7ace 100644 --- a/tests/test_testclient.py +++ b/tests/test_testclient.py @@ -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"