Skip to content

Commit 4346daa

Browse files
committed
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.
1 parent 16cfc4e commit 4346daa

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

lib/uri/common.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,13 @@ module Schemes
6868
end
6969
private_constant :Schemes
7070

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

7580
# Returns a Hash of the defined schemes.

test/uri/test_common.rb

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,52 @@ def test_ractor
4141
RUBY
4242
end
4343

44+
DEFAULT_SCHEMES = ["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS"].sort.freeze
45+
4446
def test_register_scheme
45-
assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS"].sort, URI.scheme_list.keys.sort)
47+
assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort)
4648

4749
foobar = Class.new(URI::Generic)
4850
URI.register_scheme 'FOOBAR', foobar
4951
begin
50-
assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS", "FOOBAR"].sort, URI.scheme_list.keys.sort)
52+
assert_include(URI.scheme_list.keys, "FOOBAR")
53+
assert_equal foobar, URI.parse('foobar://localhost').class
5154
ensure
5255
URI.const_get(:Schemes).send(:remove_const, :FOOBAR)
5356
end
5457

55-
assert_equal(["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS"].sort, URI.scheme_list.keys.sort)
58+
assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort)
59+
end
60+
61+
def test_register_scheme_lowercase
62+
assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort)
63+
64+
foobar = Class.new(URI::Generic)
65+
URI.register_scheme 'foobarlower', foobar
66+
begin
67+
assert_include(URI.scheme_list.keys, "FOOBARLOWER")
68+
assert_equal foobar, URI.parse('foobarlower://localhost').class
69+
ensure
70+
URI.const_get(:Schemes).send(:remove_const, :FOOBARLOWER)
71+
end
72+
73+
assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort)
74+
end
75+
76+
def test_register_scheme_with_symbols
77+
# Valid schemes from https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
78+
some_uri_class = Class.new(URI::Generic)
79+
assert_raise(NameError) { URI.register_scheme 'ms-search', some_uri_class }
80+
assert_raise(NameError) { URI.register_scheme 'microsoft.windows.camera', some_uri_class }
81+
assert_raise(NameError) { URI.register_scheme 'coaps+ws', some_uri_class }
82+
83+
ms_search_class = Class.new(URI::Generic)
84+
URI.register_scheme 'MS_SEARCH', ms_search_class
85+
begin
86+
assert_equal URI::Generic, URI.parse('ms-search://localhost').class
87+
ensure
88+
URI.const_get(:Schemes).send(:remove_const, :MS_SEARCH)
89+
end
5690
end
5791

5892
def test_regexp

0 commit comments

Comments
 (0)