Skip to content

Commit

Permalink
Revert "feat(SiteWeb.Router): add /charlie/* redirects (#1782)"
Browse files Browse the repository at this point in the history
This reverts commit 078dd1b.
  • Loading branch information
thecristen committed Nov 7, 2023
1 parent c2e04a9 commit 63fd209
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 44 deletions.
5 changes: 0 additions & 5 deletions apps/site/lib/site_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ 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: 4 additions & 17 deletions apps/site/lib/site_web/www_redirector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ defmodule SiteWeb.WwwRedirector do
alias Plug.Conn

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

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

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

# 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)
defp redirect_url(site_url, %Conn{request_path: path, query_string: query})
when is_binary(query) and query != "" do
"#{site_url}#{path}?#{query}"
end

defp do_redirect_url(site_url, path, _) do
defp redirect_url(site_url, %Conn{request_path: path}) do
"#{site_url}#{path}"
end
end
22 changes: 0 additions & 22 deletions apps/site/test/site/lib/www_redirector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,6 @@ 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 63fd209

Please sign in to comment.