Skip to content

Commit

Permalink
Fix serve directory path.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeanwallace committed Mar 25, 2024
1 parent 44adb1c commit b595cab
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
21 changes: 11 additions & 10 deletions jinjabread/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ def __init__(self, config):
self.config = config

def dispatch_request(self, request):
path = Path(request.path)
url_path = Path(request.path)
file_path = self.config.output_dir / url_path.relative_to("/")

if path.name == "index.html":
return redirect(path.parent)
# Clean up URL path.
if url_path.name == "index.html":
return redirect(url_path.parent.as_posix().removesuffix("/") + "/")
if url_path.suffix == ".html":
return redirect(url_path.with_suffix("").as_posix())
if file_path.is_dir() and not request.path.endswith("/"):
return redirect(url_path.as_posix() + "/")

if path.suffix == ".html":
return redirect(path.with_suffix(""))

file_path = self.config.output_dir / path.relative_to("/")
if (not file_path.exists() or file_path.is_dir()) and file_path.with_suffix(
".html"
).exists():
# Clean up file path.
if not file_path.exists() and file_path.with_suffix(".html").exists():
file_path = file_path.with_suffix(".html")
elif file_path.is_dir():
file_path /= "index.html"
Expand Down
32 changes: 31 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def test_response(self):
)
self.assertHtmlEqual("", response.get_data(as_text=True))

def test_redirect_index_path(self):
def test_redirect_root_index_path(self):
index_file = self.working_dir / "content" / "index.html"
index_file.parent.mkdir(parents=True)
index_file.touch()
Expand All @@ -502,6 +502,21 @@ def test_redirect_index_path(self):
self.assertEqual(302, response.status_code)
self.assertEqual("/", response.headers.get("Location"))

def test_redirect_dir_index_path(self):
index_file = self.working_dir / "content" / "posts" / "index.html"
index_file.parent.mkdir(parents=True)
index_file.touch()

config = jinjabread.Config.load()
site = jinjabread.Site(config)
site.generate()

client = Client(jinjabread.App(config))
response = client.get("/posts/index.html")

self.assertEqual(302, response.status_code)
self.assertEqual("/posts/", response.headers.get("Location"))

def test_redirect_path_with_suffix(self):
index_file = self.working_dir / "content" / "about.html"
index_file.parent.mkdir(parents=True)
Expand All @@ -516,3 +531,18 @@ def test_redirect_path_with_suffix(self):

self.assertEqual(302, response.status_code)
self.assertEqual("/about", response.headers.get("Location"))

def test_redirect_dir_path_with_missing_slash(self):
index_file = self.working_dir / "content" / "posts" / "index.html"
index_file.parent.mkdir(parents=True)
index_file.touch()

config = jinjabread.Config.load()
site = jinjabread.Site(config)
site.generate()

client = Client(jinjabread.App(config))
response = client.get("/posts")

self.assertEqual(302, response.status_code)
self.assertEqual("/posts/", response.headers.get("Location"))

0 comments on commit b595cab

Please sign in to comment.