diff --git a/lib/dotcom_web/plugs/rewrite_urls.ex b/lib/dotcom_web/plugs/rewrite_urls.ex index 2a4127216b..7994a0e48b 100644 --- a/lib/dotcom_web/plugs/rewrite_urls.ex +++ b/lib/dotcom_web/plugs/rewrite_urls.ex @@ -25,6 +25,19 @@ defmodule DotcomWeb.Plugs.RewriteUrls do end end + # Send SF1.0 URLS to the new SF2.0 + defp rewrite_url( + %{ + path_info: ["schedules", _, "line"], + params: %{ + "route" => route_id, + "schedule_finder" => %{"direction_id" => direction_id, "origin" => stop_id} + } + } = conn + ) do + "/departures/?route_id=#{route_id}&direction_id=#{direction_id}&stop_id=#{stop_id}" + end + defp rewrite_url(%{path_info: ["schedules", "Boat-F3" | _]} = conn) do String.replace(conn.request_path, "Boat-F3", "Boat-F1") end @@ -38,6 +51,10 @@ defmodule DotcomWeb.Plugs.RewriteUrls do end defp merge_url(base_url, query_string) do - "#{base_url}?#{query_string}" + if base_url |> String.contains?("?") do + "#{base_url}&#{query_string}" + else + "#{base_url}?#{query_string}" + end end end diff --git a/test/dotcom_web/plugs/rewrite_urls_test.exs b/test/dotcom_web/plugs/rewrite_urls_test.exs index 485a91f3db..850eb78f84 100644 --- a/test/dotcom_web/plugs/rewrite_urls_test.exs +++ b/test/dotcom_web/plugs/rewrite_urls_test.exs @@ -11,6 +11,28 @@ defmodule DotcomWeb.Plugs.RewriteUrlsTest do assert conn.halted end + test "redirects if we're going to the old schedule finder", %{conn: conn} do + route_id = Faker.Pokemon.name() + direction_id = Faker.Util.pick(["0", "1"]) + stop_id = Faker.Pokemon.name() + + conn = %{ + conn + | params: %{ + "route" => route_id, + "schedule_finder" => %{"direction_id" => direction_id, "origin" => stop_id} + }, + path_info: ["schedules", "red", "line"] + } + + conn = call(conn, []) + + assert redirected_to(conn, 302) == + "/departures/?route_id=#{route_id}&direction_id=#{direction_id}&stop_id=#{stop_id}" + + assert conn.halted + end + test "includes a query string if present", %{conn: conn} do conn = %{ conn