]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Add `raw_path` to `TestClient` scope (#1445)
authorMarcelo Trylesinski <marcelotryle@gmail.com>
Mon, 31 Jan 2022 09:31:40 +0000 (10:31 +0100)
committerGitHub <noreply@github.com>
Mon, 31 Jan 2022 09:31:40 +0000 (10:31 +0100)
* Add "raw_path" to TestClient request scope

* 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

Co-authored-by: Justas Trimailovas <j.trimailovas@gmail.com>
starlette/testclient.py
tests/test_requests.py

index c951767b46ea3c533e7d73b4e7e627d7f605ec0b..7567d18fd69acb20f5aced1dc95671ad5120a482 100644 (file)
@@ -162,6 +162,7 @@ class _ASGIAdapter(requests.adapters.HTTPAdapter):
             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 @@ class _ASGIAdapter(requests.adapters.HTTPAdapter):
             "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(),
index d7c69fbeb2a02b95e2a5cac1ce8a6647d123282c..e535e779031f8cf0de1d3529e3f569db6f251fb6 100644 (file)
@@ -2,7 +2,7 @@ import anyio
 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()