Preserve query string in % to - redirects, refs #1650

pull/1495/merge
Simon Willison 2022-03-07 08:18:07 -08:00
rodzic d714c67d65
commit 020effe47b
2 zmienionych plików z 16 dodań i 3 usunięć

Wyświetl plik

@ -1216,6 +1216,8 @@ class DatasetteRouter:
# 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

Wyświetl plik

@ -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