diff --git a/starlette/testclient.py b/starlette/testclient.py index c951767b4..7567d18fd 100644 --- a/starlette/testclient.py +++ b/starlette/testclient.py @@ -162,6 +162,7 @@ def send( scope = { "type": "websocket", "path": unquote(path), + "raw_path": path.encode(), "root_path": self.root_path, "scheme": scheme, "query_string": query.encode(), @@ -178,6 +179,7 @@ def send( "http_version": "1.1", "method": request.method, "path": unquote(path), + "raw_path": path.encode(), "root_path": self.root_path, "scheme": scheme, "query_string": query.encode(), diff --git a/tests/test_requests.py b/tests/test_requests.py index d7c69fbeb..e535e7790 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -2,7 +2,7 @@ import pytest from starlette.requests import ClientDisconnect, Request, State -from starlette.responses import JSONResponse, Response +from starlette.responses import JSONResponse, PlainTextResponse, Response def test_request_url(test_client_factory): @@ -176,6 +176,19 @@ def test_request_scope_interface(): assert len(request) == 3 +def test_request_raw_path(test_client_factory): + async def app(scope, receive, send): + request = Request(scope, receive) + path = request.scope["path"] + raw_path = request.scope["raw_path"] + response = PlainTextResponse(f"{path}, {raw_path}") + await response(scope, receive, send) + + client = test_client_factory(app) + response = client.get("/he%2Fllo") + assert response.text == "/he/llo, b'/he%2Fllo'" + + def test_request_without_setting_receive(test_client_factory): """ If Request is instantiated without the receive channel, then .body()