mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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]
This commit is contained in:
parent
12009fb3b9
commit
e0512b29aa
Notes:
git
2020-03-11 14:26:47 +09:00
Merged-By: k0kubun <takashikkbn@gmail.com>
4 changed files with 56 additions and 11 deletions
6
NEWS.md
6
NEWS.md
|
@ -98,6 +98,12 @@ Outstanding ones only.
|
||||||
* Add Net::HTTP#verify_hostname= and Net::HTTP#verify_hostname
|
* Add Net::HTTP#verify_hostname= and Net::HTTP#verify_hostname
|
||||||
to skip hostname verification. [[Feature #16555]]
|
to skip hostname verification. [[Feature #16555]]
|
||||||
|
|
||||||
|
* Modified method
|
||||||
|
|
||||||
|
* Net::HTTP.get, Net::HTTP.get_response, and Net::HTTP.get_print can
|
||||||
|
take request headers as Hash in the second argument when the first
|
||||||
|
argument is an URI.
|
||||||
|
|
||||||
## Compatibility issues
|
## Compatibility issues
|
||||||
|
|
||||||
Excluding feature bug fixes.
|
Excluding feature bug fixes.
|
||||||
|
|
|
@ -427,7 +427,7 @@ module Net #:nodoc:
|
||||||
#
|
#
|
||||||
# Gets the body text from the target and outputs it to $stdout. The
|
# Gets the body text from the target and outputs it to $stdout. The
|
||||||
# target can either be specified as
|
# target can either be specified as
|
||||||
# (+uri+), or as (+host+, +path+, +port+ = 80); so:
|
# (+uri+, +headers+), or as (+host+, +path+, +port+ = 80); so:
|
||||||
#
|
#
|
||||||
# Net::HTTP.get_print URI('http://www.example.com/index.html')
|
# Net::HTTP.get_print URI('http://www.example.com/index.html')
|
||||||
#
|
#
|
||||||
|
@ -435,8 +435,12 @@ module Net #:nodoc:
|
||||||
#
|
#
|
||||||
# Net::HTTP.get_print 'www.example.com', '/index.html'
|
# Net::HTTP.get_print 'www.example.com', '/index.html'
|
||||||
#
|
#
|
||||||
def HTTP.get_print(uri_or_host, path = nil, port = nil)
|
# you can also specify request headers:
|
||||||
get_response(uri_or_host, path, port) {|res|
|
#
|
||||||
|
# Net::HTTP.get_print URI('http://www.example.com/index.html'), { 'Accept' => 'text/html' }
|
||||||
|
#
|
||||||
|
def HTTP.get_print(uri_or_host, path_or_headers = nil, port = nil)
|
||||||
|
get_response(uri_or_host, path_or_headers, port) {|res|
|
||||||
res.read_body do |chunk|
|
res.read_body do |chunk|
|
||||||
$stdout.print chunk
|
$stdout.print chunk
|
||||||
end
|
end
|
||||||
|
@ -446,7 +450,7 @@ module Net #:nodoc:
|
||||||
|
|
||||||
# Sends a GET request to the target and returns the HTTP response
|
# Sends a GET request to the target and returns the HTTP response
|
||||||
# as a string. The target can either be specified as
|
# as a string. The target can either be specified as
|
||||||
# (+uri+), or as (+host+, +path+, +port+ = 80); so:
|
# (+uri+, +headers+), or as (+host+, +path+, +port+ = 80); so:
|
||||||
#
|
#
|
||||||
# print Net::HTTP.get(URI('http://www.example.com/index.html'))
|
# print Net::HTTP.get(URI('http://www.example.com/index.html'))
|
||||||
#
|
#
|
||||||
|
@ -454,13 +458,17 @@ module Net #:nodoc:
|
||||||
#
|
#
|
||||||
# print Net::HTTP.get('www.example.com', '/index.html')
|
# print Net::HTTP.get('www.example.com', '/index.html')
|
||||||
#
|
#
|
||||||
def HTTP.get(uri_or_host, path = nil, port = nil)
|
# you can also specify request headers:
|
||||||
get_response(uri_or_host, path, port).body
|
#
|
||||||
|
# Net::HTTP.get_response(URI('http://www.example.com/index.html'), { 'Accept' => 'text/html' })
|
||||||
|
#
|
||||||
|
def HTTP.get(uri_or_host, path_or_headers = nil, port = nil)
|
||||||
|
get_response(uri_or_host, path_or_headers, port).body
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sends a GET request to the target and returns the HTTP response
|
# Sends a GET request to the target and returns the HTTP response
|
||||||
# as a Net::HTTPResponse object. The target can either be specified as
|
# as a Net::HTTPResponse object. The target can either be specified as
|
||||||
# (+uri+), or as (+host+, +path+, +port+ = 80); so:
|
# (+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(URI('http://www.example.com/index.html'))
|
||||||
# print res.body
|
# print res.body
|
||||||
|
@ -470,17 +478,23 @@ module Net #:nodoc:
|
||||||
# res = Net::HTTP.get_response('www.example.com', '/index.html')
|
# res = Net::HTTP.get_response('www.example.com', '/index.html')
|
||||||
# print res.body
|
# print res.body
|
||||||
#
|
#
|
||||||
def HTTP.get_response(uri_or_host, path = nil, port = nil, &block)
|
# you can also specify request headers:
|
||||||
if path
|
#
|
||||||
|
# Net::HTTP.get_response(URI('http://www.example.com/index.html'), { 'Accept' => 'text/html' })
|
||||||
|
#
|
||||||
|
def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block)
|
||||||
|
if path_or_headers && !path_or_headers.is_a?(Hash)
|
||||||
host = uri_or_host
|
host = uri_or_host
|
||||||
|
path = path_or_headers
|
||||||
new(host, port || HTTP.default_port).start {|http|
|
new(host, port || HTTP.default_port).start {|http|
|
||||||
return http.request_get(path, &block)
|
return http.request_get(path, &block)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
uri = uri_or_host
|
uri = uri_or_host
|
||||||
|
headers = path_or_headers
|
||||||
start(uri.hostname, uri.port,
|
start(uri.hostname, uri.port,
|
||||||
:use_ssl => uri.scheme == 'https') {|http|
|
:use_ssl => uri.scheme == 'https') {|http|
|
||||||
return http.request_get(uri, &block)
|
return http.request_get(uri, headers, &block)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -283,6 +283,27 @@ module TestNetHTTP_version_1_1_methods
|
||||||
def test_s_get
|
def test_s_get
|
||||||
assert_equal $test_net_http_data,
|
assert_equal $test_net_http_data,
|
||||||
Net::HTTP.get(config('host'), '/', config('port'))
|
Net::HTTP.get(config('host'), '/', config('port'))
|
||||||
|
|
||||||
|
assert_equal $test_net_http_data, Net::HTTP.get(
|
||||||
|
URI.parse("http://#{config('host')}:#{config('port')}")
|
||||||
|
)
|
||||||
|
assert_equal $test_net_http_data, Net::HTTP.get(
|
||||||
|
URI.parse("http://#{config('host')}:#{config('port')}"), "Accept" => "text/plain"
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_s_get_response
|
||||||
|
res = Net::HTTP.get_response(
|
||||||
|
URI.parse("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(
|
||||||
|
URI.parse("http://#{config('host')}:#{config('port')}"), "Accept" => "text/plain"
|
||||||
|
)
|
||||||
|
assert_equal "text/plain", res["Content-Type"]
|
||||||
|
assert_equal $test_net_http_data, res.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_head
|
def test_head
|
||||||
|
|
|
@ -81,7 +81,11 @@ module TestNetHTTPUtils
|
||||||
end
|
end
|
||||||
|
|
||||||
def do_GET(req, res)
|
def do_GET(req, res)
|
||||||
|
if req['Accept'] != '*/*'
|
||||||
|
res['Content-Type'] = req['Accept']
|
||||||
|
else
|
||||||
res['Content-Type'] = $test_net_http_data_type
|
res['Content-Type'] = $test_net_http_data_type
|
||||||
|
end
|
||||||
res.body = $test_net_http_data
|
res.body = $test_net_http_data
|
||||||
res.chunked = @chunked
|
res.chunked = @chunked
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue