Skip to content

Commit ba69937

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

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

lib/net/http.rb

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ class HTTPHeaderSyntaxError < StandardError; end
6161
#
6262
# === GET by URI
6363
#
64-
# uri = URI('http://example.com/index.html?count=10')
65-
# Net::HTTP.get(uri) # => String
64+
# Net::HTTP.get('http://example.com/index.html?count=10') # => String
6665
#
6766
# === GET with Dynamic Parameters
6867
#
@@ -117,8 +116,7 @@ class HTTPHeaderSyntaxError < StandardError; end
117116
#
118117
# === Response Data
119118
#
120-
# uri = URI('http://example.com/index.html')
121-
# res = Net::HTTP.get_response(uri)
119+
# res = Net::HTTP.get_response('http://example.com/index.html')
122120
#
123121
# # Headers
124122
# res['Set-Cookie'] # => String
@@ -150,7 +148,7 @@ class HTTPHeaderSyntaxError < StandardError; end
150148
# # You should choose a better exception.
151149
# raise ArgumentError, 'too many HTTP redirects' if limit == 0
152150
#
153-
# response = Net::HTTP.get_response(URI(uri_str))
151+
# response = Net::HTTP.get_response(uri_str)
154152
#
155153
# case response
156154
# when Net::HTTPSuccess then
@@ -261,12 +259,11 @@ class HTTPHeaderSyntaxError < StandardError; end
261259
# response = http.request request # Net::HTTPResponse object
262260
# end
263261
#
264-
# Or if you simply want to make a GET request, you may pass in an URI
265-
# object that has an HTTPS URL. Net::HTTP automatically turns on TLS
266-
# verification if the URI object has a 'https' URI scheme.
262+
# Or if you simply want to make a GET request, you may pass in a HTTPS URL.
263+
# Net::HTTP automatically turns on TLS verification if the URL has a 'https'
264+
# scheme.
267265
#
268-
# uri = URI('https://example.com/')
269-
# Net::HTTP.get(uri) # => String
266+
# Net::HTTP.get('https://example.com/') # => String
270267
#
271268
# In previous versions of Ruby you would need to require 'net/https' to use
272269
# HTTPS. This is no longer true.
@@ -452,7 +449,7 @@ def HTTP.get_print(uri_or_host, path_or_headers = nil, port = nil)
452449
# as a string. The target can either be specified as
453450
# (+uri+, +headers+), or as (+host+, +path+, +port+ = 80); so:
454451
#
455-
# print Net::HTTP.get(URI('http://www.example.com/index.html'))
452+
# print Net::HTTP.get('http://www.example.com/index.html')
456453
#
457454
# or:
458455
#
@@ -470,7 +467,7 @@ def HTTP.get(uri_or_host, path_or_headers = nil, port = nil)
470467
# as a Net::HTTPResponse object. The target can either be specified as
471468
# (+uri+, +headers+), or as (+host+, +path+, +port+ = 80); so:
472469
#
473-
# res = Net::HTTP.get_response(URI('http://www.example.com/index.html'))
470+
# res = Net::HTTP.get_response('http://www.example.com/index.html')
474471
# print res.body
475472
#
476473
# or:
@@ -480,7 +477,7 @@ def HTTP.get(uri_or_host, path_or_headers = nil, port = nil)
480477
#
481478
# you can also specify request headers:
482479
#
483-
# Net::HTTP.get_response(URI('http://www.example.com/index.html'), { 'Accept' => 'text/html' })
480+
# Net::HTTP.get_response('http://www.example.com/index.html', { 'Accept' => 'text/html' })
484481
#
485482
def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block)
486483
if path_or_headers && !path_or_headers.is_a?(Hash)
@@ -491,6 +488,7 @@ def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block)
491488
}
492489
else
493490
uri = uri_or_host
491+
uri = URI(uri) if uri.is_a?(String)
494492
headers = path_or_headers
495493
start(uri.hostname, uri.port,
496494
: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)