1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/uri] URI#HTTP#origin and URI#HTTP#authority (https://github.com/ruby/uri/pull/30)

https://github.com/ruby/uri/commit/bf13946c32

Co-authored-by: Samuel Williams <samuel.williams@oriontransfer.co.nz>
This commit is contained in:
Tiago 2021-09-20 22:53:42 +01:00 committed by Hiroshi SHIBATA
parent c8ad024e8e
commit 553f234a07
2 changed files with 62 additions and 7 deletions

View file

@ -80,6 +80,45 @@ module URI
url = @query ? "#@path?#@query" : @path.dup url = @query ? "#@path?#@query" : @path.dup
url.start_with?(?/.freeze) ? url : ?/ + url url.start_with?(?/.freeze) ? url : ?/ + url
end end
#
# == Description
#
# Returns the authority for an HTTP uri, as defined in
# https://datatracker.ietf.org/doc/html/rfc3986/#section-3.2.
#
#
# Example:
#
# URI::HTTP.build(host: 'www.example.com', path: '/foo/bar').authority #=> "www.example.com"
# URI::HTTP.build(host: 'www.example.com', port: 8000, path: '/foo/bar').authority #=> "www.example.com:8000"
# URI::HTTP.build(host: 'www.example.com', port: 80, path: '/foo/bar').authority #=> "www.example.com"
#
def authority
if port == default_port
host
else
"#{host}:#{port}"
end
end
#
# == Description
#
# Returns the origin for an HTTP uri, as defined in
# https://datatracker.ietf.org/doc/html/rfc6454.
#
#
# Example:
#
# URI::HTTP.build(host: 'www.example.com', path: '/foo/bar').origin #=> "http://www.example.com"
# URI::HTTP.build(host: 'www.example.com', port: 8000, path: '/foo/bar').origin #=> "http://www.example.com:8000"
# URI::HTTP.build(host: 'www.example.com', port: 80, path: '/foo/bar').origin #=> "http://www.example.com"
# URI::HTTPS.build(host: 'www.example.com', path: '/foo/bar').origin #=> "https://www.example.com"
#
def origin
"#{scheme}://#{authority}"
end
end end
register_scheme 'HTTP', HTTP register_scheme 'HTTP', HTTP

View file

@ -24,9 +24,11 @@ class TestHTTP < Test::Unit::TestCase
def test_parse def test_parse
u = URI.parse('http://a') u = URI.parse('http://a')
assert_kind_of(URI::HTTP, u) assert_kind_of(URI::HTTP, u)
assert_equal(['http', assert_equal([
'http',
nil, 'a', URI::HTTP.default_port, nil, 'a', URI::HTTP.default_port,
'', nil, nil], uri_to_ary(u)) '', nil, nil
], uri_to_ary(u))
end end
def test_normalize def test_normalize
@ -64,6 +66,20 @@ class TestHTTP < Test::Unit::TestCase
u.select(:scheme, :host, :not_exist, :port) u.select(:scheme, :host, :not_exist, :port)
end end
end end
def test_authority
assert_equal('a.b.c', URI.parse('http://a.b.c/').authority)
assert_equal('a.b.c:8081', URI.parse('http://a.b.c:8081/').authority)
assert_equal('a.b.c', URI.parse('http://a.b.c:80/').authority)
end
def test_origin
assert_equal('http://a.b.c', URI.parse('http://a.b.c/').origin)
assert_equal('http://a.b.c:8081', URI.parse('http://a.b.c:8081/').origin)
assert_equal('http://a.b.c', URI.parse('http://a.b.c:80/').origin)
assert_equal('https://a.b.c', URI.parse('https://a.b.c/').origin)
end
end end