kopia lustrzana https://github.com/simonw/datasette
404s ending in slash redirect to remove that slash, closes #309
rodzic
3b53eea382
commit
97ae66ccab
|
@ -468,8 +468,16 @@ class Datasette:
|
|||
RowView.as_view(self),
|
||||
"/<db_name:[^/]+>/<table:[^/]+?>/<pk_path:[^/]+?><as_format:(\.jsono?)?$>",
|
||||
)
|
||||
|
||||
self.register_custom_units()
|
||||
# On 404 with a trailing slash redirect to path without that slash:
|
||||
@app.middleware("response")
|
||||
def redirect_on_404_with_trailing_slash(request, original_response):
|
||||
print("redirect_on_404_with_trailing_slash", request, original_response)
|
||||
if original_response.status == 404 and request.path.endswith("/"):
|
||||
path = request.path.rstrip("/")
|
||||
if request.query_string:
|
||||
path = "{}?{}".format(path, request.query_string)
|
||||
return response.redirect(path)
|
||||
|
||||
@app.exception(Exception)
|
||||
def on_exception(request, exception):
|
||||
|
|
|
@ -697,3 +697,13 @@ def inner_html(soup):
|
|||
# This includes the parent tag - so remove that
|
||||
inner_html = html.split('>', 1)[1].rsplit('<', 1)[0]
|
||||
return inner_html.strip()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('path,expected_redirect', [
|
||||
('/fixtures/', '/fixtures'),
|
||||
('/fixtures/simple_view/', '/fixtures/simple_view'),
|
||||
])
|
||||
def test_404_trailing_slash_redirect(app_client, path, expected_redirect):
|
||||
response = app_client.get(path, allow_redirects=False)
|
||||
assert 302 == response.status
|
||||
assert expected_redirect == response.headers["Location"]
|
||||
|
|
Ładowanie…
Reference in New Issue