Skip to content

Commit

Permalink
Fix TestClient for extra unquote in query parameters (#1952) (#1953)
Browse files Browse the repository at this point in the history
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
  • Loading branch information
azogue and Kludex authored Nov 16, 2022
1 parent bf5d735 commit 24887d6
Show file tree
Hide file tree
Showing 2 changed files with 14 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 @@ -196,10 +196,10 @@ def __init__(

def handle_request(self, request: httpx.Request) -> httpx.Response:
scheme = request.url.scheme
netloc = unquote(request.url.netloc.decode(encoding="ascii"))
netloc = request.url.netloc.decode(encoding="ascii")
path = request.url.path
raw_path = request.url.raw_path
query = unquote(request.url.query.decode(encoding="ascii"))
query = request.url.query.decode(encoding="ascii")

default_port = {"http": 80, "ws": 80, "https": 443, "wss": 443}[scheme]

Expand Down
13 changes: 12 additions & 1 deletion tests/test_testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.responses import JSONResponse
from starlette.responses import JSONResponse, Response
from starlette.routing import Route
from starlette.websockets import WebSocket, WebSocketDisconnect

Expand Down Expand Up @@ -240,3 +240,14 @@ async def app(scope, receive, send):
client = test_client_factory(app)
response = client.get("/")
assert response.json() == {"host": "testclient", "port": 50000}


@pytest.mark.parametrize("param", ("2020-07-14T00:00:00+00:00", "España", "voilà"))
def test_query_params(test_client_factory, param: str):
def homepage(request):
return Response(request.query_params["param"])

app = Starlette(routes=[Route("/", endpoint=homepage)])
client = test_client_factory(app)
response = client.get("/", params={"param": param})
assert response.text == param

0 comments on commit 24887d6

Please sign in to comment.