Skip to content

Commit

Permalink
Omit trailing base_url slash
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Apr 12, 2024
1 parent 4efb901 commit 42d45c1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions lib/assent/strategy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,14 @@ defmodule Assent.Strategy do

defp encode_value(value), do: URI.encode_www_form(Kernel.to_string(value))

defp endpoint(base_url, uri),
do: URI.merge(URI.parse(base_url), uri) |> URI.to_string()
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, uri), do: uri

@doc """
Normalize API user request response into standard claims
Expand Down
26 changes: 12 additions & 14 deletions test/assent/strategy_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -247,23 +247,21 @@ defmodule Assent.StrategyTest do
Strategy.verify_jwt(@token, @secret, json_library: CustomJSONLibrary)
end

describe "to_url/3" do
test "with trailing slash in domain and leading slash in path" do
assert Strategy.to_url("http://localhost/", "/path") == "http://localhost/path"
end
test "to_url/3" do
assert Strategy.to_url("http://example.com", "/path") == "http://example.com/path"
assert Strategy.to_url("http://example.com/", "/path") == "http://example.com/path"

test "with trailing slash in domain" do
assert Strategy.to_url("http://localhost/", "path") == "http://localhost/path"
end
assert Strategy.to_url("http://example.com/path", "/other-path") ==
"http://example.com/path/other-path"

test "with leading slash in path" do
assert Strategy.to_url("http://localhost", "/path") == "http://localhost/path"
end
assert Strategy.to_url("http://example.com/path/", "/other-path") ==
"http://example.com/path/other-path"

test "with valid inputs" 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"
end
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
Expand Down

0 comments on commit 42d45c1

Please sign in to comment.