diff --git a/lib/ask_web/controllers/short_link_controller.ex b/lib/ask_web/controllers/short_link_controller.ex index 6cd73a9b3..fafd88cc8 100644 --- a/lib/ask_web/controllers/short_link_controller.ex +++ b/lib/ask_web/controllers/short_link_controller.ex @@ -9,34 +9,7 @@ defmodule AskWeb.ShortLinkController do ShortLink |> Repo.get_by(hash: hash) - if link do - conn = - conn - |> assign(:skip_auth, true) - - {path, query_string} = - case :binary.split(link.target, "?", [:global]) do - [path, query_string] -> {path, query_string} - [path] -> {path, ""} - end - - conn = %{ - conn - | request_path: path, - path_info: split_path(path), - params: %{}, - path_params: %{}, - private: %{}, - req_headers: [], - query_string: query_string, - query_params: %Plug.Conn.Unfetched{aspect: :query_params} - } - - AskWeb.Endpoint.call(conn, []) - else - conn - |> send_resp(:not_found, "Link not found") - end + resolve_link(conn, link) end # Copied from Plug.Adapters.Cowboy.Conn @@ -44,4 +17,35 @@ defmodule AskWeb.ShortLinkController do segments = :binary.split(path, "/", [:global]) for segment <- segments, segment != "", do: segment end + + defp resolve_link(conn, nil) do + conn + |> send_resp(:not_found, "Link not found") + end + + defp resolve_link(conn, link) do + conn = + conn + |> assign(:skip_auth, true) + + {path, query_string} = + case :binary.split(link.target, "?", [:global]) do + [path, query_string] -> {path, query_string} + [path] -> {path, ""} + end + + conn = %{ + conn + | request_path: path, + path_info: split_path(path), + params: %{}, + path_params: %{}, + private: %{}, + req_headers: [], + query_string: query_string, + query_params: %Plug.Conn.Unfetched{aspect: :query_params} + } + + AskWeb.Endpoint.call(conn, []) + end end diff --git a/test/ask_web/controllers/short_link_controller_test.exs b/test/ask_web/controllers/short_link_controller_test.exs index 9507692ec..481733d7b 100644 --- a/test/ask_web/controllers/short_link_controller_test.exs +++ b/test/ask_web/controllers/short_link_controller_test.exs @@ -19,7 +19,7 @@ defmodule AskWeb.ShortLinkControllerTest do } do conn = get(conn, short_link_path(conn, :access, "invalid-link")) - assert response(conn, 404) + assert response(conn, 404) == "Link not found" end test "render surveys if the link specifies that endpoint even if there is no current user", %{