diff --git a/lib/uri/common.rb b/lib/uri/common.rb index 971a684..624209b 100644 --- a/lib/uri/common.rb +++ b/lib/uri/common.rb @@ -163,7 +163,8 @@ def self.scheme_list end # :stopdoc: - INITIAL_SCHEMES = scheme_list + # Constant names of the bundled schemes, by lowercase name, to skip Schemes.escape. + INITIAL_SCHEMES = %w[file ftp http https ldap ldaps mailto ws wss].map { |scheme| [scheme, scheme.upcase.to_sym] }.to_h private_constant :INITIAL_SCHEMES Ractor.make_shareable(INITIAL_SCHEMES) if defined?(Ractor) # :startdoc: @@ -185,11 +186,8 @@ def self.scheme_list # # => # # def self.for(scheme, *arguments, default: Generic) - const_name = Schemes.escape(scheme) - - uri_class = INITIAL_SCHEMES[const_name] - uri_class ||= Schemes.find(const_name) - uri_class ||= default + const_name = INITIAL_SCHEMES[scheme] || Schemes.escape(scheme) + uri_class = Schemes.find(const_name) || default return uri_class.new(scheme, *arguments) end diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index fce8a8a..30d9cec 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -507,7 +507,7 @@ def password=(password) # See also URI::Generic.userinfo=. # def set_userinfo(user, password = nil) - unless password + if user && !password user, password = split_userinfo(user) end @user = user diff --git a/test/uri/test_common.rb b/test/uri/test_common.rb index 5692640..6dfc7ee 100644 --- a/test/uri/test_common.rb +++ b/test/uri/test_common.rb @@ -114,6 +114,15 @@ def test_register_scheme_lowercase assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort) end + def test_register_scheme_overrides_bundled_scheme + http = Class.new(URI::HTTP) + EnvUtil.suppress_warning { URI.register_scheme 'HTTP', http } + assert_equal http, URI.parse('http://localhost').class + assert_equal http, URI.for('http', nil, 'localhost', nil, nil, '/', nil, nil, nil).class + ensure + EnvUtil.suppress_warning { URI.register_scheme 'HTTP', URI::HTTP } + end + def test_register_scheme_with_symbols # Valid schemes from https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml list = []