Skip to content

Commit

Permalink
Net::HTTP.get_response can receive URI as string
Browse files Browse the repository at this point in the history
  • Loading branch information
kyanagi committed May 1, 2021
1 parent 21fcf40 commit c639473
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/net/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def HTTP.get_print(uri_or_host, path_or_headers = nil, port = nil)
# as a string. The target can either be specified as
# (+uri+, +headers+), or as (+host+, +path+, +port+ = 80); so:
#
# print Net::HTTP.get(URI('http://www.example.com/index.html'))
# print Net::HTTP.get('http://www.example.com/index.html')
#
# or:
#
Expand All @@ -470,7 +470,7 @@ def HTTP.get(uri_or_host, path_or_headers = nil, port = nil)
# as a Net::HTTPResponse object. The target can either be specified as
# (+uri+, +headers+), or as (+host+, +path+, +port+ = 80); so:
#
# res = Net::HTTP.get_response(URI('http://www.example.com/index.html'))
# res = Net::HTTP.get_response('http://www.example.com/index.html')
# print res.body
#
# or:
Expand All @@ -491,6 +491,7 @@ def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block)
}
else
uri = uri_or_host
uri = URI(uri) if uri.is_a?(String)
headers = path_or_headers
start(uri.hostname, uri.port,
:use_ssl => uri.scheme == 'https') {|http|
Expand Down
25 changes: 24 additions & 1 deletion test/net/http/test_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ def test_s_get
assert_equal $test_net_http_data,
Net::HTTP.get(config('host'), '/', config('port'))

assert_equal $test_net_http_data,
Net::HTTP.get("http://#{config('host')}:#{config('port')}")

assert_equal $test_net_http_data, Net::HTTP.get(
"http://#{config('host')}:#{config('port')}", "Accept" => "text/plain"
)

assert_equal $test_net_http_data, Net::HTTP.get(
URI.parse("http://#{config('host')}:#{config('port')}")
)
Expand All @@ -309,7 +316,23 @@ def test_s_get
)
end

def test_s_get_response
def test_s_get_response_with_host
res = Net::HTTP.get_response(config('host'), '/', config('port'))
assert_equal "application/octet-stream", res["Content-Type"]
assert_equal $test_net_http_data, res.body
end

def test_s_get_response_with_uri_string
res = Net::HTTP.get_response("http://#{config('host')}:#{config('port')}")
assert_equal "application/octet-stream", res["Content-Type"]
assert_equal $test_net_http_data, res.body

res = Net::HTTP.get_response("http://#{config('host')}:#{config('port')}", "Accept" => "text/plain")
assert_equal "text/plain", res["Content-Type"]
assert_equal $test_net_http_data, res.body
end

def test_s_get_response_with_uri
res = Net::HTTP.get_response(
URI.parse("http://#{config('host')}:#{config('port')}")
)
Expand Down

0 comments on commit c639473

Please sign in to comment.