diff --git a/lib/uri/ftp.rb b/lib/uri/ftp.rb index 1c75e242..d6c002ba 100644 --- a/lib/uri/ftp.rb +++ b/lib/uri/ftp.rb @@ -247,6 +247,20 @@ def set_path(v) end protected :set_path + # An empty FTP path is the login directory. Generic#normalize! would + # pass "/" through set_path, which encodes it as the root (%2F). + def normalize! + if path&.empty? + @path = '/' + end + if scheme && scheme != scheme.downcase + set_scheme(scheme.downcase) + end + if host && host != host.downcase + set_host(host.downcase) + end + end + # Returns a String representation of the URI::FTP. def to_s save_path = nil diff --git a/test/uri/test_ftp.rb b/test/uri/test_ftp.rb index 3ad78644..2a1c17fb 100644 --- a/test/uri/test_ftp.rb +++ b/test/uri/test_ftp.rb @@ -49,6 +49,13 @@ def test_assemble uri.to_s) end + def test_normalize_empty_path + assert_equal('ftp://ftp.example.com/', URI.parse('ftp://ftp.example.com').normalize.to_s) + assert_equal('ftp://ftp.example.com/', URI.parse('FTP://FTP.Example.COM').normalize.to_s) + assert_equal('ftp://ftp.example.com/pub', URI.parse('ftp://ftp.example.com/pub').normalize.to_s) + assert_equal('ftp://ftp.example.com/%2Fpub', URI.parse('ftp://ftp.example.com/%2Fpub').normalize.to_s) + end + def test_select assert_equal(['ftp', 'a.b.c', 21], URI.parse('ftp://a.b.c/').select(:scheme, :host, :port)) u = URI.parse('ftp://a.b.c/')