2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-10-05 02:09:26 -04:00
|
|
|
require 'test/unit'
|
2019-08-19 05:37:22 -04:00
|
|
|
require 'envutil'
|
2003-10-04 00:02:16 -04:00
|
|
|
require 'uri'
|
2003-10-05 02:09:26 -04:00
|
|
|
|
2007-07-31 23:40:08 -04:00
|
|
|
class URI::TestGeneric < Test::Unit::TestCase
|
2003-10-04 00:02:16 -04:00
|
|
|
def setup
|
|
|
|
@url = 'http://a/b/c/d;p?q'
|
|
|
|
@base_url = URI.parse(@url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
end
|
|
|
|
|
2003-10-05 02:09:26 -04:00
|
|
|
def uri_to_ary(uri)
|
|
|
|
uri.class.component.collect {|c| uri.send(c)}
|
|
|
|
end
|
|
|
|
|
2015-12-08 16:27:34 -05:00
|
|
|
def test_to_s
|
|
|
|
exp = 'http://example.com/'.freeze
|
|
|
|
str = URI(exp).to_s
|
|
|
|
assert_equal exp, str
|
2015-12-08 18:48:37 -05:00
|
|
|
assert_not_predicate str, :frozen?, '[ruby-core:71785] [Bug #11759]'
|
2017-12-01 10:09:41 -05:00
|
|
|
|
|
|
|
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
|
2015-12-08 16:27:34 -05:00
|
|
|
end
|
|
|
|
|
2003-10-04 00:02:16 -04:00
|
|
|
def test_parse
|
|
|
|
# 0
|
|
|
|
assert_kind_of(URI::HTTP, @base_url)
|
|
|
|
|
|
|
|
exp = [
|
2009-03-05 22:56:38 -05:00
|
|
|
'http',
|
|
|
|
nil, 'a', URI::HTTP.default_port,
|
|
|
|
'/b/c/d;p',
|
2003-10-04 00:02:16 -04:00
|
|
|
'q',
|
|
|
|
nil
|
|
|
|
]
|
2003-10-05 02:09:26 -04:00
|
|
|
ary = uri_to_ary(@base_url)
|
2003-10-04 00:02:16 -04:00
|
|
|
assert_equal(exp, ary)
|
|
|
|
|
|
|
|
# 1
|
|
|
|
url = URI.parse('ftp://ftp.is.co.za/rfc/rfc1808.txt')
|
|
|
|
assert_kind_of(URI::FTP, url)
|
|
|
|
|
|
|
|
exp = [
|
2009-03-05 22:56:38 -05:00
|
|
|
'ftp',
|
|
|
|
nil, 'ftp.is.co.za', URI::FTP.default_port,
|
2005-10-10 00:24:07 -04:00
|
|
|
'rfc/rfc1808.txt', nil,
|
2003-10-04 00:02:16 -04:00
|
|
|
]
|
2003-10-05 02:09:26 -04:00
|
|
|
ary = uri_to_ary(url)
|
2003-10-04 00:02:16 -04:00
|
|
|
assert_equal(exp, ary)
|
2008-09-10 05:34:49 -04:00
|
|
|
# 1'
|
|
|
|
url = URI.parse('ftp://ftp.is.co.za/%2Frfc/rfc1808.txt')
|
|
|
|
assert_kind_of(URI::FTP, url)
|
|
|
|
|
|
|
|
exp = [
|
2009-03-05 22:56:38 -05:00
|
|
|
'ftp',
|
|
|
|
nil, 'ftp.is.co.za', URI::FTP.default_port,
|
2008-09-10 05:34:49 -04:00
|
|
|
'/rfc/rfc1808.txt', nil,
|
|
|
|
]
|
|
|
|
ary = uri_to_ary(url)
|
|
|
|
assert_equal(exp, ary)
|
2003-10-04 00:02:16 -04:00
|
|
|
|
|
|
|
# 2
|
|
|
|
url = URI.parse('gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles')
|
|
|
|
assert_kind_of(URI::Generic, url)
|
|
|
|
|
|
|
|
exp = [
|
2009-03-05 22:56:38 -05:00
|
|
|
'gopher',
|
2003-10-04 00:02:16 -04:00
|
|
|
nil, 'spinaltap.micro.umn.edu', nil, nil,
|
|
|
|
'/00/Weather/California/Los%20Angeles', nil,
|
|
|
|
nil,
|
|
|
|
nil
|
|
|
|
]
|
2003-10-05 02:09:26 -04:00
|
|
|
ary = uri_to_ary(url)
|
2003-10-04 00:02:16 -04:00
|
|
|
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 = [
|
2009-03-05 22:56:38 -05:00
|
|
|
'http',
|
|
|
|
nil, 'www.math.uio.no', URI::HTTP.default_port,
|
|
|
|
'/faq/compression-faq/part1.html',
|
2003-10-04 00:02:16 -04:00
|
|
|
nil,
|
|
|
|
nil
|
|
|
|
]
|
2003-10-05 02:09:26 -04:00
|
|
|
ary = uri_to_ary(url)
|
2003-10-04 00:02:16 -04:00
|
|
|
assert_equal(exp, ary)
|
|
|
|
|
|
|
|
# 4
|
|
|
|
url = URI.parse('mailto:mduerst@ifi.unizh.ch')
|
|
|
|
assert_kind_of(URI::Generic, url)
|
|
|
|
|
|
|
|
exp = [
|
2009-03-05 22:56:38 -05:00
|
|
|
'mailto',
|
2003-10-04 00:02:16 -04:00
|
|
|
'mduerst@ifi.unizh.ch',
|
|
|
|
[]
|
|
|
|
]
|
2003-10-05 02:09:26 -04:00
|
|
|
ary = uri_to_ary(url)
|
2003-10-04 00:02:16 -04:00
|
|
|
assert_equal(exp, ary)
|
|
|
|
|
|
|
|
# 5
|
|
|
|
url = URI.parse('news:comp.infosystems.www.servers.unix')
|
|
|
|
assert_kind_of(URI::Generic, url)
|
|
|
|
|
|
|
|
exp = [
|
2009-03-05 22:56:38 -05:00
|
|
|
'news',
|
|
|
|
nil, nil, nil, nil,
|
2003-10-04 00:02:16 -04:00
|
|
|
nil, 'comp.infosystems.www.servers.unix',
|
|
|
|
nil,
|
|
|
|
nil
|
|
|
|
]
|
2003-10-05 02:09:26 -04:00
|
|
|
ary = uri_to_ary(url)
|
2003-10-04 00:02:16 -04:00
|
|
|
assert_equal(exp, ary)
|
|
|
|
|
|
|
|
# 6
|
|
|
|
url = URI.parse('telnet://melvyl.ucop.edu/')
|
|
|
|
assert_kind_of(URI::Generic, url)
|
|
|
|
|
|
|
|
exp = [
|
2009-03-05 22:56:38 -05:00
|
|
|
'telnet',
|
|
|
|
nil, 'melvyl.ucop.edu', nil, nil,
|
2003-10-04 00:02:16 -04:00
|
|
|
'/', nil,
|
|
|
|
nil,
|
|
|
|
nil
|
|
|
|
]
|
2003-10-05 02:09:26 -04:00
|
|
|
ary = uri_to_ary(url)
|
2003-10-04 00:02:16 -04:00
|
|
|
assert_equal(exp, ary)
|
|
|
|
|
|
|
|
# 7
|
|
|
|
# reported by Mr. Kubota <em6t-kbt@asahi-net.or.jp>
|
2014-06-21 20:22:19 -04:00
|
|
|
assert_nothing_raised(URI::InvalidURIError) { URI.parse('http://a_b:80/') }
|
|
|
|
assert_nothing_raised(URI::InvalidURIError) { URI.parse('http://a_b/') }
|
2003-10-04 00:02:16 -04:00
|
|
|
|
|
|
|
# 8
|
2005-02-23 01:08:29 -05:00
|
|
|
# reported by m_seki
|
2014-06-16 05:23:02 -04:00
|
|
|
url = URI.parse('file:///foo/bar.txt')
|
2003-10-04 00:02:16 -04:00
|
|
|
assert_kind_of(URI::Generic, url)
|
2014-06-16 05:23:02 -04:00
|
|
|
url = URI.parse('file:/foo/bar.txt')
|
2003-10-04 00:02:16 -04:00
|
|
|
assert_kind_of(URI::Generic, url)
|
2005-02-23 01:08:29 -05:00
|
|
|
|
|
|
|
# 9
|
|
|
|
url = URI.parse('ftp://:pass@localhost/')
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_equal('', url.user, "[ruby-dev:25667]")
|
2005-02-23 01:08:29 -05:00
|
|
|
assert_equal('pass', url.password)
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_equal(':pass', url.userinfo, "[ruby-dev:25667]")
|
2005-02-23 01:08:29 -05:00
|
|
|
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)
|
2003-10-04 00:02:16 -04:00
|
|
|
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/')
|
|
|
|
|
2013-11-21 02:26:53 -05:00
|
|
|
{
|
|
|
|
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}>"
|
|
|
|
}
|
|
|
|
}
|
2003-10-04 00:02:16 -04:00
|
|
|
|
|
|
|
url = URI.parse('http://hoge/a.html') + 'b.html'
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_equal('http://hoge/b.html', url.to_s, "[ruby-dev:11508]")
|
2003-10-04 00:02:16 -04:00
|
|
|
|
|
|
|
# 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)
|
2004-06-09 03:15:56 -04:00
|
|
|
|
2017-12-13 20:11:28 -05:00
|
|
|
url = URI.parse('http://a/b//c') + 'd//e'
|
|
|
|
assert_equal('http://a/b//d//e', url.to_s)
|
|
|
|
|
2004-06-09 03:15:56 -04:00
|
|
|
u0 = URI.parse('mailto:foo@example.com')
|
|
|
|
u1 = URI.parse('mailto:foo@example.com#bar')
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_equal(uri_to_ary(u0 + '#bar'), uri_to_ary(u1), "[ruby-dev:23628]")
|
2004-07-13 06:50:26 -04:00
|
|
|
|
|
|
|
u0 = URI.parse('http://www.example.com/')
|
|
|
|
u1 = URI.parse('http://www.example.com/foo/..') + './'
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_equal(u0, u1, "[ruby-list:39838]")
|
2004-07-13 06:50:26 -04:00
|
|
|
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/..'
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_equal(u0, u1, "[ruby-list:39844]")
|
2004-07-13 06:50:26 -04:00
|
|
|
u = URI.parse('http://www.example.com/')
|
|
|
|
u0 = u + './'
|
|
|
|
u1 = u + './foo/bar/../..'
|
|
|
|
assert_equal(u0, u1)
|
2003-10-04 00:02:16 -04:00
|
|
|
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)
|
2003-12-09 06:07:36 -05:00
|
|
|
|
|
|
|
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)
|
2003-12-22 06:49:20 -05:00
|
|
|
|
2011-04-15 02:21:56 -04:00
|
|
|
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)
|
|
|
|
|
2017-12-13 20:11:28 -05:00
|
|
|
url = URI.parse('http://hoge/b//c').route_to('http://hoge/b/c')
|
|
|
|
assert_equal('../c', url.to_s)
|
|
|
|
|
2003-12-22 06:49:20 -05:00
|
|
|
url = URI.parse('file:///a/b/').route_to('file:///a/b/')
|
|
|
|
assert_equal('', url.to_s)
|
2011-04-15 02:13:08 -04:00
|
|
|
url = URI.parse('file:///a/b/').route_to('file:///a/b')
|
|
|
|
assert_equal('../b', url.to_s)
|
2004-06-09 03:15:56 -04:00
|
|
|
|
|
|
|
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)
|
2003-10-04 00:02:16 -04:00
|
|
|
end
|
|
|
|
|
2008-09-10 05:34:49 -04:00
|
|
|
def test_rfc3986_examples
|
2003-10-04 00:02:16 -04:00
|
|
|
# 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
|
2007-07-31 23:40:08 -04:00
|
|
|
# ?y = http://a/b/c/d;p?y
|
2003-10-04 00:02:16 -04:00
|
|
|
url = @base_url.merge('?y')
|
|
|
|
assert_kind_of(URI::HTTP, url)
|
2007-07-31 23:40:08 -04:00
|
|
|
assert_equal('http://a/b/c/d;p?y', url.to_s)
|
|
|
|
url = @base_url.route_to('http://a/b/c/d;p?y')
|
2003-10-04 00:02:16 -04:00
|
|
|
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
|
2008-09-10 05:34:49 -04:00
|
|
|
# #s = http://a/b/c/d;p?q#s
|
2003-10-04 00:02:16 -04:00
|
|
|
url = @base_url.merge('#s')
|
|
|
|
assert_kind_of(URI::HTTP, url)
|
2008-09-10 05:34:49 -04:00
|
|
|
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')
|
2003-10-04 00:02:16 -04:00
|
|
|
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
|
2007-07-31 23:40:08 -04:00
|
|
|
# /./g = http://a/g
|
2003-10-04 00:02:16 -04:00
|
|
|
url = @base_url.merge('/./g')
|
|
|
|
assert_kind_of(URI::HTTP, url)
|
2007-07-31 23:40:08 -04:00
|
|
|
assert_equal('http://a/g', url.to_s)
|
2008-09-10 05:34:49 -04:00
|
|
|
# url = @base_url.route_to('http://a/./g')
|
|
|
|
# assert_kind_of(URI::Generic, url)
|
|
|
|
# assert_equal('/./g', url.to_s)
|
2003-10-04 00:02:16 -04:00
|
|
|
|
|
|
|
# http://a/b/c/d;p?q
|
2008-09-10 05:34:49 -04:00
|
|
|
# /../g = http://a/g
|
2003-10-04 00:02:16 -04:00
|
|
|
url = @base_url.merge('/../g')
|
|
|
|
assert_kind_of(URI::HTTP, url)
|
2007-07-31 23:40:08 -04:00
|
|
|
assert_equal('http://a/g', url.to_s)
|
2008-09-10 05:34:49 -04:00
|
|
|
# url = @base_url.route_to('http://a/../g')
|
|
|
|
# assert_kind_of(URI::Generic, url)
|
|
|
|
# assert_equal('/../g', url.to_s)
|
2003-10-04 00:02:16 -04:00
|
|
|
|
|
|
|
# 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
|
2007-07-31 23:40:08 -04:00
|
|
|
# ../../../g = http://a/g
|
2003-10-04 00:02:16 -04:00
|
|
|
url = @base_url.merge('../../../g')
|
|
|
|
assert_kind_of(URI::HTTP, url)
|
2007-07-31 23:40:08 -04:00
|
|
|
assert_equal('http://a/g', url.to_s)
|
2008-09-10 05:34:49 -04:00
|
|
|
url = @base_url.route_to('http://a/g')
|
2003-10-04 00:02:16 -04:00
|
|
|
assert_kind_of(URI::Generic, url)
|
2008-09-10 05:34:49 -04:00
|
|
|
assert('../../../g' != url.to_s) # ok? yes, it confuses you
|
|
|
|
assert_equal('../../g', url.to_s) # and it is clearly
|
2003-10-04 00:02:16 -04:00
|
|
|
|
|
|
|
# http://a/b/c/d;p?q
|
2008-09-10 05:34:49 -04:00
|
|
|
# ../../../../g = http://a/g
|
2003-10-04 00:02:16 -04:00
|
|
|
url = @base_url.merge('../../../../g')
|
|
|
|
assert_kind_of(URI::HTTP, url)
|
2007-07-31 23:40:08 -04:00
|
|
|
assert_equal('http://a/g', url.to_s)
|
2008-09-10 05:34:49 -04:00
|
|
|
url = @base_url.route_to('http://a/g')
|
2003-10-04 00:02:16 -04:00
|
|
|
assert_kind_of(URI::Generic, url)
|
|
|
|
assert('../../../../g' != url.to_s) # ok? yes, it confuses you
|
2008-09-10 05:34:49 -04:00
|
|
|
assert_equal('../../g', url.to_s) # and it is clearly
|
2003-10-04 00:02:16 -04:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2008-09-10 05:34:49 -04:00
|
|
|
# ruby-dev:16728
|
2003-10-04 00:02:16 -04:00
|
|
|
def test_set_component
|
|
|
|
uri = URI.parse('http://foo:bar@baz')
|
2008-09-10 05:34:49 -04:00
|
|
|
assert_equal('oof', uri.user = 'oof')
|
2003-10-04 00:02:16 -04:00
|
|
|
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)
|
2014-06-22 23:18:51 -04:00
|
|
|
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)
|
2003-10-04 00:02:16 -04:00
|
|
|
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)
|
2014-11-02 19:33:47 -05:00
|
|
|
assert_equal('a[]=1', uri.query = 'a[]=1')
|
2014-12-23 08:11:43 -05:00
|
|
|
assert_equal('http://foo:bar@zab:8080/?a[]=1#b123', uri.to_s)
|
2014-11-02 19:33:47 -05:00
|
|
|
uri = URI.parse('http://foo:bar@zab:8080/?a[]=1#b123')
|
2014-12-23 08:11:43 -05:00
|
|
|
assert_equal('http://foo:bar@zab:8080/?a[]=1#b123', uri.to_s)
|
2003-12-23 00:17:00 -05:00
|
|
|
|
|
|
|
uri = URI.parse('http://example.com')
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(URI::InvalidURIError) { uri.password = 'bar' }
|
2014-07-03 14:00:08 -04:00
|
|
|
assert_equal("foo\nbar", uri.query = "foo\nbar")
|
2003-12-23 00:17:00 -05:00
|
|
|
uri.userinfo = 'foo:bar'
|
2014-07-03 14:00:08 -04:00
|
|
|
assert_equal('http://foo:bar@example.com?foobar', uri.to_s)
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(URI::InvalidURIError) { uri.registry = 'bar' }
|
|
|
|
assert_raise(URI::InvalidURIError) { uri.opaque = 'bar' }
|
2003-12-23 00:17:00 -05:00
|
|
|
|
|
|
|
uri = URI.parse('mailto:foo@example.com')
|
2008-09-24 13:44:39 -04:00
|
|
|
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' }
|
2014-09-17 10:15:03 -04:00
|
|
|
|
|
|
|
uri = URI.parse('foo:bar')
|
|
|
|
assert_raise(URI::InvalidComponentError) { uri.opaque = '/baz' }
|
|
|
|
uri.opaque = 'xyzzy'
|
|
|
|
assert_equal('foo:xyzzy', uri.to_s)
|
2003-10-04 00:02:16 -04:00
|
|
|
end
|
2010-10-05 23:30:49 -04:00
|
|
|
|
2016-09-15 17:44:56 -04:00
|
|
|
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
|
2019-08-19 05:37:22 -04:00
|
|
|
refute_match Regexp.new(password), e.message
|
2016-09-15 17:44:56 -04:00
|
|
|
end
|
|
|
|
|
2012-04-11 18:11:07 -04:00
|
|
|
def test_set_scheme
|
|
|
|
uri = URI.parse 'HTTP://example'
|
|
|
|
|
|
|
|
assert_equal 'http://example', uri.to_s
|
|
|
|
end
|
|
|
|
|
2018-03-21 12:03:59 -04:00
|
|
|
def test_hierarchical
|
|
|
|
hierarchical = URI.parse('http://a.b.c/example')
|
|
|
|
opaque = URI.parse('mailto:mduerst@ifi.unizh.ch')
|
|
|
|
|
|
|
|
assert hierarchical.hierarchical?
|
|
|
|
refute opaque.hierarchical?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_absolute
|
|
|
|
abs_uri = URI.parse('http://a.b.c/')
|
|
|
|
not_abs = URI.parse('a.b.c')
|
|
|
|
|
|
|
|
refute not_abs.absolute?
|
|
|
|
|
|
|
|
assert abs_uri.absolute
|
|
|
|
assert abs_uri.absolute?
|
|
|
|
end
|
|
|
|
|
2010-10-05 23:30:49 -04:00
|
|
|
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
|
2012-05-18 03:32:08 -04:00
|
|
|
|
|
|
|
def test_build
|
2014-12-01 12:36:54 -05:00
|
|
|
u = URI::Generic.build(['http', nil, 'example.com', 80, nil, '/foo', nil, nil, nil])
|
|
|
|
assert_equal('http://example.com:80/foo', u.to_s)
|
2016-01-14 06:20:24 -05:00
|
|
|
assert_equal(Encoding::UTF_8, u.to_s.encoding)
|
2014-12-01 12:36:54 -05:00
|
|
|
|
2015-05-18 22:34:47 -04:00
|
|
|
u = URI::Generic.build(:port => "5432")
|
|
|
|
assert_equal(":5432", u.to_s)
|
|
|
|
assert_equal(5432, u.port)
|
|
|
|
|
2014-12-01 12:36:54 -05:00
|
|
|
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)
|
2012-05-18 03:32:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_build2
|
2014-12-01 12:36:54 -05:00
|
|
|
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)
|
2012-05-18 03:32:08 -04:00
|
|
|
end
|
2012-07-19 21:56:21 -04:00
|
|
|
|
|
|
|
# 192.0.2.0/24 is TEST-NET. [RFC3330]
|
|
|
|
|
2016-02-13 02:52:25 -05:00
|
|
|
def test_find_proxy_bad_uri
|
2013-07-24 06:52:15 -04:00
|
|
|
assert_raise(URI::BadURIError){ URI("foo").find_proxy }
|
2016-02-13 02:52:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_proxy_no_env
|
2016-10-11 13:23:29 -04:00
|
|
|
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))
|
2012-07-22 12:50:49 -04:00
|
|
|
}
|
2016-02-13 02:52:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_proxy
|
2016-10-11 13:23:29 -04:00
|
|
|
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))
|
2012-07-19 21:56:21 -04:00
|
|
|
}
|
2016-10-11 13:23:29 -04:00
|
|
|
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))
|
2012-07-19 21:56:21 -04:00
|
|
|
}
|
2016-02-13 02:52:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_proxy_get
|
2016-10-11 13:23:29 -04:00
|
|
|
with_proxy_env('REQUEST_METHOD'=>'GET') {|env|
|
|
|
|
assert_nil(URI("http://192.0.2.1/").find_proxy(env))
|
2012-07-19 21:56:21 -04:00
|
|
|
}
|
2016-10-11 13:23:29 -04:00
|
|
|
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))
|
2012-07-19 21:56:21 -04:00
|
|
|
}
|
2016-02-13 02:52:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_proxy_no_proxy
|
2020-05-06 03:06:25 -04:00
|
|
|
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
|
|
|
|
|
2016-10-11 13:23:29 -04:00
|
|
|
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))
|
2017-01-17 07:54:35 -05:00
|
|
|
|
2020-05-06 03:06:25 -04:00
|
|
|
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))
|
2012-07-19 21:56:21 -04:00
|
|
|
}
|
2016-10-11 13:23:29 -04:00
|
|
|
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))
|
2016-02-13 02:52:25 -05:00
|
|
|
}
|
2016-10-11 13:23:29 -04:00
|
|
|
with_proxy_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'.example.org') {|env|
|
2018-05-17 03:40:01 -04:00
|
|
|
assert_equal(URI('http://127.0.0.1:8080'), URI("http://example.org/").find_proxy(env))
|
2016-10-11 13:23:29 -04:00
|
|
|
assert_nil(URI("http://www.example.org/").find_proxy(env))
|
2016-02-13 03:12:21 -05:00
|
|
|
}
|
2020-05-06 03:06:25 -04:00
|
|
|
ensure
|
|
|
|
IPSocket.singleton_class.class_eval do
|
|
|
|
undef getaddress
|
|
|
|
define_method(:getaddress, getaddress)
|
|
|
|
end
|
2016-02-13 02:52:25 -05:00
|
|
|
end
|
|
|
|
|
2016-02-13 03:31:12 -05:00
|
|
|
def test_find_proxy_no_proxy_cidr
|
2016-10-11 13:23:29 -04:00
|
|
|
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))
|
2016-02-13 03:31:12 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-02-13 02:52:25 -05:00
|
|
|
def test_find_proxy_bad_value
|
2016-10-11 13:23:29 -04:00
|
|
|
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))
|
2012-07-22 12:55:55 -04:00
|
|
|
}
|
2016-10-11 13:23:29 -04:00
|
|
|
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))
|
2012-07-22 12:55:55 -04:00
|
|
|
}
|
2012-07-19 21:56:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_proxy_case_sensitive_env
|
2016-10-11 13:23:29 -04:00
|
|
|
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))
|
2012-07-19 21:56:21 -04:00
|
|
|
}
|
2016-10-11 13:23:29 -04:00
|
|
|
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))
|
2012-07-19 21:56:21 -04:00
|
|
|
}
|
2016-10-11 13:23:29 -04:00
|
|
|
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))
|
2012-07-19 21:56:21 -04:00
|
|
|
}
|
2016-10-11 13:23:29 -04:00
|
|
|
end
|
|
|
|
|
2017-09-28 06:51:31 -04:00
|
|
|
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],
|
2018-05-17 03:40:01 -04:00
|
|
|
['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],
|
2017-09-28 06:51:31 -04:00
|
|
|
['foo.example.com', nil, 80, 'example.com:80', false],
|
2018-05-17 03:40:01 -04:00
|
|
|
['foo.eXample.com', nil, 80, 'example.com:80', false],
|
|
|
|
['foo.example.com', nil, 80, 'eXample.com:80', false],
|
2017-09-28 06:51:31 -04:00
|
|
|
['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
|
|
|
|
|
2016-10-11 13:23:29 -04:00
|
|
|
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
|
2016-10-12 11:28:01 -04:00
|
|
|
@h.has_key?(k) ? @h[k][1] : nil
|
2016-10-11 13:23:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def length
|
|
|
|
@h.length
|
|
|
|
end
|
|
|
|
|
|
|
|
def include?(k)
|
|
|
|
@h.include? k.downcase
|
|
|
|
end
|
|
|
|
|
|
|
|
def shift
|
|
|
|
return nil if @h.empty?
|
2016-10-12 11:28:03 -04:00
|
|
|
_kd, (k, v) = @h.shift
|
2016-10-11 13:23:29 -04:00
|
|
|
[k, v]
|
|
|
|
end
|
2012-07-19 21:56:21 -04:00
|
|
|
|
2016-10-11 13:23:29 -04:00
|
|
|
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
|
2012-07-22 12:50:49 -04:00
|
|
|
['http', 'https', 'ftp'].each do |scheme|
|
|
|
|
name = "#{scheme}_proxy"
|
|
|
|
h[name] ||= nil
|
|
|
|
h["CGI_#{name.upcase}"] ||= nil
|
|
|
|
end
|
2012-07-19 21:56:21 -04:00
|
|
|
begin
|
|
|
|
old = {}
|
|
|
|
h.each_key {|k| old[k] = ENV[k] }
|
|
|
|
h.each {|k, v| ENV[k] = v }
|
2016-10-11 13:23:29 -04:00
|
|
|
yield ENV
|
2012-07-19 21:56:21 -04:00
|
|
|
ensure
|
|
|
|
h.each_key {|k| ENV[k] = old[k] }
|
|
|
|
end
|
2016-10-11 13:23:29 -04:00
|
|
|
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
|
2012-07-19 21:56:21 -04:00
|
|
|
end
|
|
|
|
|
2003-10-04 00:02:16 -04:00
|
|
|
end
|