Skip to content

Commit c639473

Browse files
committed
Net::HTTP.get_response can receive URI as string
1 parent 21fcf40 commit c639473

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/net/http.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ def HTTP.get_print(uri_or_host, path_or_headers = nil, port = nil)
452452
# as a string. The target can either be specified as
453453
# (+uri+, +headers+), or as (+host+, +path+, +port+ = 80); so:
454454
#
455-
# print Net::HTTP.get(URI('http://www.example.com/index.html'))
455+
# print Net::HTTP.get('http://www.example.com/index.html')
456456
#
457457
# or:
458458
#
@@ -470,7 +470,7 @@ def HTTP.get(uri_or_host, path_or_headers = nil, port = nil)
470470
# as a Net::HTTPResponse object. The target can either be specified as
471471
# (+uri+, +headers+), or as (+host+, +path+, +port+ = 80); so:
472472
#
473-
# res = Net::HTTP.get_response(URI('http://www.example.com/index.html'))
473+
# res = Net::HTTP.get_response('http://www.example.com/index.html')
474474
# print res.body
475475
#
476476
# or:
@@ -491,6 +491,7 @@ def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block)
491491
}
492492
else
493493
uri = uri_or_host
494+
uri = URI(uri) if uri.is_a?(String)
494495
headers = path_or_headers
495496
start(uri.hostname, uri.port,
496497
:use_ssl => uri.scheme == 'https') {|http|

test/net/http/test_http.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,13 @@ def test_s_get
301301
assert_equal $test_net_http_data,
302302
Net::HTTP.get(config('host'), '/', config('port'))
303303

304+
assert_equal $test_net_http_data,
305+
Net::HTTP.get("http://#{config('host')}:#{config('port')}")
306+
307+
assert_equal $test_net_http_data, Net::HTTP.get(
308+
"http://#{config('host')}:#{config('port')}", "Accept" => "text/plain"
309+
)
310+
304311
assert_equal $test_net_http_data, Net::HTTP.get(
305312
URI.parse("http://#{config('host')}:#{config('port')}")
306313
)
@@ -309,7 +316,23 @@ def test_s_get
309316
)
310317
end
311318

312-
def test_s_get_response
319+
def test_s_get_response_with_host
320+
res = Net::HTTP.get_response(config('host'), '/', config('port'))
321+
assert_equal "application/octet-stream", res["Content-Type"]
322+
assert_equal $test_net_http_data, res.body
323+
end
324+
325+
def test_s_get_response_with_uri_string
326+
res = Net::HTTP.get_response("http://#{config('host')}:#{config('port')}")
327+
assert_equal "application/octet-stream", res["Content-Type"]
328+
assert_equal $test_net_http_data, res.body
329+
330+
res = Net::HTTP.get_response("http://#{config('host')}:#{config('port')}", "Accept" => "text/plain")
331+
assert_equal "text/plain", res["Content-Type"]
332+
assert_equal $test_net_http_data, res.body
333+
end
334+
335+
def test_s_get_response_with_uri
313336
res = Net::HTTP.get_response(
314337
URI.parse("http://#{config('host')}:#{config('port')}")
315338
)

0 commit comments

Comments
 (0)