Skip to content
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
7 changes: 6 additions & 1 deletion lib/uri/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,13 @@ module Schemes
end
private_constant :Schemes

#
# Register the given +klass+ to be instantiated when parsing URLs with the given +scheme+.
# Note that currently only schemes which after .upcase are valid constant names
# can be registered (no -/+/. allowed).
#
def self.register_scheme(scheme, klass)
Schemes.const_set(scheme, klass)
Schemes.const_set(scheme.to_s.upcase, klass)
end

# Returns a Hash of the defined schemes.
Expand Down
40 changes: 37 additions & 3 deletions test/uri/test_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,52 @@ def test_ractor
RUBY
end

DEFAULT_SCHEMES = ["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS"].sort.freeze

def test_register_scheme
assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS"].sort, URI.scheme_list.keys.sort)
assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort)

foobar = Class.new(URI::Generic)
URI.register_scheme 'FOOBAR', foobar
begin
assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS", "FOOBAR"].sort, URI.scheme_list.keys.sort)
assert_include(URI.scheme_list.keys, "FOOBAR")
assert_equal foobar, URI.parse('foobar://localhost').class
ensure
URI.const_get(:Schemes).send(:remove_const, :FOOBAR)
end

assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS"].sort, URI.scheme_list.keys.sort)
assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort)
end

def test_register_scheme_lowercase
assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort)

foobar = Class.new(URI::Generic)
URI.register_scheme 'foobarlower', foobar
begin
assert_include(URI.scheme_list.keys, "FOOBARLOWER")
assert_equal foobar, URI.parse('foobarlower://localhost').class
ensure
URI.const_get(:Schemes).send(:remove_const, :FOOBARLOWER)
end

assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort)
end

def test_register_scheme_with_symbols
# Valid schemes from https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
some_uri_class = Class.new(URI::Generic)
assert_raise(NameError) { URI.register_scheme 'ms-search', some_uri_class }
assert_raise(NameError) { URI.register_scheme 'microsoft.windows.camera', some_uri_class }
assert_raise(NameError) { URI.register_scheme 'coaps+ws', some_uri_class }

ms_search_class = Class.new(URI::Generic)
URI.register_scheme 'MS_SEARCH', ms_search_class
begin
assert_equal URI::Generic, URI.parse('ms-search://localhost').class
ensure
URI.const_get(:Schemes).send(:remove_const, :MS_SEARCH)
end
end

def test_regexp
Expand Down