Skip to content

Commit

Permalink
fix(testclient): exclude query sting from raw_path before passing to app
Browse files Browse the repository at this point in the history
fix #2692
  • Loading branch information
raptium committed Oct 4, 2024
1 parent e116840 commit f242cda
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
4 changes: 2 additions & 2 deletions starlette/testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,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 @@ -304,7 +304,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
31 changes: 30 additions & 1 deletion tests/test_testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.requests import Request
from starlette.responses import JSONResponse, RedirectResponse, Response
from starlette.responses import JSONResponse, PlainTextResponse, RedirectResponse, Response
from starlette.routing import Route
from starlette.testclient import ASGIInstance, TestClient
from starlette.types import ASGIApp, Receive, Scope, Send
Expand Down Expand Up @@ -378,3 +378,32 @@ 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:
raw_path = scope.get("raw_path")
assert raw_path is not None
response = PlainTextResponse(f"raw_path: {raw_path}")
await response(scope, receive, send)

client = test_client_factory(app)
response = client.get("/hello world", params={"foo": "bar"})
assert response.text == "raw_path: b'/hello%20world'"


def test_websocket_raw_path_with_querystring(test_client_factory: TestClientFactory) -> None:
def app(scope: Scope) -> ASGIInstance:
async def asgi(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_text(f"raw_path: {raw_path}")

return asgi

client = test_client_factory(app)
with client.websocket_connect("/hello%20world?foo=bar") as websocket:
data = websocket.receive_text()
assert data == "raw_path: b'/hello%20world'"

0 comments on commit f242cda

Please sign in to comment.