Skip to content

Commit c63cc10

Browse files
wojtekmachjosevalim
authored andcommitted
URI.append_query/2: Handle trailing ampersand (#12069)
1 parent 78ba7aa commit c63cc10

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/elixir/lib/uri.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,11 @@ defmodule URI do
966966
end
967967

968968
def append_query(%URI{} = uri, query) when is_binary(query) do
969-
%{uri | query: uri.query <> "&" <> query}
969+
if String.ends_with?(uri.query, "&") do
970+
%{uri | query: uri.query <> query}
971+
else
972+
%{uri | query: uri.query <> "&" <> query}
973+
end
970974
end
971975
end
972976

lib/elixir/test/elixir/uri_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,11 @@ defmodule URITest do
400400
"https://images.example.com/t/1600x/https://images.example.com/foo.jpg"
401401
end
402402

403+
test "append_query/2" do
404+
assert URI.append_query(URI.parse("http://example.com/?x=1"), "x=2").query == "x=1&x=2"
405+
assert URI.append_query(URI.parse("http://example.com/?x=1&"), "x=2").query == "x=1&x=2"
406+
end
407+
403408
## Deprecate API
404409

405410
describe "authority" do

0 commit comments

Comments
 (0)