Skip to content

Commit ed85d09

Browse files
handle undefined port with custom scheme
1 parent db89644 commit ed85d09

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

lib/elixir/lib/uri.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,9 +656,12 @@ defmodule URI do
656656
scheme = String.downcase(scheme, :ascii)
657657

658658
case map do
659-
%{port: port} when port != :undefined ->
659+
%{port: port} when is_integer(port) ->
660660
%{uri | scheme: scheme}
661661

662+
%{port: :undefined} ->
663+
%{uri | scheme: scheme, port: default_port(scheme)}
664+
662665
%{} ->
663666
case default_port(scheme) do
664667
nil -> %{uri | scheme: scheme}

lib/elixir/test/elixir/uri_test.exs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,6 @@ defmodule URITest do
9797
assert URI.new("") == {:ok, %URI{}}
9898
end
9999

100-
test "missing port part after host never resolves to :undefined" do
101-
assert URI.new("//https://www.example.com") ==
102-
{:ok,
103-
%URI{
104-
scheme: nil,
105-
userinfo: nil,
106-
host: "https",
107-
port: nil,
108-
path: "//www.example.com",
109-
query: nil,
110-
fragment: nil
111-
}}
112-
end
113-
114100
test "errors on bad URIs" do
115101
assert URI.new("/>") == {:error, ">"}
116102
assert URI.new(":https") == {:error, ":"}
@@ -291,6 +277,32 @@ defmodule URITest do
291277
test "preserves an empty query" do
292278
assert URI.new!("http://foo.com/?").query == ""
293279
end
280+
281+
test "without scheme, undefined port after host translates to nil" do
282+
assert URI.new!("//https://www.example.com") ==
283+
%URI{
284+
scheme: nil,
285+
userinfo: nil,
286+
host: "https",
287+
port: nil,
288+
path: "//www.example.com",
289+
query: nil,
290+
fragment: nil
291+
}
292+
end
293+
294+
test "with scheme, undefined port after host translates to nil" do
295+
assert URI.new!("myscheme://myhost:/path/info") ==
296+
%URI{
297+
scheme: "myscheme",
298+
userinfo: nil,
299+
host: "myhost",
300+
port: nil,
301+
path: "/path/info",
302+
query: nil,
303+
fragment: nil
304+
}
305+
end
294306
end
295307

296308
test "http://http://http://@http://http://?http://#http://" do

0 commit comments

Comments
 (0)