diff --git a/CHANGELOG.md b/CHANGELOG.md index b759e60..6c491b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Requires Elixir 1.13+ +- Fixed bug with trailing slash in `:base_url` not being ommitted when concatenating with relative path + ## v0.2.9 (2023-11-22) - Fixed bug where `Req` was not used by default if included in project diff --git a/lib/assent/strategy.ex b/lib/assent/strategy.ex index 47d8549..44bb622 100644 --- a/lib/assent/strategy.ex +++ b/lib/assent/strategy.ex @@ -183,11 +183,14 @@ defmodule Assent.Strategy do defp encode_value(value), do: URI.encode_www_form(Kernel.to_string(value)) - defp endpoint(base_url, <<"/"::utf8, _::binary>> = uri), - do: base_url <> uri + defp endpoint(base_url, "/" <> uri = all) do + case :binary.last(base_url) do + ?/ -> base_url <> uri + _ -> base_url <> all + end + end - defp endpoint(_base_url, url), - do: url + defp endpoint(_base_url, uri), do: uri @doc """ Normalize API user request response into standard claims diff --git a/test/assent/strategy_test.exs b/test/assent/strategy_test.exs index cd5f016..0f2a107 100644 --- a/test/assent/strategy_test.exs +++ b/test/assent/strategy_test.exs @@ -248,8 +248,20 @@ defmodule Assent.StrategyTest do end test "to_url/3" do - assert Strategy.to_url("http://localhost", "/path", a: 1, b: [c: 2, d: [e: 3]], f: [4, 5]) == - "http://localhost/path?a=1&b[c]=2&b[d][e]=3&f[]=4&f[]=5" + assert Strategy.to_url("http://example.com", "/path") == "http://example.com/path" + assert Strategy.to_url("http://example.com/", "/path") == "http://example.com/path" + + assert Strategy.to_url("http://example.com/path", "/other-path") == + "http://example.com/path/other-path" + + assert Strategy.to_url("http://example.com/path/", "/other-path") == + "http://example.com/path/other-path" + + assert Strategy.to_url("http://example.com/path", "http://example.org/other-path") == + "http://example.org/other-path" + + assert Strategy.to_url("http://example.com", "/path", a: 1, b: [c: 2, d: [e: 3]], f: [4, 5]) == + "http://example.com/path?a=1&b[c]=2&b[d][e]=3&f[]=4&f[]=5" end test "normalize_userinfo/2" do