Skip to content

Commit

Permalink
Merge URLs properly on TestClient (#2376)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex authored Dec 16, 2023
1 parent 9a213c1 commit efa03eb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
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"

0 comments on commit efa03eb

Please sign in to comment.