From 31e0a1107c916ce57f079a35fb42b25bf3e63705 Mon Sep 17 00:00:00 2001 From: Dan Schultzer <1254724+danschultzer@users.noreply.github.com> Date: Thu, 11 Apr 2024 17:27:19 -0700 Subject: [PATCH] Omit trailing base_url slash --- CHANGELOG.md | 2 ++ lib/assent/strategy.ex | 10 ++++++++-- test/assent/strategy_test.exs | 26 +++++++++----------------- 3 files changed, 19 insertions(+), 19 deletions(-) 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 ae70676..44bb622 100644 --- a/lib/assent/strategy.ex +++ b/lib/assent/strategy.ex @@ -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 diff --git a/test/assent/strategy_test.exs b/test/assent/strategy_test.exs index 0b8c3d5..2976459 100644 --- a/test/assent/strategy_test.exs +++ b/test/assent/strategy_test.exs @@ -247,23 +247,15 @@ 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 "with trailing slash in domain" do - assert Strategy.to_url("http://localhost/", "path") == "http://localhost/path" - end - - test "with leading slash in path" do - assert Strategy.to_url("http://localhost", "/path") == "http://localhost/path" - end - - 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 + 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" + 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