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

fix(testclient): exclude query sting from raw_path #2716

Merged
merged 6 commits into from
Nov 18, 2024
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
4 changes: 2 additions & 2 deletions starlette/testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def handle_request(self, request: httpx.Request) -> httpx.Response:
scope = {
"type": "websocket",
"path": unquote(path),
"raw_path": raw_path,
"raw_path": raw_path.split(b"?", 1)[0],
"root_path": self.root_path,
"scheme": scheme,
"query_string": query.encode(),
Expand All @@ -300,7 +300,7 @@ def handle_request(self, request: httpx.Request) -> httpx.Response:
"http_version": "1.1",
"method": request.method,
"path": unquote(path),
"raw_path": raw_path,
"raw_path": raw_path.split(b"?", 1)[0],
"root_path": self.root_path,
"scheme": scheme,
"query_string": query.encode(),
Expand Down
24 changes: 24 additions & 0 deletions tests/test_testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,27 @@ def homepage(request: Request) -> Response:
client = test_client_factory(app, base_url="http://testserver/api/v1/")
response = client.get("/bar")
assert response.text == "/api/v1/bar"


def test_raw_path_with_querystring(test_client_factory: TestClientFactory) -> None:
async def app(scope: Scope, receive: Receive, send: Send) -> None:
response = Response(scope.get("raw_path"))
await response(scope, receive, send)

client = test_client_factory(app)
response = client.get("/hello-world", params={"foo": "bar"})
assert response.content == b"/hello-world"


def test_websocket_raw_path_without_params(test_client_factory: TestClientFactory) -> None:
async def app(scope: Scope, receive: Receive, send: Send) -> None:
websocket = WebSocket(scope, receive=receive, send=send)
await websocket.accept()
raw_path = scope.get("raw_path")
assert raw_path is not None
await websocket.send_bytes(raw_path)

client = test_client_factory(app)
with client.websocket_connect("/hello-world", params={"foo": "bar"}) as websocket:
data = websocket.receive_bytes()
assert data == b"/hello-world"