Skip to content

Commit bf13946

Browse files
URI#HTTP#origin and URI#HTTP#authority (#30)
Co-authored-by: Samuel Williams <[email protected]>
1 parent bc47bf7 commit bf13946

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

lib/uri/http.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,45 @@ def request_uri
8080
url = @query ? "#@path?#@query" : @path.dup
8181
url.start_with?(?/.freeze) ? url : ?/ + url
8282
end
83+
84+
#
85+
# == Description
86+
#
87+
# Returns the authority for an HTTP uri, as defined in
88+
# https://datatracker.ietf.org/doc/html/rfc3986/#section-3.2.
89+
#
90+
#
91+
# Example:
92+
#
93+
# URI::HTTP.build(host: 'www.example.com', path: '/foo/bar').authority #=> "www.example.com"
94+
# URI::HTTP.build(host: 'www.example.com', port: 8000, path: '/foo/bar').authority #=> "www.example.com:8000"
95+
# URI::HTTP.build(host: 'www.example.com', port: 80, path: '/foo/bar').authority #=> "www.example.com"
96+
#
97+
def authority
98+
if port == default_port
99+
host
100+
else
101+
"#{host}:#{port}"
102+
end
103+
end
104+
105+
#
106+
# == Description
107+
#
108+
# Returns the origin for an HTTP uri, as defined in
109+
# https://datatracker.ietf.org/doc/html/rfc6454.
110+
#
111+
#
112+
# Example:
113+
#
114+
# URI::HTTP.build(host: 'www.example.com', path: '/foo/bar').origin #=> "http://www.example.com"
115+
# URI::HTTP.build(host: 'www.example.com', port: 8000, path: '/foo/bar').origin #=> "http://www.example.com:8000"
116+
# URI::HTTP.build(host: 'www.example.com', port: 80, path: '/foo/bar').origin #=> "http://www.example.com"
117+
# URI::HTTPS.build(host: 'www.example.com', path: '/foo/bar').origin #=> "https://www.example.com"
118+
#
119+
def origin
120+
"#{scheme}://#{authority}"
121+
end
83122
end
84123

85124
register_scheme 'HTTP', HTTP

test/uri/test_http.rb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ def test_build
2424
def test_parse
2525
u = URI.parse('http://a')
2626
assert_kind_of(URI::HTTP, u)
27-
assert_equal(['http',
28-
nil, 'a', URI::HTTP.default_port,
29-
'', nil, nil], uri_to_ary(u))
27+
assert_equal([
28+
'http',
29+
nil, 'a', URI::HTTP.default_port,
30+
'', nil, nil
31+
], uri_to_ary(u))
3032
end
3133

3234
def test_normalize
3335
host = 'aBcD'
34-
u1 = URI.parse('http://' + host + '/eFg?HiJ')
36+
u1 = URI.parse('http://' + host + '/eFg?HiJ')
3537
u2 = URI.parse('http://' + host.downcase + '/eFg?HiJ')
3638
assert(u1.normalize.host == 'abcd')
3739
assert(u1.normalize.path == u1.path)
@@ -49,11 +51,11 @@ def test_equal
4951
end
5052

5153
def test_request_uri
52-
assert_equal('/', URI.parse('http://a.b.c/').request_uri)
54+
assert_equal('/', URI.parse('http://a.b.c/').request_uri)
5355
assert_equal('/?abc=def', URI.parse('http://a.b.c/?abc=def').request_uri)
54-
assert_equal('/', URI.parse('http://a.b.c').request_uri)
56+
assert_equal('/', URI.parse('http://a.b.c').request_uri)
5557
assert_equal('/?abc=def', URI.parse('http://a.b.c?abc=def').request_uri)
56-
assert_equal(nil, URI.parse('http:foo').request_uri)
58+
assert_equal(nil, URI.parse('http:foo').request_uri)
5759
end
5860

5961
def test_select
@@ -64,6 +66,20 @@ def test_select
6466
u.select(:scheme, :host, :not_exist, :port)
6567
end
6668
end
69+
70+
def test_authority
71+
assert_equal('a.b.c', URI.parse('http://a.b.c/').authority)
72+
assert_equal('a.b.c:8081', URI.parse('http://a.b.c:8081/').authority)
73+
assert_equal('a.b.c', URI.parse('http://a.b.c:80/').authority)
74+
end
75+
76+
77+
def test_origin
78+
assert_equal('http://a.b.c', URI.parse('http://a.b.c/').origin)
79+
assert_equal('http://a.b.c:8081', URI.parse('http://a.b.c:8081/').origin)
80+
assert_equal('http://a.b.c', URI.parse('http://a.b.c:80/').origin)
81+
assert_equal('https://a.b.c', URI.parse('https://a.b.c/').origin)
82+
end
6783
end
6884

6985

0 commit comments

Comments
 (0)