Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use URI.parse to handle url edge cases #150

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading