Skip to content

Commit

Permalink
Preserve query string in % to - redirects, refs #1650
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Mar 7, 2022
1 parent d714c67 commit 020effe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 2 additions & 0 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,8 @@ async def handle_404(self, request, send, exception=None):
# Try the same path but with "%" replaced by "-"
# and "-" replaced with "-2D"
new_path = request.path.replace("-", "-2D").replace("%", "-")
if request.query_string:
new_path += "?{}".format(request.query_string)
await asgi_send_redirect(send, new_path)
return
# If URL has a trailing slash, redirect to URL without it
Expand Down
17 changes: 14 additions & 3 deletions tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,18 @@ def test_no_alternate_url_json(app_client, path):
)


def test_redirect_percent_encoding_to_dash_encoding(app_client):
response = app_client.get("/fivethirtyeight/twitter-ratio%2Fsenators")
@pytest.mark.parametrize(
"path,expected",
(
(
"/fivethirtyeight/twitter-ratio%2Fsenators",
"/fivethirtyeight/twitter-2Dratio-2Fsenators",
),
# query string should be preserved
("/foo/bar%2Fbaz?id=5", "/foo/bar-2Fbaz?id=5"),
),
)
def test_redirect_percent_encoding_to_dash_encoding(app_client, path, expected):
response = app_client.get(path)
assert response.status == 302
assert response.headers["location"] == "/fivethirtyeight/twitter-2Dratio-2Fsenators"
assert response.headers["location"] == expected

0 comments on commit 020effe

Please sign in to comment.