diff options
Diffstat (limited to 'jni/ruby/test/uri')
-rw-r--r-- | jni/ruby/test/uri/test_common.rb | 174 | ||||
-rw-r--r-- | jni/ruby/test/uri/test_ftp.rb | 66 | ||||
-rw-r--r-- | jni/ruby/test/uri/test_generic.rb | 847 | ||||
-rw-r--r-- | jni/ruby/test/uri/test_http.rb | 64 | ||||
-rw-r--r-- | jni/ruby/test/uri/test_ldap.rb | 100 | ||||
-rw-r--r-- | jni/ruby/test/uri/test_mailto.rb | 136 | ||||
-rw-r--r-- | jni/ruby/test/uri/test_parser.rb | 47 |
7 files changed, 1434 insertions, 0 deletions
diff --git a/jni/ruby/test/uri/test_common.rb b/jni/ruby/test/uri/test_common.rb new file mode 100644 index 0000000..5620415 --- /dev/null +++ b/jni/ruby/test/uri/test_common.rb @@ -0,0 +1,174 @@ +require 'test/unit' +require 'uri' + +module URI + + +class TestCommon < Test::Unit::TestCase + def setup + end + + def teardown + end + + def test_extract + 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_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 + expected = URI.parse("http://www.ruby-lang.org/") + assert_equal(expected, URI("http://www.ruby-lang.org/")) + assert_equal(expected, Kernel::URI("http://www.ruby-lang.org/")) + assert_raise(NoMethodError) { Object.new.URI("http://www.ruby-lang.org/") } + 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)) + + # 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_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 + + +end diff --git a/jni/ruby/test/uri/test_ftp.rb b/jni/ruby/test/uri/test_ftp.rb new file mode 100644 index 0000000..cc6843e --- /dev/null +++ b/jni/ruby/test/uri/test_ftp.rb @@ -0,0 +1,66 @@ +require 'test/unit' +require 'uri/ftp' + +module URI + + +class TestFTP < Test::Unit::TestCase + def setup + end + + def test_parse + url = URI.parse('ftp://user:pass@host.com/abc/def') + assert_kind_of(URI::FTP, url) + + exp = [ + 'ftp', + 'user:pass', 'host.com', URI::FTP.default_port, + 'abc/def', nil, + ] + ary = [ + url.scheme, url.userinfo, url.host, url.port, + url.path, url.opaque + ] + assert_equal(exp, ary) + + assert_equal('user', url.user) + assert_equal('pass', url.password) + end + + def test_parse_invalid + assert_raise(InvalidURIError){URI.parse('ftp:example')} + end + + def test_paths + # 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(u.path == 'foo/bar/file.ext') + u = URI.parse('ftp://ftp.example.com//foo/bar/file.ext') + assert(u.path == '/foo/bar/file.ext') + u = URI.parse('ftp://ftp.example.com/%2Ffoo/bar/file.ext') + assert(u.path == '/foo/bar/file.ext') + 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, + '/path/file.zip', 'i']) + assert(uri.to_s == + 'ftp://user:password@ftp.example.com/%2Fpath/file.zip;type=i') + end + + def test_select + assert_equal(['ftp', 'a.b.c', 21], URI.parse('ftp://a.b.c/').select(:scheme, :host, :port)) + u = URI.parse('ftp://a.b.c/') + ary = u.component.collect {|c| u.send(c)} + assert_equal(ary, u.select(*u.component)) + assert_raise(ArgumentError) do + u.select(:scheme, :host, :not_exist, :port) + end + end +end + + +end diff --git a/jni/ruby/test/uri/test_generic.rb b/jni/ruby/test/uri/test_generic.rb new file mode 100644 index 0000000..37605d5 --- /dev/null +++ b/jni/ruby/test/uri/test_generic.rb @@ -0,0 +1,847 @@ +require 'test/unit' +require 'uri' + +class URI::TestGeneric < Test::Unit::TestCase + def setup + @url = 'http://a/b/c/d;p?q' + @base_url = URI.parse(@url) + end + + def teardown + end + + def uri_to_ary(uri) + uri.class.component.collect {|c| uri.send(c)} + end + + def test_parse + # 0 + assert_kind_of(URI::HTTP, @base_url) + + exp = [ + 'http', + nil, 'a', URI::HTTP.default_port, + '/b/c/d;p', + 'q', + nil + ] + ary = uri_to_ary(@base_url) + assert_equal(exp, ary) + + # 1 + url = URI.parse('ftp://ftp.is.co.za/rfc/rfc1808.txt') + assert_kind_of(URI::FTP, url) + + exp = [ + 'ftp', + nil, 'ftp.is.co.za', URI::FTP.default_port, + 'rfc/rfc1808.txt', nil, + ] + ary = uri_to_ary(url) + assert_equal(exp, ary) + # 1' + url = URI.parse('ftp://ftp.is.co.za/%2Frfc/rfc1808.txt') + assert_kind_of(URI::FTP, url) + + exp = [ + 'ftp', + nil, 'ftp.is.co.za', URI::FTP.default_port, + '/rfc/rfc1808.txt', nil, + ] + ary = uri_to_ary(url) + assert_equal(exp, ary) + + # 2 + url = URI.parse('gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles') + assert_kind_of(URI::Generic, url) + + exp = [ + 'gopher', + nil, 'spinaltap.micro.umn.edu', nil, nil, + '/00/Weather/California/Los%20Angeles', nil, + nil, + nil + ] + ary = uri_to_ary(url) + assert_equal(exp, ary) + + # 3 + url = URI.parse('http://www.math.uio.no/faq/compression-faq/part1.html') + assert_kind_of(URI::HTTP, url) + + exp = [ + 'http', + nil, 'www.math.uio.no', URI::HTTP.default_port, + '/faq/compression-faq/part1.html', + nil, + nil + ] + ary = uri_to_ary(url) + assert_equal(exp, ary) + + # 4 + url = URI.parse('mailto:mduerst@ifi.unizh.ch') + assert_kind_of(URI::Generic, url) + + exp = [ + 'mailto', + 'mduerst@ifi.unizh.ch', + [] + ] + ary = uri_to_ary(url) + assert_equal(exp, ary) + + # 5 + url = URI.parse('news:comp.infosystems.www.servers.unix') + assert_kind_of(URI::Generic, url) + + exp = [ + 'news', + nil, nil, nil, nil, + nil, 'comp.infosystems.www.servers.unix', + nil, + nil + ] + ary = uri_to_ary(url) + assert_equal(exp, ary) + + # 6 + url = URI.parse('telnet://melvyl.ucop.edu/') + assert_kind_of(URI::Generic, url) + + exp = [ + 'telnet', + nil, 'melvyl.ucop.edu', nil, nil, + '/', nil, + nil, + nil + ] + ary = uri_to_ary(url) + assert_equal(exp, ary) + + # 7 + # reported by Mr. Kubota <em6t-kbt@asahi-net.or.jp> + 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 + url = URI.parse('file:///foo/bar.txt') + assert_kind_of(URI::Generic, url) + url = URI.parse('file:/foo/bar.txt') + assert_kind_of(URI::Generic, url) + + # 9 + url = URI.parse('ftp://:pass@localhost/') + assert_equal('', url.user, "[ruby-dev:25667]") + assert_equal('pass', url.password) + assert_equal(':pass', url.userinfo, "[ruby-dev:25667]") + url = URI.parse('ftp://user@localhost/') + assert_equal('user', url.user) + assert_equal(nil, url.password) + assert_equal('user', url.userinfo) + url = URI.parse('ftp://localhost/') + assert_equal(nil, url.user) + assert_equal(nil, url.password) + assert_equal(nil, url.userinfo) + end + + def test_merge + u1 = URI.parse('http://foo') + u2 = URI.parse('http://foo/') + u3 = URI.parse('http://foo/bar') + u4 = URI.parse('http://foo/bar/') + + { + 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]") + + # reported by Mr. Kubota <em6t-kbt@asahi-net.or.jp> + url = URI.parse('http://a/b') + 'http://x/y' + assert_equal('http://x/y', url.to_s) + assert_equal(url, URI.parse('') + 'http://x/y') + assert_equal(url, URI.parse('').normalize + 'http://x/y') + assert_equal(url, URI.parse('http://a/b').normalize + 'http://x/y') + + u = URI.parse('http://foo/bar/baz') + assert_equal(nil, u.merge!("")) + assert_equal(nil, u.merge!(u)) + assert(nil != u.merge!(".")) + assert_equal('http://foo/bar/', u.to_s) + assert(nil != u.merge!("../baz")) + assert_equal('http://foo/baz', u.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]") + + u0 = URI.parse('http://www.example.com/') + u1 = URI.parse('http://www.example.com/foo/..') + './' + assert_equal(u0, u1, "[ruby-list:39838]") + u0 = URI.parse('http://www.example.com/foo/') + u1 = URI.parse('http://www.example.com/foo/bar/..') + './' + assert_equal(u0, u1) + u0 = URI.parse('http://www.example.com/foo/bar/') + u1 = URI.parse('http://www.example.com/foo/bar/baz/..') + './' + assert_equal(u0, u1) + u0 = URI.parse('http://www.example.com/') + u1 = URI.parse('http://www.example.com/foo/bar/../..') + './' + assert_equal(u0, u1) + u0 = URI.parse('http://www.example.com/foo/') + u1 = URI.parse('http://www.example.com/foo/bar/baz/../..') + './' + assert_equal(u0, u1) + + u = URI.parse('http://www.example.com/') + u0 = u + './foo/' + u1 = u + './foo/bar/..' + assert_equal(u0, u1, "[ruby-list:39844]") + u = URI.parse('http://www.example.com/') + u0 = u + './' + u1 = u + './foo/bar/../..' + 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) + + url = URI.parse('http://hoge/a/').route_to('http://hoge/b/') + assert_equal('../b/', url.to_s) + url = URI.parse('http://hoge/a/b').route_to('http://hoge/b/') + assert_equal('../b/', url.to_s) + + url = URI.parse('http://hoge/a/b/').route_to('http://hoge/b/') + assert_equal('../../b/', url.to_s) + + url = URI.parse('http://hoge/a/b/').route_to('http://HOGE/b/') + assert_equal('../../b/', url.to_s) + + 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('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) + + url = URI.parse('mailto:foo@example.com#bar').route_to('mailto:foo@example.com') + assert_equal('', url.to_s) + + url = URI.parse('mailto:foo@example.com').route_to('mailto:foo@example.com') + assert_equal('', url.to_s) + end + + def test_rfc3986_examples +# http://a/b/c/d;p?q +# g:h = g:h + url = @base_url.merge('g:h') + assert_kind_of(URI::Generic, url) + assert_equal('g:h', url.to_s) + url = @base_url.route_to('g:h') + assert_kind_of(URI::Generic, url) + assert_equal('g:h', url.to_s) + +# http://a/b/c/d;p?q +# g = http://a/b/c/g + url = @base_url.merge('g') + assert_kind_of(URI::HTTP, url) + 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_equal('g', url.to_s) + +# http://a/b/c/d;p?q +# ./g = http://a/b/c/g + url = @base_url.merge('./g') + assert_kind_of(URI::HTTP, url) + 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 + assert_equal('g', url.to_s) + +# http://a/b/c/d;p?q +# g/ = http://a/b/c/g/ + url = @base_url.merge('g/') + assert_kind_of(URI::HTTP, url) + 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_equal('g/', url.to_s) + +# http://a/b/c/d;p?q +# /g = http://a/g + url = @base_url.merge('/g') + assert_kind_of(URI::HTTP, url) + 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 + assert_equal('../../g', url.to_s) + +# http://a/b/c/d;p?q +# //g = http://g + url = @base_url.merge('//g') + assert_kind_of(URI::HTTP, url) + assert_equal('http://g', url.to_s) + url = @base_url.route_to('http://g') + assert_kind_of(URI::Generic, url) + assert_equal('//g', url.to_s) + +# http://a/b/c/d;p?q +# ?y = http://a/b/c/d;p?y + url = @base_url.merge('?y') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/b/c/d;p?y', url.to_s) + url = @base_url.route_to('http://a/b/c/d;p?y') + assert_kind_of(URI::Generic, url) + assert_equal('?y', url.to_s) + +# http://a/b/c/d;p?q +# g?y = http://a/b/c/g?y + url = @base_url.merge('g?y') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/b/c/g?y', url.to_s) + url = @base_url.route_to('http://a/b/c/g?y') + assert_kind_of(URI::Generic, url) + assert_equal('g?y', url.to_s) + +# http://a/b/c/d;p?q +# #s = http://a/b/c/d;p?q#s + url = @base_url.merge('#s') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/b/c/d;p?q#s', url.to_s) + url = @base_url.route_to('http://a/b/c/d;p?q#s') + assert_kind_of(URI::Generic, url) + assert_equal('#s', url.to_s) + +# http://a/b/c/d;p?q +# g#s = http://a/b/c/g#s + url = @base_url.merge('g#s') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/b/c/g#s', url.to_s) + url = @base_url.route_to('http://a/b/c/g#s') + assert_kind_of(URI::Generic, url) + assert_equal('g#s', url.to_s) + +# http://a/b/c/d;p?q +# g?y#s = http://a/b/c/g?y#s + url = @base_url.merge('g?y#s') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/b/c/g?y#s', url.to_s) + url = @base_url.route_to('http://a/b/c/g?y#s') + assert_kind_of(URI::Generic, url) + assert_equal('g?y#s', url.to_s) + +# http://a/b/c/d;p?q +# ;x = http://a/b/c/;x + url = @base_url.merge(';x') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/b/c/;x', url.to_s) + url = @base_url.route_to('http://a/b/c/;x') + assert_kind_of(URI::Generic, url) + assert_equal(';x', url.to_s) + +# http://a/b/c/d;p?q +# g;x = http://a/b/c/g;x + url = @base_url.merge('g;x') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/b/c/g;x', url.to_s) + url = @base_url.route_to('http://a/b/c/g;x') + assert_kind_of(URI::Generic, url) + assert_equal('g;x', url.to_s) + +# http://a/b/c/d;p?q +# g;x?y#s = http://a/b/c/g;x?y#s + url = @base_url.merge('g;x?y#s') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/b/c/g;x?y#s', url.to_s) + url = @base_url.route_to('http://a/b/c/g;x?y#s') + assert_kind_of(URI::Generic, url) + assert_equal('g;x?y#s', url.to_s) + +# http://a/b/c/d;p?q +# . = http://a/b/c/ + url = @base_url.merge('.') + assert_kind_of(URI::HTTP, url) + 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 + assert_equal('./', url.to_s) + +# http://a/b/c/d;p?q +# ./ = http://a/b/c/ + url = @base_url.merge('./') + assert_kind_of(URI::HTTP, url) + 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_equal('./', url.to_s) + +# http://a/b/c/d;p?q +# .. = http://a/b/ + url = @base_url.merge('..') + assert_kind_of(URI::HTTP, url) + 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 + assert_equal('../', url.to_s) + +# http://a/b/c/d;p?q +# ../ = http://a/b/ + url = @base_url.merge('../') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/b/', url.to_s) + url = @base_url.route_to('http://a/b/') + assert_kind_of(URI::Generic, url) + assert_equal('../', url.to_s) + +# http://a/b/c/d;p?q +# ../g = http://a/b/g + url = @base_url.merge('../g') + assert_kind_of(URI::HTTP, url) + 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_equal('../g', url.to_s) + +# http://a/b/c/d;p?q +# ../.. = http://a/ + url = @base_url.merge('../..') + assert_kind_of(URI::HTTP, url) + 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 + assert_equal('../../', url.to_s) + +# http://a/b/c/d;p?q +# ../../ = http://a/ + url = @base_url.merge('../../') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/', url.to_s) + url = @base_url.route_to('http://a/') + assert_kind_of(URI::Generic, url) + assert_equal('../../', url.to_s) + +# http://a/b/c/d;p?q +# ../../g = http://a/g + url = @base_url.merge('../../g') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/g', url.to_s) + url = @base_url.route_to('http://a/g') + assert_kind_of(URI::Generic, url) + assert_equal('../../g', url.to_s) + +# http://a/b/c/d;p?q +# <> = (current document) + url = @base_url.merge('') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/b/c/d;p?q', url.to_s) + url = @base_url.route_to('http://a/b/c/d;p?q') + assert_kind_of(URI::Generic, url) + assert_equal('', url.to_s) + +# http://a/b/c/d;p?q +# /./g = http://a/g + url = @base_url.merge('/./g') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/g', url.to_s) +# url = @base_url.route_to('http://a/./g') +# assert_kind_of(URI::Generic, url) +# assert_equal('/./g', url.to_s) + +# http://a/b/c/d;p?q +# /../g = http://a/g + url = @base_url.merge('/../g') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/g', url.to_s) +# url = @base_url.route_to('http://a/../g') +# assert_kind_of(URI::Generic, url) +# assert_equal('/../g', url.to_s) + +# http://a/b/c/d;p?q +# g. = http://a/b/c/g. + url = @base_url.merge('g.') + assert_kind_of(URI::HTTP, url) + 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_equal('g.', url.to_s) + +# http://a/b/c/d;p?q +# .g = http://a/b/c/.g + url = @base_url.merge('.g') + assert_kind_of(URI::HTTP, url) + 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_equal('.g', url.to_s) + +# http://a/b/c/d;p?q +# g.. = http://a/b/c/g.. + url = @base_url.merge('g..') + assert_kind_of(URI::HTTP, url) + 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_equal('g..', url.to_s) + +# http://a/b/c/d;p?q +# ..g = http://a/b/c/..g + url = @base_url.merge('..g') + assert_kind_of(URI::HTTP, url) + 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_equal('..g', url.to_s) + +# http://a/b/c/d;p?q +# ../../../g = http://a/g + url = @base_url.merge('../../../g') + assert_kind_of(URI::HTTP, url) + 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 + assert_equal('../../g', url.to_s) # and it is clearly + +# http://a/b/c/d;p?q +# ../../../../g = http://a/g + url = @base_url.merge('../../../../g') + assert_kind_of(URI::HTTP, url) + 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 + assert_equal('../../g', url.to_s) # and it is clearly + +# http://a/b/c/d;p?q +# ./../g = http://a/b/g + url = @base_url.merge('./../g') + assert_kind_of(URI::HTTP, url) + 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 + assert_equal('../g', url.to_s) + +# http://a/b/c/d;p?q +# ./g/. = http://a/b/c/g/ + url = @base_url.merge('./g/.') + assert_kind_of(URI::HTTP, url) + 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 + assert_equal('g/', url.to_s) + +# http://a/b/c/d;p?q +# g/./h = http://a/b/c/g/h + url = @base_url.merge('g/./h') + assert_kind_of(URI::HTTP, url) + 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 + assert_equal('g/h', url.to_s) + +# http://a/b/c/d;p?q +# g/../h = http://a/b/c/h + url = @base_url.merge('g/../h') + assert_kind_of(URI::HTTP, url) + 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 + assert_equal('h', url.to_s) + +# http://a/b/c/d;p?q +# g;x=1/./y = http://a/b/c/g;x=1/y + url = @base_url.merge('g;x=1/./y') + assert_kind_of(URI::HTTP, url) + 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 + assert_equal('g;x=1/y', url.to_s) + +# http://a/b/c/d;p?q +# g;x=1/../y = http://a/b/c/y + url = @base_url.merge('g;x=1/../y') + assert_kind_of(URI::HTTP, url) + 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 + assert_equal('y', url.to_s) + +# http://a/b/c/d;p?q +# g?y/./x = http://a/b/c/g?y/./x + url = @base_url.merge('g?y/./x') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/b/c/g?y/./x', url.to_s) + url = @base_url.route_to('http://a/b/c/g?y/./x') + assert_kind_of(URI::Generic, url) + assert_equal('g?y/./x', url.to_s) + +# http://a/b/c/d;p?q +# g?y/../x = http://a/b/c/g?y/../x + url = @base_url.merge('g?y/../x') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/b/c/g?y/../x', url.to_s) + url = @base_url.route_to('http://a/b/c/g?y/../x') + assert_kind_of(URI::Generic, url) + assert_equal('g?y/../x', url.to_s) + +# http://a/b/c/d;p?q +# g#s/./x = http://a/b/c/g#s/./x + url = @base_url.merge('g#s/./x') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/b/c/g#s/./x', url.to_s) + url = @base_url.route_to('http://a/b/c/g#s/./x') + assert_kind_of(URI::Generic, url) + assert_equal('g#s/./x', url.to_s) + +# http://a/b/c/d;p?q +# g#s/../x = http://a/b/c/g#s/../x + url = @base_url.merge('g#s/../x') + assert_kind_of(URI::HTTP, url) + assert_equal('http://a/b/c/g#s/../x', url.to_s) + url = @base_url.route_to('http://a/b/c/g#s/../x') + assert_kind_of(URI::Generic, url) + assert_equal('g#s/../x', url.to_s) + +# http://a/b/c/d;p?q +# http:g = http:g ; for validating parsers +# | http://a/b/c/g ; for backwards compatibility + url = @base_url.merge('http:g') + assert_kind_of(URI::HTTP, url) + assert_equal('http:g', url.to_s) + url = @base_url.route_to('http:g') + assert_kind_of(URI::Generic, url) + assert_equal('http:g', url.to_s) + end + + def test_join + assert_equal(URI.parse('http://foo/bar'), URI.join('http://foo/bar')) + assert_equal(URI.parse('http://foo/bar'), URI.join('http://foo', 'bar')) + assert_equal(URI.parse('http://foo/bar/'), URI.join('http://foo', 'bar/')) + + assert_equal(URI.parse('http://foo/baz'), URI.join('http://foo', 'bar', 'baz')) + assert_equal(URI.parse('http://foo/baz'), URI.join('http://foo', 'bar', '/baz')) + assert_equal(URI.parse('http://foo/baz/'), URI.join('http://foo', 'bar', '/baz/')) + assert_equal(URI.parse('http://foo/bar/baz'), URI.join('http://foo', 'bar/', 'baz')) + assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar', 'baz', 'hoge')) + + assert_equal(URI.parse('http://foo/bar/baz'), URI.join('http://foo', 'bar/baz')) + assert_equal(URI.parse('http://foo/bar/hoge'), URI.join('http://foo', 'bar/baz', 'hoge')) + assert_equal(URI.parse('http://foo/bar/baz/hoge'), URI.join('http://foo', 'bar/baz/', 'hoge')) + assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar/baz', '/hoge')) + assert_equal(URI.parse('http://foo/bar/hoge'), URI.join('http://foo', 'bar/baz', 'hoge')) + assert_equal(URI.parse('http://foo/bar/baz/hoge'), URI.join('http://foo', 'bar/baz/', 'hoge')) + assert_equal(URI.parse('http://foo/hoge'), URI.join('http://foo', 'bar/baz', '/hoge')) + end + + # ruby-dev:16728 + 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('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(['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('zab', uri.host = 'zab') + assert_equal('http://foo:bar@zab', uri.to_s) + 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('/', 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_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?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_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_set_scheme + uri = URI.parse 'HTTP://example' + + assert_equal 'http://example', uri.to_s + 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) + 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) + + 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) + 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 + assert_raise(URI::BadURIError){ URI("foo").find_proxy } + with_env({}) { + assert_nil(URI("http://192.0.2.1/").find_proxy) + assert_nil(URI("ftp://192.0.2.1/").find_proxy) + } + with_env('http_proxy'=>'http://127.0.0.1:8080') { + assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy) + assert_nil(URI("ftp://192.0.2.1/").find_proxy) + } + with_env('ftp_proxy'=>'http://127.0.0.1:8080') { + assert_nil(URI("http://192.0.2.1/").find_proxy) + assert_equal(URI('http://127.0.0.1:8080'), URI("ftp://192.0.2.1/").find_proxy) + } + with_env('REQUEST_METHOD'=>'GET') { + assert_nil(URI("http://192.0.2.1/").find_proxy) + } + with_env('CGI_HTTP_PROXY'=>'http://127.0.0.1:8080', 'REQUEST_METHOD'=>'GET') { + assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy) + } + with_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'192.0.2.2') { + assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy) + assert_nil(URI("http://192.0.2.2/").find_proxy) + } + with_env('http_proxy'=>'') { + assert_nil(URI("http://192.0.2.1/").find_proxy) + assert_nil(URI("ftp://192.0.2.1/").find_proxy) + } + with_env('ftp_proxy'=>'') { + assert_nil(URI("http://192.0.2.1/").find_proxy) + assert_nil(URI("ftp://192.0.2.1/").find_proxy) + } + end + + def test_find_proxy_case_sensitive_env + with_env('http_proxy'=>'http://127.0.0.1:8080', 'REQUEST_METHOD'=>'GET') { + assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy) + } + with_env('HTTP_PROXY'=>'http://127.0.0.1:8081', 'REQUEST_METHOD'=>'GET') { + assert_nil(nil, URI("http://192.0.2.1/").find_proxy) + } + with_env('http_proxy'=>'http://127.0.0.1:8080', 'HTTP_PROXY'=>'http://127.0.0.1:8081', 'REQUEST_METHOD'=>'GET') { + assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy) + } + end unless RUBY_PLATFORM =~ /mswin|mingw/ + + def with_env(h) + ['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 + ensure + h.each_key {|k| ENV[k] = old[k] } + end + end + +end diff --git a/jni/ruby/test/uri/test_http.rb b/jni/ruby/test/uri/test_http.rb new file mode 100644 index 0000000..5d04e8c --- /dev/null +++ b/jni/ruby/test/uri/test_http.rb @@ -0,0 +1,64 @@ +require 'test/unit' +require 'uri/http' + +module URI + + +class TestHTTP < 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_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)) + end + + def test_normalize + host = 'aBcD' + 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('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')) + end + + def test_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('/?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_raise(ArgumentError) do + u.select(:scheme, :host, :not_exist, :port) + end + end +end + + +end diff --git a/jni/ruby/test/uri/test_ldap.rb b/jni/ruby/test/uri/test_ldap.rb new file mode 100644 index 0000000..a4cdbff --- /dev/null +++ b/jni/ruby/test/uri/test_ldap.rb @@ -0,0 +1,100 @@ +require 'test/unit' +require 'uri/ldap' + +module URI + + +class TestLDAP < 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_parse + url = 'ldap://ldap.jaist.ac.jp/o=JAIST,c=JP?sn?base?(sn=ttate*)' + u = URI.parse(url) + assert_kind_of(URI::LDAP, u) + assert_equal(url, u.to_s) + assert_equal('o=JAIST,c=JP', u.dn) + assert_equal('sn', u.attributes) + assert_equal('base', u.scope) + assert_equal('(sn=ttate*)', u.filter) + assert_equal(nil, u.extensions) + + u.scope = URI::LDAP::SCOPE_SUB + u.attributes = 'sn,cn,mail' + assert_equal('ldap://ldap.jaist.ac.jp/o=JAIST,c=JP?sn,cn,mail?sub?(sn=ttate*)', u.to_s) + assert_equal('o=JAIST,c=JP', u.dn) + assert_equal('sn,cn,mail', u.attributes) + assert_equal('sub', u.scope) + assert_equal('(sn=ttate*)', u.filter) + assert_equal(nil, u.extensions) + + # from RFC2255, section 6. + { + 'ldap:///o=University%20of%20Michigan,c=US' => + ['ldap', nil, 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', + 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, + '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, + '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', + 'objectClass', 'one', nil, nil], + + 'ldap://ldap.question.com/o=Question%3f,c=US?mail' => + ['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, + '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, + '', + nil, 'sub', nil, 'bindname=cn=Manager%2co=Foo'], + + 'ldap:///??sub??!bindname=cn=Manager%2co=Foo' => + ['ldap', nil, URI::LDAP::DEFAULT_PORT, + '', + nil, 'sub', nil, '!bindname=cn=Manager%2co=Foo'], + }.each do |url2, ary| + u = URI.parse(url2) + assert_equal(ary, uri_to_ary(u)) + end + end + + def test_select + u = URI.parse('ldap:///??sub??!bindname=cn=Manager%2co=Foo') + assert_equal(uri_to_ary(u), u.select(*u.component)) + assert_raise(ArgumentError) do + u.select(:scheme, :host, :not_exist, :port) + end + end +end + + +end diff --git a/jni/ruby/test/uri/test_mailto.rb b/jni/ruby/test/uri/test_mailto.rb new file mode 100644 index 0000000..661f7f7 --- /dev/null +++ b/jni/ruby/test/uri/test_mailto.rb @@ -0,0 +1,136 @@ +require 'test/unit' +require 'uri/mailto' + +module URI + + +class TestMailTo < Test::Unit::TestCase + def setup + @u = URI::MailTo + end + + def teardown + end + + def uri_to_ary(uri) + uri.class.component.collect {|c| uri.send(c)} + end + + def test_build + ok = [] + bad = [] + + # RFC2368, 6. Examples + # mailto:chris@example.com + ok << ["mailto:chris@example.com"] + 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", + :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", + :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", + ["body=send%20current-issue%0D%0Asend%20index"]] + 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", + ["In-Reply-To=%3c3469A91.D10AF4C@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", + :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", + :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, + ["to=joe@example.com", "cc=bob@example.com", "body=hello"]] + ok[-1] << {:headers => ["to=joe@example.com", + "cc=bob@example.com", "body=hello"]} + + # mailto:gorby%25kremvax@example.com + ok << ["mailto:gorby%25kremvax@example.com"] + ok[-1] << ["gorby%25kremvax@example.com", nil] + ok[-1] << {:to => "gorby%25kremvax@example.com"} + + # 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", + :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']]} + + ok_all = ok.flatten.join("\0") + + # mailto:joe@example.com?cc=bob@example.com?body=hello ; WRONG! + bad << ["joe@example.com", ["cc=bob@example.com?body=hello"]] + + # mailto:javascript:alert() + bad << ["javascript:alert()", []] + + # '=' 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) + end + + bad.each do |x| + assert_raise(URI::InvalidComponentError) { + @u.build(x) + } + end + + assert_equal(ok_all, ok.flatten.join("\0")) + 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_raise(ArgumentError) do + u.select(:scheme, :host, :not_exist, :port) + end + end +end + + +end diff --git a/jni/ruby/test/uri/test_parser.rb b/jni/ruby/test/uri/test_parser.rb new file mode 100644 index 0000000..188b4f8 --- /dev/null +++ b/jni/ruby/test/uri/test_parser.rb @@ -0,0 +1,47 @@ +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_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(u0 == u1) + assert(u0.eql?(u1)) + assert(!u0.equal?(u1)) + + assert(u1 == u2) + assert(!u1.eql?(u2)) + assert(!u1.equal?(u2)) + + assert(u2 == u3) + assert(u2.eql?(u3)) + assert(!u2.equal?(u3)) + end + + def test_parse + 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)) + end + + def test_raise_bad_uri_for_integer + assert_raise(URI::InvalidURIError) do + URI.parse(1) + end + end +end |