Skip to content

Commit e61c140

Browse files
k0kubunhsbt
authored andcommitted
Let Net::HTTP.get take request headers (#2957)
* Let Net::HTTP.get take request headers * Add more test cases for no header usages * Add examples with request headers * Add a NEWS entry [ci skip] [Feature #16686]
1 parent 3391c84 commit e61c140

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

lib/net/http.rb

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -427,16 +427,20 @@ class << HTTP
427427
#
428428
# Gets the body text from the target and outputs it to $stdout. The
429429
# target can either be specified as
430-
# (+uri+), or as (+host+, +path+, +port+ = 80); so:
430+
# (+uri+, +headers+), or as (+host+, +path+, +port+ = 80); so:
431431
#
432432
# Net::HTTP.get_print URI('http://www.example.com/index.html')
433433
#
434434
# or:
435435
#
436436
# Net::HTTP.get_print 'www.example.com', '/index.html'
437437
#
438-
def HTTP.get_print(uri_or_host, path = nil, port = nil)
439-
get_response(uri_or_host, path, port) {|res|
438+
# you can also specify request headers:
439+
#
440+
# Net::HTTP.get_print URI('http://www.example.com/index.html'), { 'Accept' => 'text/html' }
441+
#
442+
def HTTP.get_print(uri_or_host, path_or_headers = nil, port = nil)
443+
get_response(uri_or_host, path_or_headers, port) {|res|
440444
res.read_body do |chunk|
441445
$stdout.print chunk
442446
end
@@ -446,21 +450,25 @@ def HTTP.get_print(uri_or_host, path = nil, port = nil)
446450

447451
# Sends a GET request to the target and returns the HTTP response
448452
# as a string. The target can either be specified as
449-
# (+uri+), or as (+host+, +path+, +port+ = 80); so:
453+
# (+uri+, +headers+), or as (+host+, +path+, +port+ = 80); so:
450454
#
451455
# print Net::HTTP.get(URI('http://www.example.com/index.html'))
452456
#
453457
# or:
454458
#
455459
# print Net::HTTP.get('www.example.com', '/index.html')
456460
#
457-
def HTTP.get(uri_or_host, path = nil, port = nil)
458-
get_response(uri_or_host, path, port).body
461+
# you can also specify request headers:
462+
#
463+
# Net::HTTP.get_response(URI('http://www.example.com/index.html'), { 'Accept' => 'text/html' })
464+
#
465+
def HTTP.get(uri_or_host, path_or_headers = nil, port = nil)
466+
get_response(uri_or_host, path_or_headers, port).body
459467
end
460468

461469
# Sends a GET request to the target and returns the HTTP response
462470
# as a Net::HTTPResponse object. The target can either be specified as
463-
# (+uri+), or as (+host+, +path+, +port+ = 80); so:
471+
# (+uri+, +headers+), or as (+host+, +path+, +port+ = 80); so:
464472
#
465473
# res = Net::HTTP.get_response(URI('http://www.example.com/index.html'))
466474
# print res.body
@@ -470,17 +478,23 @@ def HTTP.get(uri_or_host, path = nil, port = nil)
470478
# res = Net::HTTP.get_response('www.example.com', '/index.html')
471479
# print res.body
472480
#
473-
def HTTP.get_response(uri_or_host, path = nil, port = nil, &block)
474-
if path
481+
# you can also specify request headers:
482+
#
483+
# Net::HTTP.get_response(URI('http://www.example.com/index.html'), { 'Accept' => 'text/html' })
484+
#
485+
def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block)
486+
if path_or_headers && !path_or_headers.is_a?(Hash)
475487
host = uri_or_host
488+
path = path_or_headers
476489
new(host, port || HTTP.default_port).start {|http|
477490
return http.request_get(path, &block)
478491
}
479492
else
480493
uri = uri_or_host
494+
headers = path_or_headers
481495
start(uri.hostname, uri.port,
482496
:use_ssl => uri.scheme == 'https') {|http|
483-
return http.request_get(uri, &block)
497+
return http.request_get(uri, headers, &block)
484498
}
485499
end
486500
end

test/net/http/test_http.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,27 @@ def test_s_start
283283
def test_s_get
284284
assert_equal $test_net_http_data,
285285
Net::HTTP.get(config('host'), '/', config('port'))
286+
287+
assert_equal $test_net_http_data, Net::HTTP.get(
288+
URI.parse("http://#{config('host')}:#{config('port')}")
289+
)
290+
assert_equal $test_net_http_data, Net::HTTP.get(
291+
URI.parse("http://#{config('host')}:#{config('port')}"), "Accept" => "text/plain"
292+
)
293+
end
294+
295+
def test_s_get_response
296+
res = Net::HTTP.get_response(
297+
URI.parse("http://#{config('host')}:#{config('port')}")
298+
)
299+
assert_equal "application/octet-stream", res["Content-Type"]
300+
assert_equal $test_net_http_data, res.body
301+
302+
res = Net::HTTP.get_response(
303+
URI.parse("http://#{config('host')}:#{config('port')}"), "Accept" => "text/plain"
304+
)
305+
assert_equal "text/plain", res["Content-Type"]
306+
assert_equal $test_net_http_data, res.body
286307
end
287308

288309
def test_head

test/net/http/utils.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ def initialize(this, chunked = false)
8181
end
8282

8383
def do_GET(req, res)
84-
res['Content-Type'] = $test_net_http_data_type
84+
if req['Accept'] != '*/*'
85+
res['Content-Type'] = req['Accept']
86+
else
87+
res['Content-Type'] = $test_net_http_data_type
88+
end
8589
res.body = $test_net_http_data
8690
res.chunked = @chunked
8791
end

0 commit comments

Comments
 (0)