зеркало из https://github.com/github/ruby.git
[ruby/uri] Improve URI.register_scheme tests and automatically upcase the given scheme
* Also add docs and mention current limitations. * For reference, https://stackoverflow.com/a/3641782/388803 mentions the valid characters in schemes. https://github.com/ruby/uri/commit/4346daac75
This commit is contained in:
Родитель
fbebfe1697
Коммит
40ca208a6d
|
@ -68,8 +68,13 @@ module URI
|
|||
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.
|
||||
|
|
|
@ -41,18 +41,52 @@ class TestCommon < Test::Unit::TestCase
|
|||
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
|
||||
|
|
Загрузка…
Ссылка в новой задаче