Skip to content

Translate :undefined URI port to nil #13464

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

Merged
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
10 changes: 5 additions & 5 deletions lib/elixir/lib/uri.ex
Original file line number Diff line number Diff line change
Expand Up @@ -656,16 +656,16 @@ defmodule URI do
scheme = String.downcase(scheme, :ascii)

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

%{} ->
case default_port(scheme) do
nil -> %{uri | scheme: scheme}
port -> %{uri | scheme: scheme, port: port}
end
%{uri | scheme: scheme, port: default_port(scheme)}
end

%{port: :undefined} ->
%{uri | port: nil}

%{} ->
uri
end
Expand Down
26 changes: 26 additions & 0 deletions lib/elixir/test/elixir/uri_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,32 @@ defmodule URITest do
test "preserves an empty query" do
assert URI.new!("http://foo.com/?").query == ""
end

test "without scheme, undefined port after host translates to nil" do
assert URI.new!("//https://www.example.com") ==
%URI{
scheme: nil,
userinfo: nil,
host: "https",
port: nil,
path: "//www.example.com",
query: nil,
fragment: nil
}
end

test "with scheme, undefined port after host translates to nil" do
assert URI.new!("myscheme://myhost:/path/info") ==
%URI{
scheme: "myscheme",
userinfo: nil,
host: "myhost",
port: nil,
path: "/path/info",
query: nil,
fragment: nil
}
end
end

test "http://http://http://@http://http://?http://#http://" do
Expand Down