Skip to content

Commit

Permalink
Override existing "k" parameter in prepare_url
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicletz committed Mar 20, 2024
1 parent d920c6f commit c99d93e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
30 changes: 9 additions & 21 deletions lib/desktop/window.ex
Original file line number Diff line number Diff line change
Expand Up @@ -636,27 +636,15 @@ defmodule Desktop.Window do
{:reply, frame, ui}
end

def prepare_url(url) do
query = "k=" <> Desktop.Auth.login_key()

case url do
nil -> nil
fun when is_function(fun) -> append_query(fun.(), query)
string when is_binary(string) -> append_query(string, query)
end
end

defp append_query(url, query) do
case URI.parse(url) do
url = %URI{query: nil} ->
%URI{url | query: query}

url = %URI{query: other} ->
if String.contains?(other, query) do
url
else
%URI{url | query: other <> "&" <> query}
end
def prepare_url(url) when is_function(url), do: prepare_url(url.())
def prepare_url(nil), do: nil
def prepare_url(url) when is_binary(url) do
query = %{"k" => Desktop.Auth.login_key()}
uri = URI.parse(url)

case uri do
%URI{query: nil} -> %URI{uri | query: URI.encode_query(query)}
%URI{query: other} -> %URI{uri | query: URI.encode_query(Map.merge(URI.decode_query(other), query))}
end
|> URI.to_string()
end
Expand Down
15 changes: 15 additions & 0 deletions test/window_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule DesktopWindowTest do
use ExUnit.Case

test "prepare_url" do
assert Desktop.Window.prepare_url(nil) == nil

expected = "/some/?k=" <> Desktop.Auth.login_key()
assert Desktop.Window.prepare_url("/some/?k=123") == expected
assert Desktop.Window.prepare_url("/some/") == expected

expected = "/some/?bla=something_cool&k=" <> Desktop.Auth.login_key()
assert Desktop.Window.prepare_url("/some/?bla=something_cool") == expected
assert Desktop.Window.prepare_url("/some/?bla=something_cool&k=xx") == expected
end
end

0 comments on commit c99d93e

Please sign in to comment.