Skip to content

Commit

Permalink
Fix double slash, fix redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
rcmonsayac committed Dec 12, 2023
1 parent 04a762d commit c209722
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 17 deletions.
7 changes: 3 additions & 4 deletions lib/literature/helpers/meta_tag_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ defmodule Literature.MetaTagHelpers do
href:
current_url
|> String.replace("/page/#{page_number}", "")
|> IO.inspect()
|> put_page_number(page_number + 1)
|> IO.inspect()
)
]
end
Expand All @@ -196,7 +194,8 @@ defmodule Literature.MetaTagHelpers do
|> put_page_number(page_number - 1)

Check warning on line 194 in lib/literature/helpers/meta_tag_helpers.ex

View check run for this annotation

Codecov / codecov/patch

lib/literature/helpers/meta_tag_helpers.ex#L193-L194

Added lines #L193 - L194 were not covered by tests

defp put_page_number(current_url, page_number) do
current_url <>
"#{if String.ends_with?(current_url, "/"), do: "", else: "/"}page/#{page_number}"
current_url
|> String.replace_suffix("/", "")
|> Kernel.<>("/page/#{page_number}")
end
end
14 changes: 9 additions & 5 deletions lib/literature/plugs/redirect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ defmodule Literature.Plugs.Redirect do
{base_path, current_path} =
parse_paths(conn.request_path, root_path)

case Literature.get_redirect!(publication_slug: publication_slug, from: current_path) do
case Literature.get_redirect!(
publication_slug: publication_slug,
from: current_path
) do
nil ->
conn

redirect ->
new_path = "#{base_path}#{root_path}#{redirect.to}"
new_path = String.replace_suffix("#{base_path}#{root_path}", "/", "") <> redirect.to

conn
|> put_status(redirect.type)
Expand All @@ -42,9 +45,10 @@ defmodule Literature.Plugs.Redirect do

# Match empty path with /
current_path =
case current_path do
"" -> "/"
_ -> current_path
if String.starts_with?(current_path, "/") do
current_path
else
"/#{current_path}"
end

{base_path, current_path}
Expand Down
6 changes: 1 addition & 5 deletions lib/literature/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,7 @@ defmodule Literature.Router do
scope = "/#{if root?, do: "", else: "#{publication_slug}"}"

root_path =
if path == "/" do
scope
else
"#{path}#{scope}" |> String.replace_suffix("/", "")
end
String.replace_suffix(path, "/", "") <> scope

route_opts = [
private: %{
Expand Down
69 changes: 66 additions & 3 deletions test/literature/live/blog_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,76 @@ defmodule Literature.BlogLiveTest do
end
end

describe "Error view" do
describe "On root routes" do
setup do
publication = publication_fixture(name: "Error View", description: "Blog description")
publication =
publication_fixture(
name: "On root",
description: "On root description"
)

author =
author_fixture(publication_id: publication.id, name: "Author", bio: "Author description")

tag =
tag_fixture(publication_id: publication.id, name: "Tag", description: "Tag description")

post =
post_fixture(
publication_id: publication.id,
authors_ids: [author.id],
tags_ids: [tag.id],
html: ["<p>content</p>"],
locales: [
%{locale: "en", url: "http://example.com/en"},
%{locale: "de", url: "http://example.com/de"}
]
)

%{publication: publication, author: author, tag: tag, post: post}
end

test "redirects on root routes", %{conn: conn, publication: publication} do
redirect =
redirect_fixture(publication_id: publication.id, from: "/some-post", to: "/", type: 302)

conn = get(conn, Routes.on_root_path(conn, :show, "some-post"))
assert redirected_to(conn, redirect.type) == "/"
end

test "lists blog posts page 2", %{
conn: conn,
publication: publication,
tag: tag,
author: author
} do
for i <- 1..20 do
post_fixture(
title: "Post #{i}",
publication_id: publication.id,
authors_ids: [author.id],
tags_ids: [tag.id]
)
end

assert {:ok, _view, html} = live(conn, Routes.on_root_path(conn, :index, 2))

prev_url = @endpoint.url()
next_url = @endpoint.url() <> Routes.on_root_path(conn, :index, 3)

%{publication: publication}
assert get_element(
html,
"link[href='#{prev_url}'][rel='prev']"
)

assert get_element(
html,
"link[href='#{next_url}'][rel='next']"
)
end
end

describe "Error view" do
test "Display 404 page when page not found", %{conn: conn} do
{:ok, view, _html} = live(conn, Routes.error_view_path(conn, :show, "page-not-exists"))
html = render(view)
Expand Down

0 comments on commit c209722

Please sign in to comment.