add the ability to redirect a file-looking request to a dir

if the client has requested /foo, and foo is actually a directory,
this redirects the client to /foo/
This commit is contained in:
2020-06-19 19:58:12 -05:00
parent cf8f0325a2
commit 0f7495bf2b
3 changed files with 62 additions and 2 deletions

View File

@@ -35,3 +35,20 @@ def test_page_has_modified_timestamp(client):
response = client.get('/')
assert response.status_code == 200
assert re.search(r'Last modified: ....-..-.. ..:..:.. ...', response.data.decode()) is not None
def test_that_page_request_redirects_to_directory(client):
"""Test that a request to /foo reirects to /foo/, if foo is a directory.
This might be useful in cases where a formerly page-only page has been
converted to a directory with subpages.
"""
response = client.get('/subdir')
assert response.status_code == 301
def test_that_dir_request_does_not_redirect(client):
"""Test that a request to /foo/ serves the index page, if foo is a directory."""
response = client.get('/subdir/')
assert response.status_code == 200
assert b'another page' in response.data

View File

@@ -1,5 +1,5 @@
"""Unit test helper methods."""
from incorporealcms.pages import generate_parent_navs, resolve_page_file
from incorporealcms.pages import generate_parent_navs, is_file_path_actually_dir_path, resolve_page_file
def test_resolve_page_file_dir_to_index():
@@ -50,3 +50,27 @@ def test_generate_page_navs_subdir_with_title_parsing_real_page(app):
('SUB!', '/subdir-with-title/'),
('/subdir-with-title/page', '/subdir-with-title/page')
]
def test_is_file_path_actually_dir_path_valid_file_is_yes(app):
"""Test that a file request for what's actually a directory is detected as such."""
with app.app_context():
assert is_file_path_actually_dir_path('/subdir')
def test_is_file_path_actually_dir_path_valid_dir_is_no(app):
"""Test that a directory request is still a directory request."""
with app.app_context():
assert not is_file_path_actually_dir_path('/subdir/')
def test_is_file_path_actually_dir_path_nonsense_file_is_no(app):
"""Test that requests to nonsense file-looking paths aren't treated as dirs."""
with app.app_context():
assert not is_file_path_actually_dir_path('/antphnathpnthapnthsnthax')
def test_is_file_path_actually_dir_path_nonsense_dir_is_no(app):
"""Test that a directory request is a directory request even if the dir doesn't exist."""
with app.app_context():
assert not is_file_path_actually_dir_path('/antphnathpnthapnthsnthax/')