diff options
Diffstat (limited to 'test/uri')
| -rw-r--r-- | test/uri/test_common.rb | 343 | ||||
| -rw-r--r-- | test/uri/test_file.rb | 67 | ||||
| -rw-r--r-- | test/uri/test_ftp.rb | 31 | ||||
| -rw-r--r-- | test/uri/test_generic.rb | 545 | ||||
| -rw-r--r-- | test/uri/test_http.rb | 62 | ||||
| -rw-r--r-- | test/uri/test_ldap.rb | 43 | ||||
| -rw-r--r-- | test/uri/test_mailto.rb | 189 | ||||
| -rw-r--r-- | test/uri/test_parser.rb | 124 | ||||
| -rw-r--r-- | test/uri/test_ws.rb | 65 | ||||
| -rw-r--r-- | test/uri/test_wss.rb | 65 |
10 files changed, 1356 insertions, 178 deletions
diff --git a/test/uri/test_common.rb b/test/uri/test_common.rb index 0fe031bdee..569264005a 100644 --- a/test/uri/test_common.rb +++ b/test/uri/test_common.rb @@ -1,46 +1,150 @@ +# frozen_string_literal: false require 'test/unit' +require 'envutil' require 'uri' -module URI - - -class TestCommon < Test::Unit::TestCase +class URI::TestCommon < Test::Unit::TestCase def setup end def teardown end + EnvUtil.suppress_warning do + class Foo + # Intentionally use `URI::REGEXP`, which is for the compatibility + include URI::REGEXP::PATTERN + end + end + + def test_fallback_constants + EnvUtil.suppress_warning do + assert_raise(NameError) { URI::FOO } + + assert_equal URI::ABS_URI, URI::RFC2396_PARSER.regexp[:ABS_URI] + assert_equal URI::PATTERN, URI::RFC2396_Parser::PATTERN + assert_equal URI::REGEXP, URI::RFC2396_REGEXP + assert_equal URI::REGEXP::PATTERN, URI::RFC2396_REGEXP::PATTERN + assert_equal Foo::IPV4ADDR, URI::RFC2396_REGEXP::PATTERN::IPV4ADDR + end + end + + def test_parser_switch + assert_equal(URI::Parser, URI::RFC3986_Parser) + assert_equal(URI::PARSER, URI::RFC3986_PARSER) + refute defined?(URI::REGEXP) + refute defined?(URI::PATTERN) + + URI.parser = URI::RFC2396_PARSER + + assert_equal(URI::Parser, URI::RFC2396_Parser) + assert_equal(URI::PARSER, URI::RFC2396_PARSER) + assert defined?(URI::REGEXP) + assert defined?(URI::PATTERN) + assert defined?(URI::PATTERN::ESCAPED) + assert defined?(URI::REGEXP::PATTERN::IPV6ADDR) + + URI.parser = URI::RFC3986_PARSER + + assert_equal(URI::Parser, URI::RFC3986_Parser) + assert_equal(URI::PARSER, URI::RFC3986_PARSER) + refute defined?(URI::REGEXP) + refute defined?(URI::PATTERN) + ensure + URI.parser = URI::RFC3986_PARSER + end + def test_extract - assert_equal(['http://example.com'], - URI.extract('http://example.com')) - assert_equal(['http://example.com'], - URI.extract('(http://example.com)')) - assert_equal(['http://example.com/foo)'], - URI.extract('(http://example.com/foo)')) - assert_equal(['http://example.jphttp://example.jp'], - URI.extract('http://example.jphttp://example.jp'), "[ruby-list:36086]") - assert_equal(['http://example.jphttp://example.jp'], - URI.extract('http://example.jphttp://example.jp', ['http']), "[ruby-list:36086]") - assert_equal(['http://', 'mailto:'].sort, - URI.extract('ftp:// http:// mailto: https://', ['http', 'mailto']).sort) - # reported by Doug Kearns <djkea2@mugca.its.monash.edu.au> - assert_equal(['From:', 'mailto:xxx@xxx.xxx.xxx]'].sort, - URI.extract('From: XXX [mailto:xxx@xxx.xxx.xxx]').sort) + EnvUtil.suppress_warning do + assert_equal(['http://example.com'], + URI.extract('http://example.com')) + assert_equal(['http://example.com'], + URI.extract('(http://example.com)')) + assert_equal(['http://example.com/foo)'], + URI.extract('(http://example.com/foo)')) + assert_equal(['http://example.jphttp://example.jp'], + URI.extract('http://example.jphttp://example.jp'), "[ruby-list:36086]") + assert_equal(['http://example.jphttp://example.jp'], + URI.extract('http://example.jphttp://example.jp', ['http']), "[ruby-list:36086]") + assert_equal(['http://', 'mailto:'].sort, + URI.extract('ftp:// http:// mailto: https://', ['http', 'mailto']).sort) + # reported by Doug Kearns <djkea2@mugca.its.monash.edu.au> + assert_equal(['From:', 'mailto:xxx@xxx.xxx.xxx]'].sort, + URI.extract('From: XXX [mailto:xxx@xxx.xxx.xxx]').sort) + end + end + + def test_ractor + return unless defined?(Ractor) + assert_ractor(<<~RUBY, require: 'uri') + r = Ractor.new { URI.parse("https://ruby-lang.org/").inspect } + assert_equal(URI.parse("https://ruby-lang.org/").inspect, r.value) + RUBY + end + + DEFAULT_SCHEMES = ["FILE", "FTP", "HTTP", "HTTPS", "LDAP", "LDAPS", "MAILTO", "WS", "WSS"].sort.freeze + + def test_register_scheme + assert_equal(DEFAULT_SCHEMES, URI.scheme_list.keys.sort) + + foobar = Class.new(URI::Generic) + URI.register_scheme 'FOOBAR', foobar + begin + 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(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 + list = [] + %w[ms-search microsoft.windows.camera coaps+ws].each {|name| + list << [name, URI.register_scheme(name, Class.new(URI::Generic))] + } + + list.each do |scheme, uri_class| + assert_equal uri_class, URI.parse("#{scheme}://localhost").class + end + ensure + schemes = URI.const_get(:Schemes) + list.each do |scheme, | + schemes.send(:remove_const, schemes.escape(scheme)) + end end def test_regexp - assert_instance_of Regexp, URI.regexp - assert_instance_of Regexp, URI.regexp(['http']) - assert_equal URI.regexp, URI.regexp - assert_equal 'http://', 'x http:// x'.slice(URI.regexp) - assert_equal 'http://', 'x http:// x'.slice(URI.regexp(['http'])) - assert_equal 'http://', 'x http:// x ftp://'.slice(URI.regexp(['http'])) - assert_equal nil, 'http://'.slice(URI.regexp([])) - assert_equal nil, ''.slice(URI.regexp) - assert_equal nil, 'xxxx'.slice(URI.regexp) - assert_equal nil, ':'.slice(URI.regexp) - assert_equal 'From:', 'From:'.slice(URI.regexp) + EnvUtil.suppress_warning do + assert_instance_of Regexp, URI.regexp + assert_instance_of Regexp, URI.regexp(['http']) + assert_equal URI.regexp, URI.regexp + assert_equal 'http://', 'x http:// x'.slice(URI.regexp) + assert_equal 'http://', 'x http:// x'.slice(URI.regexp(['http'])) + assert_equal 'http://', 'x http:// x ftp://'.slice(URI.regexp(['http'])) + assert_equal nil, 'http://'.slice(URI.regexp([])) + assert_equal nil, ''.slice(URI.regexp) + assert_equal nil, 'xxxx'.slice(URI.regexp) + assert_equal nil, ':'.slice(URI.regexp) + assert_equal 'From:', 'From:'.slice(URI.regexp) + end end def test_kernel_uri @@ -49,7 +153,184 @@ class TestCommon < Test::Unit::TestCase assert_equal(expected, Kernel::URI("http://www.ruby-lang.org/")) assert_raise(NoMethodError) { Object.new.URI("http://www.ruby-lang.org/") } end -end + def test_parse_timeout + pre = ->(n) { + 'https://example.com/dir/' + 'a' * (n * 100) + '/##.jpg' + } + assert_linear_performance((1..3).map {|i| 10**i}, rehearsal: 1000, pre: pre) do |uri| + assert_raise(URI::InvalidURIError) do + URI.parse(uri) + end + end + end + + def test_encode_www_form_component + assert_equal("%00+%21%22%23%24%25%26%27%28%29*%2B%2C-.%2F09%3A%3B%3C%3D%3E%3F%40" \ + "AZ%5B%5C%5D%5E_%60az%7B%7C%7D%7E", + URI.encode_www_form_component("\x00 !\"\#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~")) + assert_equal("%95A", URI.encode_www_form_component( + "\x95\x41".force_encoding(Encoding::Shift_JIS))) + assert_equal("0B", URI.encode_www_form_component( + "\x30\x42".force_encoding(Encoding::UTF_16BE))) + assert_equal("%1B%24B%24%22%1B%28B", URI.encode_www_form_component( + "\e$B$\"\e(B".force_encoding(Encoding::ISO_2022_JP))) + + assert_equal("%E3%81%82", URI.encode_www_form_component( + "\u3042", Encoding::ASCII_8BIT)) + assert_equal("%82%A0", URI.encode_www_form_component( + "\u3042", Encoding::Windows_31J)) + assert_equal("%E3%81%82", URI.encode_www_form_component( + "\u3042", Encoding::UTF_8)) + + assert_equal("%82%A0", URI.encode_www_form_component( + "\u3042".encode("sjis"), Encoding::ASCII_8BIT)) + assert_equal("%A4%A2", URI.encode_www_form_component( + "\u3042".encode("sjis"), Encoding::EUC_JP)) + assert_equal("%E3%81%82", URI.encode_www_form_component( + "\u3042".encode("sjis"), Encoding::UTF_8)) + assert_equal("B0", URI.encode_www_form_component( + "\u3042".encode("sjis"), Encoding::UTF_16LE)) + assert_equal("%26%23730%3B", URI.encode_www_form_component( + "\u02DA", Encoding::WINDOWS_1252)) + + # invalid + assert_equal("%EF%BF%BD%EF%BF%BD", URI.encode_www_form_component( + "\xE3\x81\xFF", "utf-8")) + assert_equal("%E6%9F%8A%EF%BF%BD%EF%BF%BD", URI.encode_www_form_component( + "\x95\x41\xff\xff".force_encoding(Encoding::Shift_JIS), "utf-8")) + end + + def test_decode_www_form_component + assert_equal(" !\"\#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~", + URI.decode_www_form_component( + "%20+%21%22%23%24%25%26%27%28%29*%2B%2C-.%2F09%3A%3B%3C%3D%3E%3F%40" \ + "AZ%5B%5C%5D%5E_%60az%7B%7C%7D%7E")) + assert_equal("\xA1\xA2".force_encoding(Encoding::EUC_JP), + URI.decode_www_form_component("%A1%A2", "EUC-JP")) + assert_equal("\xE3\x81\x82\xE3\x81\x82".force_encoding("UTF-8"), + URI.decode_www_form_component("\xE3\x81\x82%E3%81%82".force_encoding("UTF-8"))) + + assert_raise(ArgumentError){URI.decode_www_form_component("%")} + assert_raise(ArgumentError){URI.decode_www_form_component("%a")} + assert_raise(ArgumentError){URI.decode_www_form_component("x%a_")} + assert_nothing_raised(ArgumentError){URI.decode_www_form_component("x"*(1024*1024))} + end + + def test_encode_uri_component + assert_equal("%00%20%21%22%23%24%25%26%27%28%29*%2B%2C-.%2F09%3A%3B%3C%3D%3E%3F%40" \ + "AZ%5B%5C%5D%5E_%60az%7B%7C%7D%7E", + URI.encode_uri_component("\x00 !\"\#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~")) + assert_equal("%95A", URI.encode_uri_component( + "\x95\x41".force_encoding(Encoding::Shift_JIS))) + assert_equal("0B", URI.encode_uri_component( + "\x30\x42".force_encoding(Encoding::UTF_16BE))) + assert_equal("%1B%24B%24%22%1B%28B", URI.encode_uri_component( + "\e$B$\"\e(B".force_encoding(Encoding::ISO_2022_JP))) + + assert_equal("%E3%81%82", URI.encode_uri_component( + "\u3042", Encoding::ASCII_8BIT)) + assert_equal("%82%A0", URI.encode_uri_component( + "\u3042", Encoding::Windows_31J)) + assert_equal("%E3%81%82", URI.encode_uri_component( + "\u3042", Encoding::UTF_8)) + + assert_equal("%82%A0", URI.encode_uri_component( + "\u3042".encode("sjis"), Encoding::ASCII_8BIT)) + assert_equal("%A4%A2", URI.encode_uri_component( + "\u3042".encode("sjis"), Encoding::EUC_JP)) + assert_equal("%E3%81%82", URI.encode_uri_component( + "\u3042".encode("sjis"), Encoding::UTF_8)) + assert_equal("B0", URI.encode_uri_component( + "\u3042".encode("sjis"), Encoding::UTF_16LE)) + assert_equal("%26%23730%3B", URI.encode_uri_component( + "\u02DA", Encoding::WINDOWS_1252)) + + # invalid + assert_equal("%EF%BF%BD%EF%BF%BD", URI.encode_uri_component( + "\xE3\x81\xFF", "utf-8")) + assert_equal("%E6%9F%8A%EF%BF%BD%EF%BF%BD", URI.encode_uri_component( + "\x95\x41\xff\xff".force_encoding(Encoding::Shift_JIS), "utf-8")) + end + + def test_decode_uri_component + assert_equal(" +!\"\#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~", + URI.decode_uri_component( + "%20+%21%22%23%24%25%26%27%28%29*%2B%2C-.%2F09%3A%3B%3C%3D%3E%3F%40" \ + "AZ%5B%5C%5D%5E_%60az%7B%7C%7D%7E")) + assert_equal("\xA1\xA2".force_encoding(Encoding::EUC_JP), + URI.decode_uri_component("%A1%A2", "EUC-JP")) + assert_equal("\xE3\x81\x82\xE3\x81\x82".force_encoding("UTF-8"), + URI.decode_uri_component("\xE3\x81\x82%E3%81%82".force_encoding("UTF-8"))) + + assert_raise(ArgumentError){URI.decode_uri_component("%")} + assert_raise(ArgumentError){URI.decode_uri_component("%a")} + assert_raise(ArgumentError){URI.decode_uri_component("x%a_")} + assert_nothing_raised(ArgumentError){URI.decode_uri_component("x"*(1024*1024))} + end + + def test_encode_www_form + assert_equal("a=1", URI.encode_www_form("a" => "1")) + assert_equal("a=1", URI.encode_www_form(a: 1)) + assert_equal("a=1", URI.encode_www_form([["a", "1"]])) + assert_equal("a=1", URI.encode_www_form([[:a, 1]])) + expected = "a=1&%E3%81%82=%E6%BC%A2" + assert_equal(expected, URI.encode_www_form("a" => "1", "\u3042" => "\u6F22")) + assert_equal(expected, URI.encode_www_form(a: 1, :"\u3042" => "\u6F22")) + assert_equal(expected, URI.encode_www_form([["a", "1"], ["\u3042", "\u6F22"]])) + assert_equal(expected, URI.encode_www_form([[:a, 1], [:"\u3042", "\u6F22"]])) + assert_equal("a=1&%82%A0=%8A%BF", + URI.encode_www_form({"a" => "1", "\u3042" => "\u6F22"}, "sjis")) + + assert_equal('+a+=+1+', URI.encode_www_form([[' a ', ' 1 ']])) + assert_equal('text=x%0Ay', URI.encode_www_form([['text', "x\u000Ay"]])) + assert_equal('constellation=Bo%C3%B6tes', URI.encode_www_form([['constellation', "Bo\u00F6tes"]])) + assert_equal('name=%00value', URI.encode_www_form([['name', "\u0000value"]])) + assert_equal('Cipher=c%3D%28m%5Ee%29%25n', URI.encode_www_form([['Cipher', 'c=(m^e)%n']])) + assert_equal('&', URI.encode_www_form([['', nil], ['', nil]])) + assert_equal('&=', URI.encode_www_form([['', nil], ['', '']])) + assert_equal('=&', URI.encode_www_form([['', ''], ['', nil]])) + assert_equal('=&=', URI.encode_www_form([['', ''], ['', '']])) + assert_equal('', URI.encode_www_form([['', nil]])) + assert_equal('', URI.encode_www_form([])) + assert_equal('=', URI.encode_www_form([['', '']])) + assert_equal('a%26b=1&c=2%3B3&e=4', URI.encode_www_form([['a&b', '1'], ['c', '2;3'], ['e', '4']])) + assert_equal('image&title&price', URI.encode_www_form([['image', nil], ['title', nil], ['price', nil]])) + + assert_equal("q=ruby&lang=en", URI.encode_www_form([["q", "ruby"], ["lang", "en"]])) + assert_equal("q=ruby&lang=en", URI.encode_www_form("q" => "ruby", "lang" => "en")) + assert_equal("q=ruby&q=perl&lang=en", URI.encode_www_form("q" => ["ruby", "perl"], "lang" => "en")) + assert_equal("q=ruby&q=perl&lang=en", URI.encode_www_form([["q", "ruby"], ["q", "perl"], ["lang", "en"]])) + end + + def test_decode_www_form + assert_equal([%w[a 1], %w[a 2]], URI.decode_www_form("a=1&a=2")) + assert_equal([%w[a 1;a=2]], URI.decode_www_form("a=1;a=2")) + assert_equal([%w[a 1], ['', ''], %w[a 2]], URI.decode_www_form("a=1&&a=2")) + assert_raise(ArgumentError){URI.decode_www_form("\u3042")} + assert_equal([%w[a 1], ["\u3042", "\u6F22"]], + URI.decode_www_form("a=1&%E3%81%82=%E6%BC%A2")) + assert_equal([%w[a 1], ["\uFFFD%8", "\uFFFD"]], + URI.decode_www_form("a=1&%E3%81%8=%E6%BC")) + assert_equal([%w[?a 1], %w[a 2]], URI.decode_www_form("?a=1&a=2")) + assert_equal([], URI.decode_www_form("")) + assert_equal([%w[% 1]], URI.decode_www_form("%=1")) + assert_equal([%w[a %]], URI.decode_www_form("a=%")) + assert_equal([%w[a 1], %w[% 2]], URI.decode_www_form("a=1&%=2")) + assert_equal([%w[a 1], %w[b %]], URI.decode_www_form("a=1&b=%")) + assert_equal([['a', ''], ['b', '']], URI.decode_www_form("a&b")) + bug4098 = '[ruby-core:33464]' + assert_equal([['a', 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'], ['b', '']], URI.decode_www_form("a=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&b"), bug4098) + + assert_raise(ArgumentError){ URI.decode_www_form("a=1&%82%A0=%8A%BF", "x-sjis") } + assert_equal([["a", "1"], [s("\x82\xA0"), s("\x8a\xBF")]], + URI.decode_www_form("a=1&%82%A0=%8A%BF", "sjis")) + assert_equal([["a", "1"], [s("\x82\xA0"), s("\x8a\xBF")], %w[_charset_ sjis], [s("\x82\xA1"), s("\x8a\xC0")]], + URI.decode_www_form("a=1&%82%A0=%8A%BF&_charset_=sjis&%82%A1=%8A%C0", use__charset_: true)) + assert_equal([["", "isindex"], ["a", "1"]], + URI.decode_www_form("isindex&a=1", isindex: true)) + end + private + def s(str) str.force_encoding(Encoding::Windows_31J); end end diff --git a/test/uri/test_file.rb b/test/uri/test_file.rb new file mode 100644 index 0000000000..7e542b652d --- /dev/null +++ b/test/uri/test_file.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: false +require 'test/unit' +require 'uri/file' + +class URI::TestFile < Test::Unit::TestCase + def test_parse + u = URI("file://example.com/file") + assert_equal "/file", u.path + + u = URI("file://localhost/file") + assert_equal "/file", u.path + assert_equal "file:///file", u.to_s + + u = URI("file://localhost:30/file") + assert_equal "", u.host + assert_equal nil, u.port + assert_equal "/file", u.path + assert_equal "file:///file", u.to_s + + u = URI("file:///file") + assert_equal "/file", u.path + assert_equal "file:///file", u.to_s + + u = URI("file:/file") + assert_equal "/file", u.path + assert_equal "file:///file", u.to_s + + u = URI("file://foo:pass@example.com/file") + assert_equal "/file", u.path + assert_equal nil, u.user + assert_equal nil, u.password + + u = URI("file:///c:/path/to/file") + assert_equal "/c:/path/to/file", u.path + + # this form is not supported + u = URI("file:c:/path/to/file") + assert_equal "c:/path/to/file", u.opaque + + end + + def test_build + u = URI::File.build(scheme: "file", host: "example.com", path:"/file") + assert_equal "/file", u.path + assert_equal "file://example.com/file", u.to_s + assert_raise(URI::InvalidURIError){ u.user = "foo" } + assert_raise(URI::InvalidURIError){ u.password = "foo" } + assert_raise(URI::InvalidURIError){ u.userinfo = "foo" } + assert_raise(URI::InvalidURIError){ URI::File.build(scheme: "file", userinfo: "foo", host: "example.com", path:"/file") } + + u = URI::File.build(scheme: "file", path:"/file") + assert_equal "", u.host + assert_equal "/file", u.path + assert_equal "file:///file", u.to_s + + u = URI::File.build(scheme: "file", host: "localhost", path:"/file") + assert_equal "", u.host + assert_equal "/file", u.path + assert_equal "file:///file", u.to_s + + u = URI::File.build(scheme: "file", path:"/file", port: 30) + assert_equal "", u.host + assert_equal nil, u.port + assert_equal "/file", u.path + assert_equal "file:///file", u.to_s + end +end diff --git a/test/uri/test_ftp.rb b/test/uri/test_ftp.rb index 10abd29502..3ad7864490 100644 --- a/test/uri/test_ftp.rb +++ b/test/uri/test_ftp.rb @@ -1,10 +1,8 @@ +# frozen_string_literal: false require 'test/unit' require 'uri/ftp' -module URI - - -class TestFTP < Test::Unit::TestCase +class URI::TestFTP < Test::Unit::TestCase def setup end @@ -14,7 +12,7 @@ class TestFTP < Test::Unit::TestCase exp = [ 'ftp', - 'user:pass', 'host.com', URI::FTP.default_port, + 'user:pass', 'host.com', URI::FTP.default_port, 'abc/def', nil, ] ary = [ @@ -27,24 +25,28 @@ class TestFTP < Test::Unit::TestCase assert_equal('pass', url.password) end + def test_parse_invalid + assert_raise(URI::InvalidURIError) {URI.parse('ftp:example')} + end + def test_paths - # If you think what's below is wrong, please read RubyForge bug 2055, + # If you think what's below is wrong, please read RubyForge bug 2055, # RFC 1738 section 3.2.2, and RFC 2396. u = URI.parse('ftp://ftp.example.com/foo/bar/file.ext') - assert_equal(u.path, 'foo/bar/file.ext') + assert_equal('foo/bar/file.ext', u.path) u = URI.parse('ftp://ftp.example.com//foo/bar/file.ext') - assert_equal(u.path, '/foo/bar/file.ext') + assert_equal('/foo/bar/file.ext', u.path) u = URI.parse('ftp://ftp.example.com/%2Ffoo/bar/file.ext') - assert_equal(u.path, '/foo/bar/file.ext') + assert_equal('/foo/bar/file.ext', u.path) end def test_assemble # uri/ftp is conservative and uses the older RFC 1738 rules, rather than # assuming everyone else has implemented RFC 2396. - uri = URI::FTP.build(['user:password', 'ftp.example.com', nil, + uri = URI::FTP.build(['user:password', 'ftp.example.com', nil, '/path/file.zip', 'i']) - assert_equal(uri.to_s, - 'ftp://user:password@ftp.example.com/%2Fpath/file.zip;type=i') + assert_equal('ftp://user:password@ftp.example.com/%2Fpath/file.zip;type=i', + uri.to_s) end def test_select @@ -52,11 +54,8 @@ class TestFTP < Test::Unit::TestCase u = URI.parse('ftp://a.b.c/') ary = u.component.collect {|c| u.send(c)} assert_equal(ary, u.select(*u.component)) - assert_raises(ArgumentError) do + assert_raise(ArgumentError) do u.select(:scheme, :host, :not_exist, :port) end end end - - -end diff --git a/test/uri/test_generic.rb b/test/uri/test_generic.rb index 5cd4c7f7ae..94eea71b51 100644 --- a/test/uri/test_generic.rb +++ b/test/uri/test_generic.rb @@ -1,10 +1,9 @@ +# frozen_string_literal: false require 'test/unit' +require 'envutil' require 'uri' -module URI - - -class TestGeneric < Test::Unit::TestCase +class URI::TestGeneric < Test::Unit::TestCase def setup @url = 'http://a/b/c/d;p?q' @base_url = URI.parse(@url) @@ -17,14 +16,37 @@ class TestGeneric < Test::Unit::TestCase uri.class.component.collect {|c| uri.send(c)} end + def test_to_s + exp = 'http://example.com/'.freeze + str = URI(exp).to_s + assert_equal exp, str + assert_not_predicate str, :frozen?, '[ruby-core:71785] [Bug #11759]' + + assert_equal "file:///foo", URI("file:///foo").to_s + assert_equal "postgres:///foo", URI("postgres:///foo").to_s + assert_equal "http:///foo", URI("http:///foo").to_s + assert_equal "http:/foo", URI("http:/foo").to_s + + uri = URI('rel_path') + assert_equal "rel_path", uri.to_s + uri.scheme = 'http' + assert_equal "http:rel_path", uri.to_s + uri.host = 'h' + assert_equal "http://h/rel_path", uri.to_s + uri.port = 8080 + assert_equal "http://h:8080/rel_path", uri.to_s + uri.host = nil + assert_equal "http::8080/rel_path", uri.to_s + end + def test_parse # 0 assert_kind_of(URI::HTTP, @base_url) exp = [ - 'http', - nil, 'a', URI::HTTP.default_port, - '/b/c/d;p', + 'http', + nil, 'a', URI::HTTP.default_port, + '/b/c/d;p', 'q', nil ] @@ -36,8 +58,8 @@ class TestGeneric < Test::Unit::TestCase assert_kind_of(URI::FTP, url) exp = [ - 'ftp', - nil, 'ftp.is.co.za', URI::FTP.default_port, + 'ftp', + nil, 'ftp.is.co.za', URI::FTP.default_port, 'rfc/rfc1808.txt', nil, ] ary = uri_to_ary(url) @@ -47,8 +69,8 @@ class TestGeneric < Test::Unit::TestCase assert_kind_of(URI::FTP, url) exp = [ - 'ftp', - nil, 'ftp.is.co.za', URI::FTP.default_port, + 'ftp', + nil, 'ftp.is.co.za', URI::FTP.default_port, '/rfc/rfc1808.txt', nil, ] ary = uri_to_ary(url) @@ -59,7 +81,7 @@ class TestGeneric < Test::Unit::TestCase assert_kind_of(URI::Generic, url) exp = [ - 'gopher', + 'gopher', nil, 'spinaltap.micro.umn.edu', nil, nil, '/00/Weather/California/Los%20Angeles', nil, nil, @@ -73,9 +95,9 @@ class TestGeneric < Test::Unit::TestCase assert_kind_of(URI::HTTP, url) exp = [ - 'http', - nil, 'www.math.uio.no', URI::HTTP.default_port, - '/faq/compression-faq/part1.html', + 'http', + nil, 'www.math.uio.no', URI::HTTP.default_port, + '/faq/compression-faq/part1.html', nil, nil ] @@ -87,7 +109,7 @@ class TestGeneric < Test::Unit::TestCase assert_kind_of(URI::Generic, url) exp = [ - 'mailto', + 'mailto', 'mduerst@ifi.unizh.ch', [] ] @@ -99,8 +121,8 @@ class TestGeneric < Test::Unit::TestCase assert_kind_of(URI::Generic, url) exp = [ - 'news', - nil, nil, nil, nil, + 'news', + nil, nil, nil, nil, nil, 'comp.infosystems.www.servers.unix', nil, nil @@ -113,8 +135,8 @@ class TestGeneric < Test::Unit::TestCase assert_kind_of(URI::Generic, url) exp = [ - 'telnet', - nil, 'melvyl.ucop.edu', nil, nil, + 'telnet', + nil, 'melvyl.ucop.edu', nil, nil, '/', nil, nil, nil @@ -124,14 +146,14 @@ class TestGeneric < Test::Unit::TestCase # 7 # reported by Mr. Kubota <em6t-kbt@asahi-net.or.jp> - assert_raises(URI::InvalidURIError) { URI.parse('http://a_b:80/') } - assert_raises(URI::InvalidURIError) { URI.parse('http://a_b/') } + assert_nothing_raised(URI::InvalidURIError) { URI.parse('http://a_b:80/') } + assert_nothing_raised(URI::InvalidURIError) { URI.parse('http://a_b/') } # 8 # reported by m_seki - uri = URI.parse('file:///foo/bar.txt') + url = URI.parse('file:///foo/bar.txt') assert_kind_of(URI::Generic, url) - uri = URI.parse('file:/foo/bar.txt') + url = URI.parse('file:/foo/bar.txt') assert_kind_of(URI::Generic, url) # 9 @@ -147,6 +169,30 @@ class TestGeneric < Test::Unit::TestCase assert_equal(nil, url.user) assert_equal(nil, url.password) assert_equal(nil, url.userinfo) + + # sec-156615 + url = URI.parse('http:////example.com') + # must be empty string to identify as path-abempty, not path-absolute + assert_equal('', url.host) + assert_equal('http:////example.com', url.to_s) + + # sec-2957667 + url = URI.parse('http://user:pass@example.com').merge('//example.net') + assert_equal('http://example.net', url.to_s) + assert_nil(url.userinfo) + url = URI.join('http://user:pass@example.com', '//example.net') + assert_equal('http://example.net', url.to_s) + assert_nil(url.userinfo) + url = URI.parse('http://user:pass@example.com') + '//example.net' + assert_equal('http://example.net', url.to_s) + assert_nil(url.userinfo) + end + + def test_parse_scheme_with_symbols + # Valid schemes from https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml + assert_equal 'ms-search', URI.parse('ms-search://localhost').scheme + assert_equal 'microsoft.windows.camera', URI.parse('microsoft.windows.camera://localhost').scheme + assert_equal 'coaps+ws', URI.parse('coaps+ws:localhost').scheme end def test_merge @@ -155,15 +201,31 @@ class TestGeneric < Test::Unit::TestCase u3 = URI.parse('http://foo/bar') u4 = URI.parse('http://foo/bar/') - assert_equal(URI.parse('http://foo/baz'), u1 + 'baz') - assert_equal(URI.parse('http://foo/baz'), u2 + 'baz') - assert_equal(URI.parse('http://foo/baz'), u3 + 'baz') - assert_equal(URI.parse('http://foo/bar/baz'), u4 + 'baz') - - assert_equal(URI.parse('http://foo/baz'), u1 + '/baz') - assert_equal(URI.parse('http://foo/baz'), u2 + '/baz') - assert_equal(URI.parse('http://foo/baz'), u3 + '/baz') - assert_equal(URI.parse('http://foo/baz'), u4 + '/baz') + { + u1 => { + 'baz' => 'http://foo/baz', + '/baz' => 'http://foo/baz', + }, + u2 => { + 'baz' => 'http://foo/baz', + '/baz' => 'http://foo/baz', + }, + u3 => { + 'baz' => 'http://foo/baz', + '/baz' => 'http://foo/baz', + }, + u4 => { + 'baz' => 'http://foo/bar/baz', + '/baz' => 'http://foo/baz', + }, + }.each { |base, map| + map.each { |url, result| + expected = URI.parse(result) + uri = URI.parse(url) + assert_equal expected, base + url, "<#{base}> + #{url.inspect} to become <#{expected}>" + assert_equal expected, base + uri, "<#{base}> + <#{uri}> to become <#{expected}>" + } + } url = URI.parse('http://hoge/a.html') + 'b.html' assert_equal('http://hoge/b.html', url.to_s, "[ruby-dev:11508]") @@ -178,11 +240,14 @@ class TestGeneric < Test::Unit::TestCase u = URI.parse('http://foo/bar/baz') assert_equal(nil, u.merge!("")) assert_equal(nil, u.merge!(u)) - assert(nil != u.merge!(".")) + refute_nil(u.merge!(".")) assert_equal('http://foo/bar/', u.to_s) - assert(nil != u.merge!("../baz")) + refute_nil(u.merge!("../baz")) assert_equal('http://foo/baz', u.to_s) + url = URI.parse('http://a/b//c') + 'd//e' + assert_equal('http://a/b//d//e', url.to_s) + u0 = URI.parse('mailto:foo@example.com') u1 = URI.parse('mailto:foo@example.com#bar') assert_equal(uri_to_ary(u0 + '#bar'), uri_to_ary(u1), "[ruby-dev:23628]") @@ -213,6 +278,16 @@ class TestGeneric < Test::Unit::TestCase assert_equal(u0, u1) end + def test_merge_authority + u = URI.parse('http://user:pass@example.com:8080') + u0 = URI.parse('http://new.example.org/path') + u1 = u.merge('//new.example.org/path') + assert_equal(u0, u1) + u0 = URI.parse('http://other@example.net') + u1 = u.merge('//other@example.net') + assert_equal(u0, u1) + end + def test_route url = URI.parse('http://hoge/a.html').route_to('http://hoge/b.html') assert_equal('b.html', url.to_s) @@ -231,8 +306,22 @@ class TestGeneric < Test::Unit::TestCase url = URI.parse('http://hoge/a/b/').route_to('http://MOGE/b/') assert_equal('//MOGE/b/', url.to_s) + url = URI.parse('http://hoge/b').route_to('http://hoge/b/') + assert_equal('b/', url.to_s) + url = URI.parse('http://hoge/b/a').route_to('http://hoge/b/') + assert_equal('./', url.to_s) + url = URI.parse('http://hoge/b/').route_to('http://hoge/b') + assert_equal('../b', url.to_s) + url = URI.parse('http://hoge/b').route_to('http://hoge/b:c') + assert_equal('./b:c', url.to_s) + + url = URI.parse('http://hoge/b//c').route_to('http://hoge/b/c') + assert_equal('../c', url.to_s) + url = URI.parse('file:///a/b/').route_to('file:///a/b/') assert_equal('', url.to_s) + url = URI.parse('file:///a/b/').route_to('file:///a/b') + assert_equal('../b', url.to_s) url = URI.parse('mailto:foo@example.com').route_to('mailto:foo@example.com#bar') assert_equal('#bar', url.to_s) @@ -270,7 +359,7 @@ class TestGeneric < Test::Unit::TestCase assert_equal('http://a/b/c/g', url.to_s) url = @base_url.route_to('http://a/b/c/g') assert_kind_of(URI::Generic, url) - assert('./g' != url.to_s) # ok + refute_equal('./g', url.to_s) # ok assert_equal('g', url.to_s) # http://a/b/c/d;p?q @@ -289,7 +378,7 @@ class TestGeneric < Test::Unit::TestCase assert_equal('http://a/g', url.to_s) url = @base_url.route_to('http://a/g') assert_kind_of(URI::Generic, url) - assert('/g' != url.to_s) # ok + refute_equal('/g', url.to_s) # ok assert_equal('../../g', url.to_s) # http://a/b/c/d;p?q @@ -380,7 +469,7 @@ class TestGeneric < Test::Unit::TestCase assert_equal('http://a/b/c/', url.to_s) url = @base_url.route_to('http://a/b/c/') assert_kind_of(URI::Generic, url) - assert('.' != url.to_s) # ok + refute_equal('.', url.to_s) # ok assert_equal('./', url.to_s) # http://a/b/c/d;p?q @@ -399,7 +488,7 @@ class TestGeneric < Test::Unit::TestCase assert_equal('http://a/b/', url.to_s) url = @base_url.route_to('http://a/b/') assert_kind_of(URI::Generic, url) - assert('..' != url.to_s) # ok + refute_equal('..', url.to_s) # ok assert_equal('../', url.to_s) # http://a/b/c/d;p?q @@ -427,7 +516,7 @@ class TestGeneric < Test::Unit::TestCase assert_equal('http://a/', url.to_s) url = @base_url.route_to('http://a/') assert_kind_of(URI::Generic, url) - assert('../..' != url.to_s) # ok + refute_equal('../..', url.to_s) # ok assert_equal('../../', url.to_s) # http://a/b/c/d;p?q @@ -518,7 +607,7 @@ class TestGeneric < Test::Unit::TestCase assert_equal('http://a/g', url.to_s) url = @base_url.route_to('http://a/g') assert_kind_of(URI::Generic, url) - assert('../../../g' != url.to_s) # ok? yes, it confuses you + refute_equal('../../../g', url.to_s) # ok? yes, it confuses you assert_equal('../../g', url.to_s) # and it is clearly # http://a/b/c/d;p?q @@ -528,7 +617,7 @@ class TestGeneric < Test::Unit::TestCase assert_equal('http://a/g', url.to_s) url = @base_url.route_to('http://a/g') assert_kind_of(URI::Generic, url) - assert('../../../../g' != url.to_s) # ok? yes, it confuses you + refute_equal('../../../../g', url.to_s) # ok? yes, it confuses you assert_equal('../../g', url.to_s) # and it is clearly # http://a/b/c/d;p?q @@ -538,7 +627,7 @@ class TestGeneric < Test::Unit::TestCase assert_equal('http://a/b/g', url.to_s) url = @base_url.route_to('http://a/b/g') assert_kind_of(URI::Generic, url) - assert('./../g' != url.to_s) # ok + refute_equal('./../g', url.to_s) # ok assert_equal('../g', url.to_s) # http://a/b/c/d;p?q @@ -548,7 +637,7 @@ class TestGeneric < Test::Unit::TestCase assert_equal('http://a/b/c/g/', url.to_s) url = @base_url.route_to('http://a/b/c/g/') assert_kind_of(URI::Generic, url) - assert('./g/.' != url.to_s) # ok + refute_equal('./g/.', url.to_s) # ok assert_equal('g/', url.to_s) # http://a/b/c/d;p?q @@ -558,7 +647,7 @@ class TestGeneric < Test::Unit::TestCase assert_equal('http://a/b/c/g/h', url.to_s) url = @base_url.route_to('http://a/b/c/g/h') assert_kind_of(URI::Generic, url) - assert('g/./h' != url.to_s) # ok + refute_equal('g/./h', url.to_s) # ok assert_equal('g/h', url.to_s) # http://a/b/c/d;p?q @@ -568,7 +657,7 @@ class TestGeneric < Test::Unit::TestCase assert_equal('http://a/b/c/h', url.to_s) url = @base_url.route_to('http://a/b/c/h') assert_kind_of(URI::Generic, url) - assert('g/../h' != url.to_s) # ok + refute_equal('g/../h', url.to_s) # ok assert_equal('h', url.to_s) # http://a/b/c/d;p?q @@ -578,7 +667,7 @@ class TestGeneric < Test::Unit::TestCase assert_equal('http://a/b/c/g;x=1/y', url.to_s) url = @base_url.route_to('http://a/b/c/g;x=1/y') assert_kind_of(URI::Generic, url) - assert('g;x=1/./y' != url.to_s) # ok + refute_equal('g;x=1/./y', url.to_s) # ok assert_equal('g;x=1/y', url.to_s) # http://a/b/c/d;p?q @@ -588,7 +677,7 @@ class TestGeneric < Test::Unit::TestCase assert_equal('http://a/b/c/y', url.to_s) url = @base_url.route_to('http://a/b/c/y') assert_kind_of(URI::Generic, url) - assert('g;x=1/../y' != url.to_s) # ok + refute_equal('g;x=1/../y', url.to_s) # ok assert_equal('y', url.to_s) # http://a/b/c/d;p?q @@ -662,43 +751,369 @@ class TestGeneric < Test::Unit::TestCase def test_set_component uri = URI.parse('http://foo:bar@baz') assert_equal('oof', uri.user = 'oof') - assert_equal('http://oof:bar@baz', uri.to_s) + assert_equal('http://oof@baz', uri.to_s) assert_equal('rab', uri.password = 'rab') assert_equal('http://oof:rab@baz', uri.to_s) assert_equal('foo', uri.userinfo = 'foo') - assert_equal('http://foo:rab@baz', uri.to_s) + assert_equal('http://foo@baz', uri.to_s) assert_equal(['foo', 'bar'], uri.userinfo = ['foo', 'bar']) assert_equal('http://foo:bar@baz', uri.to_s) assert_equal(['foo'], uri.userinfo = ['foo']) - assert_equal('http://foo:bar@baz', uri.to_s) + assert_equal('http://foo@baz', uri.to_s) assert_equal('zab', uri.host = 'zab') - assert_equal('http://foo:bar@zab', uri.to_s) + assert_equal('http://zab', uri.to_s) + uri.userinfo = ['foo', 'bar'] + uri.port = "" + assert_nil(uri.port) + uri.port = "80" + assert_equal(80, uri.port) + uri.port = "080" + assert_equal(80, uri.port) + uri.port = " 080 " + assert_equal(80, uri.port) assert_equal(8080, uri.port = 8080) - assert_equal('http://foo:bar@zab:8080', uri.to_s) + assert_equal('http://zab:8080', uri.to_s) + uri = URI.parse('http://foo:bar@zab:8080') assert_equal('/', uri.path = '/') assert_equal('http://foo:bar@zab:8080/', uri.to_s) assert_equal('a=1', uri.query = 'a=1') assert_equal('http://foo:bar@zab:8080/?a=1', uri.to_s) assert_equal('b123', uri.fragment = 'b123') assert_equal('http://foo:bar@zab:8080/?a=1#b123', uri.to_s) + assert_equal('a[]=1', uri.query = 'a[]=1') + assert_equal('http://foo:bar@zab:8080/?a[]=1#b123', uri.to_s) + uri = URI.parse('http://foo:bar@zab:8080/?a[]=1#b123') + assert_equal('http://foo:bar@zab:8080/?a[]=1#b123', uri.to_s) uri = URI.parse('http://example.com') - assert_raises(URI::InvalidURIError) { uri.password = 'bar' } + assert_raise(URI::InvalidURIError) { uri.password = 'bar' } + assert_equal("foo\nbar", uri.query = "foo\nbar") uri.userinfo = 'foo:bar' - assert_equal('http://foo:bar@example.com', uri.to_s) - assert_raises(URI::InvalidURIError) { uri.registry = 'bar' } - assert_raises(URI::InvalidURIError) { uri.opaque = 'bar' } + assert_equal('http://foo:bar@example.com?foobar', uri.to_s) + assert_raise(URI::InvalidURIError) { uri.registry = 'bar' } + assert_raise(URI::InvalidURIError) { uri.opaque = 'bar' } uri = URI.parse('mailto:foo@example.com') - assert_raises(URI::InvalidURIError) { uri.user = 'bar' } - assert_raises(URI::InvalidURIError) { uri.password = 'bar' } - assert_raises(URI::InvalidURIError) { uri.userinfo = ['bar', 'baz'] } - assert_raises(URI::InvalidURIError) { uri.host = 'bar' } - assert_raises(URI::InvalidURIError) { uri.port = 'bar' } - assert_raises(URI::InvalidURIError) { uri.path = 'bar' } - assert_raises(URI::InvalidURIError) { uri.query = 'bar' } + assert_raise(URI::InvalidURIError) { uri.user = 'bar' } + assert_raise(URI::InvalidURIError) { uri.password = 'bar' } + assert_raise(URI::InvalidURIError) { uri.userinfo = ['bar', 'baz'] } + assert_raise(URI::InvalidURIError) { uri.host = 'bar' } + assert_raise(URI::InvalidURIError) { uri.port = 'bar' } + assert_raise(URI::InvalidURIError) { uri.path = 'bar' } + assert_raise(URI::InvalidURIError) { uri.query = 'bar' } + + uri = URI.parse('foo:bar') + assert_raise(URI::InvalidComponentError) { uri.opaque = '/baz' } + uri.opaque = 'xyzzy' + assert_equal('foo:xyzzy', uri.to_s) + end + + def test_bad_password_component + uri = URI.parse('http://foo:bar@baz') + password = 'foo@bar' + e = assert_raise(URI::InvalidComponentError) do + uri.password = password + end + refute_match Regexp.new(password), e.message + end + + def test_set_scheme + uri = URI.parse 'HTTP://example' + + assert_equal 'http://example', uri.to_s + end + + def test_hierarchical + hierarchical = URI.parse('http://a.b.c/example') + opaque = URI.parse('mailto:mduerst@ifi.unizh.ch') + + assert_predicate hierarchical, :hierarchical? + refute_predicate opaque, :hierarchical? + end + + def test_absolute + abs_uri = URI.parse('http://a.b.c/') + not_abs = URI.parse('a.b.c') + + refute_predicate not_abs, :absolute? + + assert_predicate abs_uri, :absolute + assert_predicate abs_uri, :absolute? + end + + def test_ipv6 + assert_equal("[::1]", URI("http://[::1]/bar/baz").host) + assert_equal("::1", URI("http://[::1]/bar/baz").hostname) + + u = URI("http://foo/bar") + assert_equal("http://foo/bar", u.to_s) + u.hostname = "[::1]" + assert_equal("http://[::1]/bar", u.to_s) + u.hostname = "::1" + assert_equal("http://[::1]/bar", u.to_s) + + u = URI("file://foo/bar") + u.hostname = '' + assert_equal("file:///bar", u.to_s) + end + + def test_build + u = URI::Generic.build(['http', nil, 'example.com', 80, nil, '/foo', nil, nil, nil]) + assert_equal('http://example.com:80/foo', u.to_s) + assert_equal(Encoding::UTF_8, u.to_s.encoding) + + u = URI::Generic.build(:port => "5432") + assert_equal(":5432", u.to_s) + assert_equal(5432, u.port) + + u = URI::Generic.build(:scheme => "http", :host => "::1", :path => "/bar/baz") + assert_equal("http://[::1]/bar/baz", u.to_s) + assert_equal("[::1]", u.host) + assert_equal("::1", u.hostname) + + u = URI::Generic.build(:scheme => "http", :host => "[::1]", :path => "/bar/baz") + assert_equal("http://[::1]/bar/baz", u.to_s) + assert_equal("[::1]", u.host) + assert_equal("::1", u.hostname) + + assert_raise_with_message(ArgumentError, /URI::Generic/) { + URI::Generic.build(nil) + } + + c = Class.new(URI::Generic) do + def self.component; raise; end + end + expected = /\(#{URI::Generic::COMPONENT.join(', ')}\)/ + message = "fallback to URI::Generic::COMPONENT if component raised" + assert_raise_with_message(ArgumentError, expected, message) { + c.build(nil) + } + end + + def test_build2 + u = URI::Generic.build2(path: "/foo bar/baz") + assert_equal('/foo%20bar/baz', u.to_s) + + u = URI::Generic.build2(['http', nil, 'example.com', 80, nil, '/foo bar' , nil, nil, nil]) + assert_equal('http://example.com:80/foo%20bar', u.to_s) + end + + # 192.0.2.0/24 is TEST-NET. [RFC3330] + + def test_find_proxy_bad_uri + assert_raise(URI::BadURIError){ URI("foo").find_proxy } + end + + def test_find_proxy_no_env + with_proxy_env({}) {|env| + assert_nil(URI("http://192.0.2.1/").find_proxy(env)) + assert_nil(URI("ftp://192.0.2.1/").find_proxy(env)) + } + end + + def test_find_proxy + with_proxy_env('http_proxy'=>'http://127.0.0.1:8080') {|env| + assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy(env)) + assert_nil(URI("ftp://192.0.2.1/").find_proxy(env)) + } + with_proxy_env('ftp_proxy'=>'http://127.0.0.1:8080') {|env| + assert_nil(URI("http://192.0.2.1/").find_proxy(env)) + assert_equal(URI('http://127.0.0.1:8080'), URI("ftp://192.0.2.1/").find_proxy(env)) + } + end + + def test_find_proxy_get + with_proxy_env('REQUEST_METHOD'=>'GET') {|env| + assert_nil(URI("http://192.0.2.1/").find_proxy(env)) + } + with_proxy_env('CGI_HTTP_PROXY'=>'http://127.0.0.1:8080', 'REQUEST_METHOD'=>'GET') {|env| + assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy(env)) + } + end + + def test_find_proxy_no_proxy + getaddress = IPSocket.method(:getaddress) + example_address = nil + IPSocket.singleton_class.class_eval do + undef getaddress + define_method(:getaddress) do |host| + case host + when "example.org", "www.example.org" + example_address + when /\A\d+(?:\.\d+){3}\z/ + host + else + raise host + end + end + end + + with_proxy_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'192.0.2.2') {|env| + assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy(env)) + assert_nil(URI("http://192.0.2.2/").find_proxy(env)) + + example_address = "192.0.2.1" + assert_equal(URI('http://127.0.0.1:8080'), URI.parse("http://example.org").find_proxy(env)) + example_address = "192.0.2.2" + assert_nil(URI.parse("http://example.org").find_proxy(env)) + } + with_proxy_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'example.org') {|env| + assert_nil(URI("http://example.org/").find_proxy(env)) + assert_nil(URI("http://www.example.org/").find_proxy(env)) + } + with_proxy_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'.example.org') {|env| + assert_equal(URI('http://127.0.0.1:8080'), URI("http://example.org/").find_proxy(env)) + assert_nil(URI("http://www.example.org/").find_proxy(env)) + } + ensure + IPSocket.singleton_class.class_eval do + undef getaddress + define_method(:getaddress, getaddress) + end end -end + def test_find_proxy_no_proxy_cidr + with_proxy_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'192.0.2.0/24') {|env| + assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.1.1/").find_proxy(env)) + assert_nil(URI("http://192.0.2.1/").find_proxy(env)) + assert_nil(URI("http://192.0.2.2/").find_proxy(env)) + } + end + + def test_find_proxy_bad_value + with_proxy_env('http_proxy'=>'') {|env| + assert_nil(URI("http://192.0.2.1/").find_proxy(env)) + assert_nil(URI("ftp://192.0.2.1/").find_proxy(env)) + } + with_proxy_env('ftp_proxy'=>'') {|env| + assert_nil(URI("http://192.0.2.1/").find_proxy(env)) + assert_nil(URI("ftp://192.0.2.1/").find_proxy(env)) + } + end + + def test_find_proxy_case_sensitive_env + with_proxy_env_case_sensitive('http_proxy'=>'http://127.0.0.1:8080', 'REQUEST_METHOD'=>'GET') {|env| + assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy(env)) + } + with_proxy_env_case_sensitive('HTTP_PROXY'=>'http://127.0.0.1:8081', 'REQUEST_METHOD'=>'GET') {|env| + assert_nil(URI("http://192.0.2.1/").find_proxy(env)) + } + with_proxy_env_case_sensitive('http_proxy'=>'http://127.0.0.1:8080', 'HTTP_PROXY'=>'http://127.0.0.1:8081', 'REQUEST_METHOD'=>'GET') {|env| + assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy(env)) + } + end + + def test_use_proxy_p + [ + ['example.com', nil, 80, '', true], + ['example.com', nil, 80, 'example.com:80', false], + ['example.com', nil, 80, 'example.org,example.com:80,example.net', false], + ['foo.example.com', nil, 80, 'example.com', false], + ['foo.example.com', nil, 80, '.example.com', false], + ['example.com', nil, 80, '.example.com', true], + ['xample.com', nil, 80, '.example.com', true], + ['fooexample.com', nil, 80, '.example.com', true], + ['foo.example.com', nil, 80, 'example.com:80', false], + ['foo.eXample.com', nil, 80, 'example.com:80', false], + ['foo.example.com', nil, 80, 'eXample.com:80', false], + ['foo.example.com', nil, 80, 'example.com:443', true], + ['127.0.0.1', '127.0.0.1', 80, '10.224.0.0/22', true], + ['10.224.1.1', '10.224.1.1', 80, '10.224.1.1', false], + ['10.224.1.1', '10.224.1.1', 80, '10.224.0.0/22', false], + ].each do |hostname, addr, port, no_proxy, expected| + assert_equal expected, URI::Generic.use_proxy?(hostname, addr, port, no_proxy), + "use_proxy?('#{hostname}', '#{addr}', #{port}, '#{no_proxy}')" + end + end + + def test_split + assert_equal [nil, nil, nil, nil, nil, "", nil, nil, nil], URI.split("//") + end + + class CaseInsensitiveEnv + def initialize(h={}) + @h = {} + h.each {|k, v| self[k] = v } + end + + def []=(k, v) + if v + @h[k.downcase] = [k, v.to_s] + else + @h.delete [k.downcase] + end + v + end + + def [](k) + k = k.downcase + @h.has_key?(k) ? @h[k][1] : nil + end + + def length + @h.length + end + + def include?(k) + @h.include? k.downcase + end + + def shift + return nil if @h.empty? + _kd, (k, v) = @h.shift + [k, v] + end + + def each + @h.each {|kd, (k, v)| yield [k, v] } + end + + def reject + ret = CaseInsensitiveEnv.new + self.each {|k, v| + ret[k] = v unless yield [k, v] + } + ret + end + + def to_hash + ret = {} + self.each {|k, v| + ret[k] = v + } + ret + end + end + + def with_proxy_real_env(h) + h = h.dup + ['http', 'https', 'ftp'].each do |scheme| + name = "#{scheme}_proxy" + h[name] ||= nil + h["CGI_#{name.upcase}"] ||= nil + end + begin + old = {} + h.each_key {|k| old[k] = ENV[k] } + h.each {|k, v| ENV[k] = v } + yield ENV + ensure + h.each_key {|k| ENV[k] = old[k] } + end + h.reject! {|k, v| v.nil? } + end + + def with_proxy_env(h, &b) + with_proxy_real_env(h, &b) + h = h.reject {|k, v| v.nil? } + yield h + yield CaseInsensitiveEnv.new(h) + end + + def with_proxy_env_case_sensitive(h, &b) + with_proxy_real_env(h, &b) unless RUBY_PLATFORM =~ /mswin|mingw/ + h = h.reject {|k, v| v.nil? } + yield h + end end diff --git a/test/uri/test_http.rb b/test/uri/test_http.rb index a6846141df..8816d20175 100644 --- a/test/uri/test_http.rb +++ b/test/uri/test_http.rb @@ -1,10 +1,9 @@ +# frozen_string_literal: false require 'test/unit' require 'uri/http' +require 'uri/https' -module URI - - -class TestHTTP < Test::Unit::TestCase +class URI::TestHTTP < Test::Unit::TestCase def setup end @@ -15,49 +14,72 @@ class TestHTTP < Test::Unit::TestCase uri.class.component.collect {|c| uri.send(c)} end + def test_build + u = URI::HTTP.build(host: 'www.example.com', path: '/foo/bar') + assert_kind_of(URI::HTTP, u) + end + + def test_build_empty_host + assert_raise(URI::InvalidComponentError) { URI::HTTP.build(host: '') } + end + def test_parse u = URI.parse('http://a') assert_kind_of(URI::HTTP, u) - assert_equal(['http', - nil, 'a', URI::HTTP.default_port, - '', nil, nil], uri_to_ary(u)) + assert_equal([ + 'http', + nil, 'a', URI::HTTP.default_port, + '', nil, nil + ], uri_to_ary(u)) end def test_normalize host = 'aBcD' - u1 = URI.parse('http://' + host + '/eFg?HiJ') + u1 = URI.parse('http://' + host + '/eFg?HiJ') u2 = URI.parse('http://' + host.downcase + '/eFg?HiJ') - assert(u1.normalize.host == 'abcd') - assert(u1.normalize.path == u1.path) - assert(u1.normalize == u2.normalize) - assert(!u1.normalize.host.equal?(u1.host)) - assert( u2.normalize.host.equal?(u2.host)) + assert_equal('abcd', u1.normalize.host) + assert_equal(u1.path, u1.normalize.path) + assert_equal(u2.normalize, u1.normalize) + refute_same(u1.host, u1.normalize.host) + assert_same(u2.host, u2.normalize.host) assert_equal('http://abc/', URI.parse('http://abc').normalize.to_s) end def test_equal - assert(URI.parse('http://abc') == URI.parse('http://ABC')) - assert(URI.parse('http://abc/def') == URI.parse('http://ABC/def')) - assert(URI.parse('http://abc/def') != URI.parse('http://ABC/DEF')) + assert_equal(URI.parse('http://ABC'), URI.parse('http://abc')) + assert_equal(URI.parse('http://ABC/def'), URI.parse('http://abc/def')) + refute_equal(URI.parse('http://ABC/DEF'), URI.parse('http://abc/def')) end def test_request_uri - assert_equal('/', URI.parse('http://a.b.c/').request_uri) + assert_equal('/', URI.parse('http://a.b.c/').request_uri) assert_equal('/?abc=def', URI.parse('http://a.b.c/?abc=def').request_uri) - assert_equal('/', URI.parse('http://a.b.c').request_uri) + assert_equal('/', URI.parse('http://a.b.c').request_uri) assert_equal('/?abc=def', URI.parse('http://a.b.c?abc=def').request_uri) + assert_equal(nil, URI.parse('http:foo').request_uri) end def test_select assert_equal(['http', 'a.b.c', 80], URI.parse('http://a.b.c/').select(:scheme, :host, :port)) u = URI.parse('http://a.b.c/') assert_equal(uri_to_ary(u), u.select(*u.component)) - assert_raises(ArgumentError) do + assert_raise(ArgumentError) do u.select(:scheme, :host, :not_exist, :port) end end -end + def test_authority + assert_equal('a.b.c', URI.parse('http://a.b.c/').authority) + assert_equal('a.b.c:8081', URI.parse('http://a.b.c:8081/').authority) + assert_equal('a.b.c', URI.parse('http://a.b.c:80/').authority) + end + + def test_origin + assert_equal('http://a.b.c', URI.parse('http://a.b.c/').origin) + assert_equal('http://a.b.c:8081', URI.parse('http://a.b.c:8081/').origin) + assert_equal('http://a.b.c', URI.parse('http://a.b.c:80/').origin) + assert_equal('https://a.b.c', URI.parse('https://a.b.c/').origin) + end end diff --git a/test/uri/test_ldap.rb b/test/uri/test_ldap.rb index 866b7d8066..9c4506a357 100644 --- a/test/uri/test_ldap.rb +++ b/test/uri/test_ldap.rb @@ -1,10 +1,8 @@ +# frozen_string_literal: false require 'test/unit' require 'uri/ldap' -module URI - - -class TestLDAP < Test::Unit::TestCase +class URI::TestLDAP < Test::Unit::TestCase def setup end @@ -36,53 +34,53 @@ class TestLDAP < Test::Unit::TestCase assert_equal(nil, u.extensions) # from RFC2255, section 6. - urls = { + { 'ldap:///o=University%20of%20Michigan,c=US' => - ['ldap', nil, URI::LDAP::DEFAULT_PORT, - 'o=University%20of%20Michigan,c=US', + ['ldap', '', URI::LDAP::DEFAULT_PORT, + 'o=University%20of%20Michigan,c=US', nil, nil, nil, nil], 'ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US' => - ['ldap', 'ldap.itd.umich.edu', URI::LDAP::DEFAULT_PORT, - 'o=University%20of%20Michigan,c=US', + ['ldap', 'ldap.itd.umich.edu', URI::LDAP::DEFAULT_PORT, + 'o=University%20of%20Michigan,c=US', nil, nil, nil, nil], 'ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US?postalAddress' => - ['ldap', 'ldap.itd.umich.edu', URI::LDAP::DEFAULT_PORT, + ['ldap', 'ldap.itd.umich.edu', URI::LDAP::DEFAULT_PORT, 'o=University%20of%20Michigan,c=US', 'postalAddress', nil, nil, nil], 'ldap://host.com:6666/o=University%20of%20Michigan,c=US??sub?(cn=Babs%20Jensen)' => - ['ldap', 'host.com', 6666, + ['ldap', 'host.com', 6666, 'o=University%20of%20Michigan,c=US', nil, 'sub', '(cn=Babs%20Jensen)', nil], 'ldap://ldap.itd.umich.edu/c=GB?objectClass?one' => - ['ldap', 'ldap.itd.umich.edu', URI::LDAP::DEFAULT_PORT, - 'c=GB', + ['ldap', 'ldap.itd.umich.edu', URI::LDAP::DEFAULT_PORT, + 'c=GB', 'objectClass', 'one', nil, nil], 'ldap://ldap.question.com/o=Question%3f,c=US?mail' => - ['ldap', 'ldap.question.com', URI::LDAP::DEFAULT_PORT, + ['ldap', 'ldap.question.com', URI::LDAP::DEFAULT_PORT, 'o=Question%3f,c=US', 'mail', nil, nil, nil], 'ldap://ldap.netscape.com/o=Babsco,c=US??(int=%5c00%5c00%5c00%5c04)' => - ['ldap', 'ldap.netscape.com', URI::LDAP::DEFAULT_PORT, + ['ldap', 'ldap.netscape.com', URI::LDAP::DEFAULT_PORT, 'o=Babsco,c=US', nil, '(int=%5c00%5c00%5c00%5c04)', nil, nil], 'ldap:///??sub??bindname=cn=Manager%2co=Foo' => - ['ldap', nil, URI::LDAP::DEFAULT_PORT, + ['ldap', '', URI::LDAP::DEFAULT_PORT, '', nil, 'sub', nil, 'bindname=cn=Manager%2co=Foo'], 'ldap:///??sub??!bindname=cn=Manager%2co=Foo' => - ['ldap', nil, URI::LDAP::DEFAULT_PORT, + ['ldap', '', URI::LDAP::DEFAULT_PORT, '', nil, 'sub', nil, '!bindname=cn=Manager%2co=Foo'], - }.each do |url, ary| - u = URI.parse(url) + }.each do |url2, ary| + u = URI.parse(url2) assert_equal(ary, uri_to_ary(u)) end end @@ -90,11 +88,12 @@ class TestLDAP < Test::Unit::TestCase def test_select u = URI.parse('ldap:///??sub??!bindname=cn=Manager%2co=Foo') assert_equal(uri_to_ary(u), u.select(*u.component)) - assert_raises(ArgumentError) do + assert_raise(ArgumentError) do u.select(:scheme, :host, :not_exist, :port) end end -end - + def test_parse_invalid_uri + assert_raise(URI::InvalidURIError) {URI.parse("ldap:https://example.com")} + end end diff --git a/test/uri/test_mailto.rb b/test/uri/test_mailto.rb index ba437904f8..6cd3352978 100644 --- a/test/uri/test_mailto.rb +++ b/test/uri/test_mailto.rb @@ -1,10 +1,8 @@ +# frozen_string_literal: false require 'test/unit' require 'uri/mailto' -module URI - - -class TestMailTo < Test::Unit::TestCase +class URI::TestMailTo < Test::Unit::TestCase def setup @u = URI::MailTo end @@ -26,49 +24,53 @@ class TestMailTo < Test::Unit::TestCase ok[-1] << ["chris@example.com", nil] ok[-1] << {:to => "chris@example.com"} + ok << ["mailto:foo+@example.com,bar@example.com"] + ok[-1] << [["foo+@example.com", "bar@example.com"], nil] + ok[-1] << {:to => "foo+@example.com,bar@example.com"} + # mailto:infobot@example.com?subject=current-issue ok << ["mailto:infobot@example.com?subject=current-issue"] ok[-1] << ["infobot@example.com", ["subject=current-issue"]] - ok[-1] << {:to => "infobot@example.com", + ok[-1] << {:to => "infobot@example.com", :headers => ["subject=current-issue"]} # mailto:infobot@example.com?body=send%20current-issue ok << ["mailto:infobot@example.com?body=send%20current-issue"] ok[-1] << ["infobot@example.com", ["body=send%20current-issue"]] - ok[-1] << {:to => "infobot@example.com", + ok[-1] << {:to => "infobot@example.com", :headers => ["body=send%20current-issue"]} # mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index ok << ["mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index"] - ok[-1] << ["infobot@example.com", + ok[-1] << ["infobot@example.com", ["body=send%20current-issue%0D%0Asend%20index"]] - ok[-1] << {:to => "infobot@example.com", + ok[-1] << {:to => "infobot@example.com", :headers => ["body=send%20current-issue%0D%0Asend%20index"]} # mailto:foobar@example.com?In-Reply-To=%3c3469A91.D10AF4C@example.com ok << ["mailto:foobar@example.com?In-Reply-To=%3c3469A91.D10AF4C@example.com"] - ok[-1] << ["foobar@example.com", + ok[-1] << ["foobar@example.com", ["In-Reply-To=%3c3469A91.D10AF4C@example.com"]] - ok[-1] << {:to => "foobar@example.com", + ok[-1] << {:to => "foobar@example.com", :headers => ["In-Reply-To=%3c3469A91.D10AF4C@example.com"]} # mailto:majordomo@example.com?body=subscribe%20bamboo-l ok << ["mailto:majordomo@example.com?body=subscribe%20bamboo-l"] ok[-1] << ["majordomo@example.com", ["body=subscribe%20bamboo-l"]] - ok[-1] << {:to => "majordomo@example.com", + ok[-1] << {:to => "majordomo@example.com", :headers => ["body=subscribe%20bamboo-l"]} # mailto:joe@example.com?cc=bob@example.com&body=hello ok << ["mailto:joe@example.com?cc=bob@example.com&body=hello"] ok[-1] << ["joe@example.com", ["cc=bob@example.com", "body=hello"]] - ok[-1] << {:to => "joe@example.com", + ok[-1] << {:to => "joe@example.com", :headers => ["cc=bob@example.com", "body=hello"]} # mailto:?to=joe@example.com&cc=bob@example.com&body=hello ok << ["mailto:?to=joe@example.com&cc=bob@example.com&body=hello"] - ok[-1] << [nil, + ok[-1] << [nil, ["to=joe@example.com", "cc=bob@example.com", "body=hello"]] - ok[-1] << {:headers => ["to=joe@example.com", + ok[-1] << {:headers => ["to=joe@example.com", "cc=bob@example.com", "body=hello"]} # mailto:gorby%25kremvax@example.com @@ -79,9 +81,24 @@ class TestMailTo < Test::Unit::TestCase # mailto:unlikely%3Faddress@example.com?blat=foop ok << ["mailto:unlikely%3Faddress@example.com?blat=foop"] ok[-1] << ["unlikely%3Faddress@example.com", ["blat=foop"]] - ok[-1] << {:to => "unlikely%3Faddress@example.com", + ok[-1] << {:to => "unlikely%3Faddress@example.com", :headers => ["blat=foop"]} + # mailto:john@example.com?Subject=Ruby&Cc=jack@example.com + ok << ["mailto:john@example.com?Subject=Ruby&Cc=jack@example.com"] + ok[-1] << ['john@example.com', [['Subject', 'Ruby'], ['Cc', 'jack@example.com']]] + ok[-1] << {:to=>"john@example.com", :headers=>[["Subject", "Ruby"], ["Cc", "jack@example.com"]]} + + # mailto:listman@example.com?subject=subscribe + ok << ["mailto:listman@example.com?subject=subscribe"] + ok[-1] << {:to => 'listman@example.com', :headers => [['subject', 'subscribe']]} + ok[-1] << {:to => 'listman@example.com', :headers => [['subject', 'subscribe']]} + + # mailto:listman@example.com?subject=subscribe + ok << ["mailto:listman@example.com?subject=subscribe"] + ok[-1] << {:to => 'listman@example.com', :headers => { 'subject' => 'subscribe' }} + ok[-1] << {:to => 'listman@example.com', :headers => 'subject=subscribe' } + ok_all = ok.flatten.join("\0") # mailto:joe@example.com?cc=bob@example.com?body=hello ; WRONG! @@ -90,18 +107,23 @@ class TestMailTo < Test::Unit::TestCase # mailto:javascript:alert() bad << ["javascript:alert()", []] + # mailto:/example.com/ ; WRONG, not a mail address + bad << ["/example.com/", []] + # '=' which is in hname or hvalue is wrong. bad << ["foo@example.jp?subject=1+1=2", []] ok.each do |x| - assert_equal(x[0], - @u.build(x[1]).to_s) - assert_equal(x[0], - @u.build(x[2]).to_s) + assert_equal(x[0], URI.parse(x[0]).to_s) + assert_equal(x[0], @u.build(x[1]).to_s) + assert_equal(x[0], @u.build(x[2]).to_s) end bad.each do |x| - assert_raises(URI::InvalidComponentError) { + assert_raise(URI::InvalidURIError) { + URI.parse(x) + } + assert_raise(URI::InvalidComponentError) { @u.build(x) } end @@ -109,14 +131,133 @@ class TestMailTo < Test::Unit::TestCase assert_equal(ok_all, ok.flatten.join("\0")) end + def test_initializer + assert_raise(URI::InvalidComponentError) do + URI::MailTo.new('mailto', 'sdmitry:bla', 'localhost', '2000', nil, + 'joe@example.com', nil, nil, 'subject=Ruby') + end + end + + def test_check_to + u = URI::MailTo.build(['joe@example.com', 'subject=Ruby']) + + # Valid emails + u.to = 'a@valid.com' + assert_equal(u.to, 'a@valid.com') + + # Intentionally allowed violations of RFC 5322 + u.to = 'a..a@valid.com' + assert_equal(u.to, 'a..a@valid.com') + + u.to = 'hello.@valid.com' + assert_equal(u.to, 'hello.@valid.com') + + u.to = '.hello@valid.com' + assert_equal(u.to, '.hello@valid.com') + + # Invalid emails + assert_raise(URI::InvalidComponentError) do + u.to = '#1@mail.com' + end + + assert_raise(URI::InvalidComponentError) do + u.to = '@invalid.email' + end + + # Invalid host emails + assert_raise(URI::InvalidComponentError) do + u.to = 'a@.invalid.email' + end + + assert_raise(URI::InvalidComponentError) do + u.to = 'a@invalid.email.' + end + + assert_raise(URI::InvalidComponentError) do + u.to = 'a@invalid..email' + end + + assert_raise(URI::InvalidComponentError) do + u.to = 'a@-invalid.email' + end + + assert_raise(URI::InvalidComponentError) do + u.to = 'a@invalid-.email' + end + + assert_raise(URI::InvalidComponentError) do + u.to = 'a@invalid.-email' + end + + assert_raise(URI::InvalidComponentError) do + u.to = 'a@invalid.email-' + end + + u.to = 'a@'+'invalid'.ljust(63, 'd')+'.email' + assert_raise(URI::InvalidComponentError) do + u.to = 'a@'+'invalid'.ljust(64, 'd')+'.email' + end + + u.to = 'a@invalid.'+'email'.rjust(63, 'e') + assert_raise(URI::InvalidComponentError) do + u.to = 'a@invalid.'+'email'.rjust(64, 'e') + end + end + + def test_email_regexp + re = URI::MailTo::EMAIL_REGEXP + + repeat = 10 + longlabel = '.' + 'invalid'.ljust(63, 'd') + endlabel = '' + seq = (1..3).map {|i| 10**i} + rehearsal = 10 + pre = ->(n) {'a@invalid' + longlabel*(n) + endlabel} + assert_linear_performance(seq, rehearsal: rehearsal, pre: pre) do |to| + repeat.times {re =~ to or flunk} + end + endlabel = '.' + 'email'.rjust(64, 'd') + assert_linear_performance(seq, rehearsal: rehearsal, pre: pre) do |to| + repeat.times {re =~ to and flunk} + end + end + + def test_to_s + u = URI::MailTo.build([nil, 'subject=Ruby']) + + u.send(:set_to, nil) + assert_equal('mailto:?subject=Ruby', u.to_s) + + u.fragment = 'test' + assert_equal('mailto:?subject=Ruby#test', u.to_s) + end + + def test_to_mailtext + results = [] + results << ["To: ruby-list@ruby-lang.org\nSubject: subscribe\n\n\n"] + results[-1] << { to: 'ruby-list@ruby-lang.org', headers: { 'subject' => 'subscribe' } } + + results << ["To: ruby-list@ruby-lang.org\n\nBody\n"] + results[-1] << { to: 'ruby-list@ruby-lang.org', headers: { 'body' => 'Body' } } + + results << ["To: ruby-list@ruby-lang.org, cc@ruby-lang.org\n\n\n"] + results[-1] << { to: 'ruby-list@ruby-lang.org', headers: { 'to' => 'cc@ruby-lang.org' } } + + results.each do |expected, params| + u = URI::MailTo.build(params) + assert_equal(expected, u.to_mailtext) + end + + u = URI.parse('mailto:ruby-list@ruby-lang.org?Subject=subscribe&cc=myaddr') + assert_equal "To: ruby-list@ruby-lang.org\nSubject: subscribe\nCc: myaddr\n\n\n", + u.to_mailtext + end + def test_select u = URI.parse('mailto:joe@example.com?cc=bob@example.com&body=hello') assert_equal(uri_to_ary(u), u.select(*u.component)) - assert_raises(ArgumentError) do + assert_raise(ArgumentError) do u.select(:scheme, :host, :not_exist, :port) end end end - - -end diff --git a/test/uri/test_parser.rb b/test/uri/test_parser.rb new file mode 100644 index 0000000000..c14824f5e8 --- /dev/null +++ b/test/uri/test_parser.rb @@ -0,0 +1,124 @@ +# frozen_string_literal: false +require 'test/unit' +require 'uri' + +class URI::TestParser < Test::Unit::TestCase + def uri_to_ary(uri) + uri.class.component.collect {|c| uri.send(c)} + end + + def test_inspect + assert_match(/URI::RFC2396_Parser/, URI::RFC2396_Parser.new.inspect) + assert_match(/URI::RFC3986_Parser/, URI::Parser.new.inspect) + end + + def test_compare + url = 'http://a/b/c/d;p?q' + u0 = URI.parse(url) + u1 = URI.parse(url) + p = URI::Parser.new + u2 = p.parse(url) + u3 = p.parse(url) + + assert_equal(u1, u0) + assert_send([u0, :eql?, u1]) + refute_same(u1, u0) + + assert_equal(u2, u1) + assert_not_send([u1, :eql?, u2]) + refute_same(u1, u2) + + assert_equal(u3, u2) + assert_send([u2, :eql?, u3]) + refute_same(u3, u2) + end + + def test_parse_rfc2396_parser + URI.parser = URI::RFC2396_PARSER + + escaped = URI::REGEXP::PATTERN::ESCAPED + hex = URI::REGEXP::PATTERN::HEX + p1 = URI::Parser.new(:ESCAPED => "(?:#{escaped}|%u[#{hex}]{4})") + u1 = p1.parse('http://a/b/%uABCD') + assert_equal(['http', nil, 'a', URI::HTTP.default_port, '/b/%uABCD', nil, nil], + uri_to_ary(u1)) + u1.path = '/%uDCBA' + assert_equal(['http', nil, 'a', URI::HTTP.default_port, '/%uDCBA', nil, nil], + uri_to_ary(u1)) + ensure + URI.parser = URI::DEFAULT_PARSER + end + + def test_parse_query_pct_encoded + assert_equal('q=%32!$&-/?.09;=:@AZ_az~', URI.parse('https://www.example.com/search?q=%32!$&-/?.09;=:@AZ_az~').query) + assert_raise(URI::InvalidURIError) { URI.parse('https://www.example.com/search?q=%XX') } + end + + def test_parse_auth + str = "http://al%40ice:p%40s%25sword@example.com/dir%2Fname/subdir?foo=bar%40example.com" + uri = URI.parse(str) + assert_equal "al%40ice", uri.user + assert_equal "p%40s%25sword", uri.password + assert_equal "al@ice", uri.decoded_user + assert_equal "p@s%sword", uri.decoded_password + end + + def test_raise_bad_uri_for_integer + assert_raise(URI::InvalidURIError) do + URI.parse(1) + end + end + + def test_rfc2822_unescape + p1 = URI::RFC2396_Parser.new + assert_equal("\xe3\x83\x90", p1.unescape("\xe3\x83\x90")) + assert_equal("\xe3\x83\x90", p1.unescape('%e3%83%90')) + assert_equal("\u3042", p1.unescape('%e3%81%82'.force_encoding(Encoding::US_ASCII))) + assert_equal("\xe3\x83\x90\xe3\x83\x90", p1.unescape("\xe3\x83\x90%e3%83%90")) + end + + def test_split + assert_equal(["http", nil, "example.com", nil, nil, "", nil, nil, nil], URI.split("http://example.com")) + assert_equal(["http", nil, "[0::0]", nil, nil, "", nil, nil, nil], URI.split("http://[0::0]")) + assert_equal([nil, nil, "example.com", nil, nil, "", nil, nil, nil], URI.split("//example.com")) + assert_equal([nil, nil, "[0::0]", nil, nil, "", nil, nil, nil], URI.split("//[0::0]")) + + assert_equal(["a", nil, nil, nil, nil, "", nil, nil, nil], URI.split("a:")) + assert_raise(URI::InvalidURIError) do + URI.parse("::") + end + assert_raise(URI::InvalidURIError) do + URI.parse("foo@example:foo") + end + end + + def test_rfc2822_parse_relative_uri + pre = ->(length) { + " " * length + "\0" + } + parser = URI::RFC2396_Parser.new + assert_linear_performance((1..5).map {|i| 10**i}, pre: pre) do |uri| + assert_raise(URI::InvalidURIError) do + parser.split(uri) + end + end + end + + def test_rfc3986_port_check + pre = ->(length) {"\t" * length + "a"} + uri = URI.parse("http://my.example.com") + assert_linear_performance((1..5).map {|i| 10**i}, pre: pre) do |port| + assert_raise(URI::InvalidComponentError) do + uri.port = port + end + end + end + + def test_rfc2822_make_regexp + parser = URI::RFC2396_Parser.new + regexp = parser.make_regexp("HTTP") + assert_match(regexp, "HTTP://EXAMPLE.COM/") + assert_match(regexp, "http://example.com/") + refute_match(regexp, "https://example.com/") + end +end diff --git a/test/uri/test_ws.rb b/test/uri/test_ws.rb new file mode 100644 index 0000000000..d63ebd4a46 --- /dev/null +++ b/test/uri/test_ws.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: false +require 'test/unit' +require 'uri/http' +require 'uri/ws' + +class URI::TestWS < Test::Unit::TestCase + def setup + end + + def teardown + end + + def uri_to_ary(uri) + uri.class.component.collect {|c| uri.send(c)} + end + + def test_build + u = URI::WS.build(host: 'www.example.com', path: '/foo/bar') + assert_kind_of(URI::WS, u) + end + + def test_parse + u = URI.parse('ws://a') + assert_kind_of(URI::WS, u) + assert_equal(['ws', + nil, 'a', URI::HTTP.default_port, + '', nil], uri_to_ary(u)) + end + + def test_normalize + host = 'aBcD' + u1 = URI.parse('ws://' + host + '/eFg?HiJ') + u2 = URI.parse('ws://' + host.downcase + '/eFg?HiJ') + assert_equal('abcd', u1.normalize.host) + assert_equal(u1.path, u1.normalize.path) + assert_equal(u2.normalize, u1.normalize) + refute_same(u1.host, u1.normalize.host) + assert_same(u2.host, u2.normalize.host) + + assert_equal('ws://abc/', URI.parse('ws://abc').normalize.to_s) + end + + def test_equal + assert_equal(URI.parse('ws://ABC'), URI.parse('ws://abc')) + assert_equal(URI.parse('ws://ABC/def'), URI.parse('ws://abc/def')) + refute_equal(URI.parse('ws://ABC/DEF'), URI.parse('ws://abc/def')) + end + + def test_request_uri + assert_equal('/', URI.parse('ws://a.b.c/').request_uri) + assert_equal('/?abc=def', URI.parse('ws://a.b.c/?abc=def').request_uri) + assert_equal('/', URI.parse('ws://a.b.c').request_uri) + assert_equal('/?abc=def', URI.parse('ws://a.b.c?abc=def').request_uri) + assert_equal(nil, URI.parse('ws:foo').request_uri) + end + + def test_select + assert_equal(['ws', 'a.b.c', 80], URI.parse('ws://a.b.c/').select(:scheme, :host, :port)) + u = URI.parse('ws://a.b.c/') + assert_equal(uri_to_ary(u), u.select(*u.component)) + assert_raise(ArgumentError) do + u.select(:scheme, :host, :not_exist, :port) + end + end +end diff --git a/test/uri/test_wss.rb b/test/uri/test_wss.rb new file mode 100644 index 0000000000..cbef327cc6 --- /dev/null +++ b/test/uri/test_wss.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: false +require 'test/unit' +require 'uri/https' +require 'uri/wss' + +class URI::TestWSS < Test::Unit::TestCase + def setup + end + + def teardown + end + + def uri_to_ary(uri) + uri.class.component.collect {|c| uri.send(c)} + end + + def test_build + u = URI::WSS.build(host: 'www.example.com', path: '/foo/bar') + assert_kind_of(URI::WSS, u) + end + + def test_parse + u = URI.parse('wss://a') + assert_kind_of(URI::WSS, u) + assert_equal(['wss', + nil, 'a', URI::HTTPS.default_port, + '', nil], uri_to_ary(u)) + end + + def test_normalize + host = 'aBcD' + u1 = URI.parse('wss://' + host + '/eFg?HiJ') + u2 = URI.parse('wss://' + host.downcase + '/eFg?HiJ') + assert_equal('abcd', u1.normalize.host) + assert_equal(u1.path, u1.normalize.path) + assert_equal(u2.normalize, u1.normalize) + refute_same(u1.host, u1.normalize.host) + assert_same(u2.host, u2.normalize.host) + + assert_equal('wss://abc/', URI.parse('wss://abc').normalize.to_s) + end + + def test_equal + assert_equal(URI.parse('wss://ABC'), URI.parse('wss://abc')) + assert_equal(URI.parse('wss://ABC/def'), URI.parse('wss://abc/def')) + refute_equal(URI.parse('wss://ABC/DEF'), URI.parse('wss://abc/def')) + end + + def test_request_uri + assert_equal('/', URI.parse('wss://a.b.c/').request_uri) + assert_equal('/?abc=def', URI.parse('wss://a.b.c/?abc=def').request_uri) + assert_equal('/', URI.parse('wss://a.b.c').request_uri) + assert_equal('/?abc=def', URI.parse('wss://a.b.c?abc=def').request_uri) + assert_equal(nil, URI.parse('wss:foo').request_uri) + end + + def test_select + assert_equal(['wss', 'a.b.c', 443], URI.parse('wss://a.b.c/').select(:scheme, :host, :port)) + u = URI.parse('wss://a.b.c/') + assert_equal(uri_to_ary(u), u.select(*u.component)) + assert_raise(ArgumentError) do + u.select(:scheme, :host, :not_exist, :port) + end + end +end |
