Skip to content

Commit

Permalink
feat(SiteWeb.Router): add /charlie/* redirects (#1782)
Browse files Browse the repository at this point in the history
* feat(SiteWeb.WwwRedirector): support optional host

* feat(SiteWeb.WwwRedirector): use conn.path_params

* feat(SiteWeb.Router): add /charlie/* redirect
  • Loading branch information
thecristen authored Nov 7, 2023
1 parent dba772c commit 078dd1b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
5 changes: 5 additions & 0 deletions apps/site/lib/site_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ defmodule SiteWeb.Router do
get("/*path", WwwRedirector, [])
end

# redirect 'mbta.com/charlie/*' to 'charlie.mbta.com/*'
scope "/charlie", SiteWeb do
get("/*path", WwwRedirector, host: "https://charlie.mbta.com")
end

scope "/", SiteWeb do
pipe_through([:secure, :browser])

Expand Down
21 changes: 17 additions & 4 deletions apps/site/lib/site_web/www_redirector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ defmodule SiteWeb.WwwRedirector do
alias Plug.Conn

@impl true
def init([]), do: []
def init(options), do: options

@impl true
def call(conn, _options), do: site_redirect(SiteWeb.Endpoint.url(), conn)
def call(conn, options) do
url = Keyword.get(options, :host, SiteWeb.Endpoint.url())
site_redirect(url, conn)
end

@spec site_redirect(String.t(), Conn.t()) :: Plug.Conn.t()
def site_redirect(site_url, conn) do
Expand All @@ -21,12 +24,22 @@ defmodule SiteWeb.WwwRedirector do
|> halt()
end

defp redirect_url(site_url, %Conn{request_path: path, query_string: query})
# If path_params are matched via SiteWeb.Router, use that to determine path
defp redirect_url(site_url, %Conn{path_params: %{"path" => path}, query_string: query}) do
revised_path = "/" <> Enum.join(path, "/")
do_redirect_url(site_url, revised_path, query)
end

defp redirect_url(site_url, %Conn{request_path: path, query_string: query}) do
do_redirect_url(site_url, path, query)
end

defp do_redirect_url(site_url, path, query)
when is_binary(query) and query != "" do
"#{site_url}#{path}?#{query}"
end

defp redirect_url(site_url, %Conn{request_path: path}) do
defp do_redirect_url(site_url, path, _) do
"#{site_url}#{path}"
end
end
22 changes: 22 additions & 0 deletions apps/site/test/site/lib/www_redirector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@ defmodule SiteWeb.WwwRedirectorTest do
assert_conn_redirected_halted(conn, SiteWeb.Endpoint.url() <> "/news")
end

test "call with host option", %{conn: conn} do
conn = WwwRedirector.call(%{conn | request_path: "/news"}, host: "https://myfakedomain.xyz")
assert_conn_redirected_halted(conn, "https://myfakedomain.xyz/news")
end

@doc "Use case: The path_params are populated in the router, and if they're part of a scoped route, the redirected URL should not include the path prefix."
test "omits scoped route path prefix", %{conn: conn} do
request_path = "/charlie/page/one/two"

conn = %{
conn
| request_path: request_path,
query_string: nil,
path_params: %{"path" => ["page", "one", "two"]}
}

conn = WwwRedirector.call(conn, host: "https://myfakedomain.xyz")

refute redirected_to(conn, :moved_permanently) == "https://myfakedomain.xyz#{request_path}"
assert_conn_redirected_halted(conn, "https://myfakedomain.xyz/page/one/two")
end

describe "redirect" do
test "top level redirected", %{conn: conn} do
check_redirect_to_www_mbta(conn, "/", nil, "https://www.mbta.com/")
Expand Down

0 comments on commit 078dd1b

Please sign in to comment.