Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lib/jola_dev_web/plugs/blog_redirect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ defmodule JolaDevWeb.Plugs.BlogRedirect do
ids = JolaDev.Blog.ids()
path = strip_path(conn.request_path)

if path in ids do
conn
|> put_resp_header("location", "https://jola.dev/posts/" <> path)
|> send_resp(:moved_permanently, "")
|> halt()
else
conn
end
path =
if path in ids do
"posts/" <> path
else
path
end

conn
|> put_resp_header("location", "https://jola.dev/" <> path)
|> send_resp(:moved_permanently, "")
|> halt()
else
conn
end
Expand Down
14 changes: 8 additions & 6 deletions test/jola_dev_web/plugs/blog_redirect_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ defmodule JolaDevWeb.Plugs.BlogRedirectTest do
assert conn.halted
end

test "does not redirect invalid paths on blog.jola.dev", %{conn: conn} do
original_conn =
test "redirects invalid paths on blog.jola.dev to jola.dev without /posts prefix", %{
conn: conn
} do
conn =
conn
|> Map.put(:host, "blog.jola.dev")
|> Map.put(:request_path, "/non-existent-post")
|> BlogRedirect.call([])

result_conn = BlogRedirect.call(original_conn, [])

assert result_conn == original_conn
refute result_conn.halted
assert conn.status == 301
assert get_resp_header(conn, "location") == ["https://jola.dev/non-existent-post"]
assert conn.halted
end

test "does not redirect on non-blog.jola.dev hosts", %{conn: conn} do
Expand Down