Skip to content

Commit

Permalink
Apply fixes from PR
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaelbej committed Aug 28, 2024
1 parent 2e05c07 commit 592da7b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
60 changes: 32 additions & 28 deletions lib/ask_web/controllers/short_link_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,43 @@ 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
defp split_path(path) 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
2 changes: 1 addition & 1 deletion test/ask_web/controllers/short_link_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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", %{
Expand Down

0 comments on commit 592da7b

Please sign in to comment.