2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-10-05 02:09:26 -04:00
|
|
|
require 'test/unit'
|
2003-10-04 00:02:16 -04:00
|
|
|
require 'uri/http'
|
2021-10-22 10:54:51 -04:00
|
|
|
require 'uri/https'
|
2003-10-05 02:09:26 -04:00
|
|
|
|
2003-10-04 00:02:16 -04:00
|
|
|
module URI
|
|
|
|
|
2003-10-05 02:09:26 -04:00
|
|
|
|
|
|
|
class TestHTTP < Test::Unit::TestCase
|
2003-10-04 00:02:16 -04:00
|
|
|
def setup
|
|
|
|
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
|
|
|
|
|
2016-09-30 06:06:24 -04:00
|
|
|
def test_build
|
|
|
|
u = URI::HTTP.build(host: 'www.example.com', path: '/foo/bar')
|
|
|
|
assert_kind_of(URI::HTTP, u)
|
|
|
|
end
|
|
|
|
|
2003-10-04 00:02:16 -04:00
|
|
|
def test_parse
|
|
|
|
u = URI.parse('http://a')
|
|
|
|
assert_kind_of(URI::HTTP, u)
|
2021-09-20 17:53:42 -04:00
|
|
|
assert_equal([
|
|
|
|
'http',
|
|
|
|
nil, 'a', URI::HTTP.default_port,
|
|
|
|
'', nil, nil
|
|
|
|
], uri_to_ary(u))
|
2003-10-04 00:02:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_normalize
|
|
|
|
host = 'aBcD'
|
2021-09-20 17:53:42 -04:00
|
|
|
u1 = URI.parse('http://' + host + '/eFg?HiJ')
|
2003-10-04 00:02:16 -04:00
|
|
|
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
|
2021-09-20 17:53:42 -04:00
|
|
|
assert_equal('/', URI.parse('http://a.b.c/').request_uri)
|
2003-10-04 00:02:16 -04:00
|
|
|
assert_equal('/?abc=def', URI.parse('http://a.b.c/?abc=def').request_uri)
|
2021-09-20 17:53:42 -04:00
|
|
|
assert_equal('/', URI.parse('http://a.b.c').request_uri)
|
2003-10-04 00:02:16 -04:00
|
|
|
assert_equal('/?abc=def', URI.parse('http://a.b.c?abc=def').request_uri)
|
2021-09-20 17:53:42 -04:00
|
|
|
assert_equal(nil, URI.parse('http:foo').request_uri)
|
2003-10-04 00:02:16 -04:00
|
|
|
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/')
|
2003-10-05 02:09:26 -04:00
|
|
|
assert_equal(uri_to_ary(u), u.select(*u.component))
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(ArgumentError) do
|
2003-10-04 00:02:16 -04:00
|
|
|
u.select(:scheme, :host, :not_exist, :port)
|
|
|
|
end
|
|
|
|
end
|
2021-09-20 17:53:42 -04:00
|
|
|
|
|
|
|
def test_authority
|
|
|
|
assert_equal('a.b.c', URI.parse('http://a.b.c/').authority)
|
|
|
|
assert_equal('a.b.c:8081', URI.parse('http://a.b.c:8081/').authority)
|
|
|
|
assert_equal('a.b.c', URI.parse('http://a.b.c:80/').authority)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test_origin
|
|
|
|
assert_equal('http://a.b.c', URI.parse('http://a.b.c/').origin)
|
|
|
|
assert_equal('http://a.b.c:8081', URI.parse('http://a.b.c:8081/').origin)
|
|
|
|
assert_equal('http://a.b.c', URI.parse('http://a.b.c:80/').origin)
|
|
|
|
assert_equal('https://a.b.c', URI.parse('https://a.b.c/').origin)
|
|
|
|
end
|
2003-10-04 00:02:16 -04:00
|
|
|
end
|
|
|
|
|
2003-10-05 02:09:26 -04:00
|
|
|
|
2003-10-04 00:02:16 -04:00
|
|
|
end
|