Skip to content

Commit

Permalink
use quote instead of quote_plus for RedirectResponse location header (#…
Browse files Browse the repository at this point in the history
…1164)

* use quote instead of quote_plus for RedirectResponse location header

adjust safe characters: rem. duplicate & symbol

add test for redirect quoting

* remove unused import

Co-authored-by: Jamie Hewland <jhewland@gmail.com>
  • Loading branch information
falkben and JayH5 committed Apr 14, 2021
1 parent 995d70c commit f997938
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions starlette/responses.py
Expand Up @@ -7,7 +7,7 @@
import typing
from email.utils import formatdate
from mimetypes import guess_type as mimetypes_guess_type
from urllib.parse import quote, quote_plus
from urllib.parse import quote

from starlette.background import BackgroundTask
from starlette.concurrency import iterate_in_threadpool, run_until_first_complete
Expand Down Expand Up @@ -178,7 +178,7 @@ def __init__(
super().__init__(
content=b"", status_code=status_code, headers=headers, background=background
)
self.headers["location"] = quote_plus(str(url), safe=":/%#?&=@[]!$&'()*+,;")
self.headers["location"] = quote(str(url), safe=":/%#?=@[]!$&'()*+,;")


class StreamingResponse(Response):
Expand Down
14 changes: 14 additions & 0 deletions tests/test_responses.py
Expand Up @@ -60,6 +60,20 @@ async def app(scope, receive, send):
assert response.url == "http://testserver/"


def test_quoting_redirect_response():
async def app(scope, receive, send):
if scope["path"] == "/I ♥ Starlette/":
response = Response("hello, world", media_type="text/plain")
else:
response = RedirectResponse("/I ♥ Starlette/")
await response(scope, receive, send)

client = TestClient(app)
response = client.get("/redirect")
assert response.text == "hello, world"
assert response.url == "http://testserver/I%20%E2%99%A5%20Starlette/"


def test_streaming_response():
filled_by_bg_task = ""

Expand Down

0 comments on commit f997938

Please sign in to comment.