Skip to content

Commit 6d63f70

Browse files
authored
Merge pull request #150 from dtluther/handle-more-cases-for-to_url
Use URI.parse to handle url edge cases
2 parents 8091522 + 42d45c1 commit 6d63f70

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
Requires Elixir 1.13+
66

7+
- Fixed bug with trailing slash in `:base_url` not being ommitted when concatenating with relative path
8+
79
## v0.2.9 (2023-11-22)
810

911
- Fixed bug where `Req` was not used by default if included in project

lib/assent/strategy.ex

+7-4
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,14 @@ defmodule Assent.Strategy do
183183

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

186-
defp endpoint(base_url, <<"/"::utf8, _::binary>> = uri),
187-
do: base_url <> uri
186+
defp endpoint(base_url, "/" <> uri = all) do
187+
case :binary.last(base_url) do
188+
?/ -> base_url <> uri
189+
_ -> base_url <> all
190+
end
191+
end
188192

189-
defp endpoint(_base_url, url),
190-
do: url
193+
defp endpoint(_base_url, uri), do: uri
191194

192195
@doc """
193196
Normalize API user request response into standard claims

test/assent/strategy_test.exs

+14-2
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,20 @@ defmodule Assent.StrategyTest do
248248
end
249249

250250
test "to_url/3" do
251-
assert Strategy.to_url("http://localhost", "/path", a: 1, b: [c: 2, d: [e: 3]], f: [4, 5]) ==
252-
"http://localhost/path?a=1&b[c]=2&b[d][e]=3&f[]=4&f[]=5"
251+
assert Strategy.to_url("http://example.com", "/path") == "http://example.com/path"
252+
assert Strategy.to_url("http://example.com/", "/path") == "http://example.com/path"
253+
254+
assert Strategy.to_url("http://example.com/path", "/other-path") ==
255+
"http://example.com/path/other-path"
256+
257+
assert Strategy.to_url("http://example.com/path/", "/other-path") ==
258+
"http://example.com/path/other-path"
259+
260+
assert Strategy.to_url("http://example.com/path", "http://example.org/other-path") ==
261+
"http://example.org/other-path"
262+
263+
assert Strategy.to_url("http://example.com", "/path", a: 1, b: [c: 2, d: [e: 3]], f: [4, 5]) ==
264+
"http://example.com/path?a=1&b[c]=2&b[d][e]=3&f[]=4&f[]=5"
253265
end
254266

255267
test "normalize_userinfo/2" do

0 commit comments

Comments
 (0)