Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Net::HTTP.get_response can receive URI as string #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions lib/net/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ class HTTPHeaderSyntaxError < StandardError; end
#
# === GET by URI
#
# uri = URI('http://example.com/index.html?count=10')
# Net::HTTP.get(uri) # => String
# Net::HTTP.get('http://example.com/index.html?count=10') # => String
#
# === GET with Dynamic Parameters
#
Expand All @@ -75,13 +74,13 @@ class HTTPHeaderSyntaxError < StandardError; end
#
# === POST
#
# uri = URI('http://www.example.com/search.cgi')
# uri = 'http://www.example.com/search.cgi'
# res = Net::HTTP.post_form(uri, 'q' => 'ruby', 'max' => '50')
# puts res.body
#
# === POST with Multiple Values
#
# uri = URI('http://www.example.com/search.cgi')
# uri = 'http://www.example.com/search.cgi'
# res = Net::HTTP.post_form(uri, 'q' => ['ruby', 'perl'], 'max' => '50')
# puts res.body
#
Expand Down Expand Up @@ -117,8 +116,7 @@ class HTTPHeaderSyntaxError < StandardError; end
#
# === Response Data
#
# uri = URI('http://example.com/index.html')
# res = Net::HTTP.get_response(uri)
# res = Net::HTTP.get_response('http://example.com/index.html')
#
# # Headers
# res['Set-Cookie'] # => String
Expand Down Expand Up @@ -150,7 +148,7 @@ class HTTPHeaderSyntaxError < StandardError; end
# # You should choose a better exception.
# raise ArgumentError, 'too many HTTP redirects' if limit == 0
#
# response = Net::HTTP.get_response(URI(uri_str))
# response = Net::HTTP.get_response(uri_str)
#
# case response
# when Net::HTTPSuccess then
Expand Down Expand Up @@ -261,12 +259,11 @@ class HTTPHeaderSyntaxError < StandardError; end
# response = http.request request # Net::HTTPResponse object
# end
#
# Or if you simply want to make a GET request, you may pass in an URI
# object that has an HTTPS URL. Net::HTTP automatically turns on TLS
# verification if the URI object has a 'https' URI scheme.
# Or if you simply want to make a GET request, you may pass in a HTTPS URL.
# Net::HTTP automatically turns on TLS verification if the URL has a 'https'
# scheme.
#
# uri = URI('https://example.com/')
# Net::HTTP.get(uri) # => String
# Net::HTTP.get('https://example.com/') # => String
#
# In previous versions of Ruby you would need to require 'net/https' to use
# HTTPS. This is no longer true.
Expand Down Expand Up @@ -452,7 +449,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 +467,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 @@ -480,7 +477,7 @@ def HTTP.get(uri_or_host, path_or_headers = nil, port = nil)
#
# you can also specify request headers:
#
# Net::HTTP.get_response(URI('http://www.example.com/index.html'), { 'Accept' => 'text/html' })
# Net::HTTP.get_response('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)
Expand All @@ -491,6 +488,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 All @@ -499,25 +497,26 @@ def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block)
end
end

# Posts data to the specified URI object.
# Posts data to the specified URI.
#
# Example:
#
# require 'net/http'
# require 'uri'
#
# Net::HTTP.post URI('http://www.example.com/api/search'),
# Net::HTTP.post 'http://www.example.com/api/search',
# { "q" => "ruby", "max" => "50" }.to_json,
# "Content-Type" => "application/json"
#
def HTTP.post(url, data, header = nil)
url = URI(url) if url.is_a?(String)
start(url.hostname, url.port,
:use_ssl => url.scheme == 'https' ) {|http|
http.post(url, data, header)
}
end

# Posts HTML form data to the specified URI object.
# Posts HTML form data to the specified URI.
# The form data must be provided as a Hash mapping from String to String.
# Example:
#
Expand All @@ -531,10 +530,11 @@ def HTTP.post(url, data, header = nil)
#
# require 'net/http'
#
# Net::HTTP.post_form URI('http://www.example.com/search.cgi'),
# Net::HTTP.post_form 'http://www.example.com/search.cgi',
# { "q" => "ruby", "max" => "50" }
#
def HTTP.post_form(url, params)
url = URI(url) if url.is_a?(String)
req = Post.new(url)
req.form_data = params
req.basic_auth url.user, url.password if url.user
Expand Down
75 changes: 72 additions & 3 deletions 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 Expand Up @@ -486,7 +509,26 @@ def _test_post__no_data(http)
end
end

def test_s_post
def test_s_post_with_uri_string
url = "http://#{config('host')}:#{config('port')}/?q=a"
res = assert_warning(/Content-Type did not set/) do
Net::HTTP.post(
url,
"a=x")
end
assert_equal "application/x-www-form-urlencoded", res["Content-Type"]
assert_equal "a=x", res.body
assert_equal url, res["X-request-uri"]

res = Net::HTTP.post(
url,
"hello world",
"Content-Type" => "text/plain; charset=US-ASCII")
assert_equal "text/plain; charset=US-ASCII", res["Content-Type"]
assert_equal "hello world", res.body
end

def test_s_post_with_uri
url = "http://#{config('host')}:#{config('port')}/?q=a"
res = assert_warning(/Content-Type did not set/) do
Net::HTTP.post(
Expand All @@ -505,7 +547,34 @@ def test_s_post
assert_equal "hello world", res.body
end

def test_s_post_form
def test_s_post_form_with_uri_string
url = "http://#{config('host')}:#{config('port')}/"
res = Net::HTTP.post_form(
url,
"a" => "x")
assert_equal ["a=x"], res.body.split(/[;&]/).sort

res = Net::HTTP.post_form(
url,
"a" => "x",
"b" => "y")
assert_equal ["a=x", "b=y"], res.body.split(/[;&]/).sort

res = Net::HTTP.post_form(
url,
"a" => ["x1", "x2"],
"b" => "y")
assert_equal url, res['X-request-uri']
assert_equal ["a=x1", "a=x2", "b=y"], res.body.split(/[;&]/).sort

res = Net::HTTP.post_form(
url + '?a=x',
"b" => "y")
assert_equal url + '?a=x', res['X-request-uri']
assert_equal ["b=y"], res.body.split(/[;&]/).sort
end

def test_s_post_form_with_uri
url = "http://#{config('host')}:#{config('port')}/"
res = Net::HTTP.post_form(
URI.parse(url),
Expand Down