Skip to content

Commit

Permalink
Merge pull request #150 from dtluther/handle-more-cases-for-to_url
Browse files Browse the repository at this point in the history
Use URI.parse to handle url edge cases
  • Loading branch information
danschultzer authored Apr 12, 2024
2 parents 8091522 + 42d45c1 commit 6d63f70
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 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
11 changes: 7 additions & 4 deletions lib/assent/strategy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions test/assent/strategy_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6d63f70

Please sign in to comment.