Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URI#HTTP#origin and URI#HTTP#authority #30

Merged
merged 3 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/uri/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,42 @@ def request_uri
url = @query ? "#@path?#@query" : @path.dup
url.start_with?(?/.freeze) ? url : ?/ + url
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
port_string = port == default_port ? nil : ":#{port}"
"#{host}#{port_string}"
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

register_scheme 'HTTP', HTTP
Expand Down
14 changes: 14 additions & 0 deletions test/uri/test_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ def test_select
u.select(:scheme, :host, :not_exist, :port)
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


Expand Down