From 93c424974a173d600deb2cd1fcd0e2b32b1e8a22 Mon Sep 17 00:00:00 2001 From: Justas Trimailovas Date: Thu, 16 Jan 2020 12:47:44 +0200 Subject: [PATCH 1/2] Add "raw_path" to TestClient request scope --- starlette/testclient.py | 2 ++ tests/test_requests.py | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/starlette/testclient.py b/starlette/testclient.py index 03506998c..c5bcc01a1 100644 --- a/starlette/testclient.py +++ b/starlette/testclient.py @@ -134,6 +134,7 @@ def send( scope = { "type": "websocket", "path": unquote(path), + "raw_path": path, "root_path": self.root_path, "scheme": scheme, "query_string": query.encode(), @@ -150,6 +151,7 @@ def send( "http_version": "1.1", "method": request.method, "path": unquote(path), + "raw_path": path, "root_path": self.root_path, "scheme": scheme, "query_string": query.encode(), diff --git a/tests/test_requests.py b/tests/test_requests.py index defdf3a45..7d45608e3 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -3,7 +3,7 @@ import pytest from starlette.requests import ClientDisconnect, Request, State -from starlette.responses import JSONResponse, Response +from starlette.responses import JSONResponse, PlainTextResponse, Response from starlette.testclient import TestClient @@ -178,6 +178,19 @@ def test_request_scope_interface(): assert len(request) == 3 +def test_request_raw_path(): + 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 = TestClient(app) + response = client.get("/he%2Fllo") + assert response.text == "/he/llo, /he%2Fllo" + + def test_request_without_setting_receive(): """ If Request is instantiated without the receive channel, then .body() From 39f448d76a3c04b13ba68a316d86e78630b152cc Mon Sep 17 00:00:00 2001 From: Justas Trimailovas Date: Thu, 30 Jan 2020 10:02:15 +0200 Subject: [PATCH 2/2] Return raw_path as byte string According to ASGI spec [0], connection scope's `raw_path` should be a byte string. [0] https://asgi.readthedocs.io/en/latest/specs/www.html#connection-scope --- starlette/testclient.py | 4 ++-- tests/test_requests.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/starlette/testclient.py b/starlette/testclient.py index c5bcc01a1..ba24f5151 100644 --- a/starlette/testclient.py +++ b/starlette/testclient.py @@ -134,7 +134,7 @@ def send( scope = { "type": "websocket", "path": unquote(path), - "raw_path": path, + "raw_path": path.encode(), "root_path": self.root_path, "scheme": scheme, "query_string": query.encode(), @@ -151,7 +151,7 @@ def send( "http_version": "1.1", "method": request.method, "path": unquote(path), - "raw_path": 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 7d45608e3..7ca013e90 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -188,7 +188,7 @@ async def app(scope, receive, send): client = TestClient(app) response = client.get("/he%2Fllo") - assert response.text == "/he/llo, /he%2Fllo" + assert response.text == "/he/llo, b'/he%2Fllo'" def test_request_without_setting_receive():