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

Add raw_path to TestClient scope #1445

Merged
merged 4 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions starlette/testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
15 changes: 14 additions & 1 deletion tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down