Skip to content

Allow Net::HTTP.get, get_response, post, post_form, put to receive URI String #24

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

Open
wants to merge 3 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
16 changes: 13 additions & 3 deletions lib/net/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,12 @@ def HTTP.get_print(uri_or_host, path_or_headers = nil, port = nil)
# headers = {'Content-type' => 'application/json; charset=UTF-8'}
# Net::HTTP.get(uri, headers)
#
# Alternatively, +uri+ may be a String:
#
# uri = 'https://jsonplaceholder.typicode.com/todos/1'
# headers = {'Content-type' => 'application/json; charset=UTF-8'}
# Net::HTTP.get(uri, headers)
#
# Related:
#
# - Net::HTTP::Get: request class for \HTTP method +GET+.
Expand All @@ -820,6 +826,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 @@ -830,7 +837,7 @@ def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block)

# Posts data to a host; returns a Net::HTTPResponse object.
#
# Argument +url+ must be a URL;
# Argument +url+ must be a URI object or a URI string;
# argument +data+ must be a string:
#
# _uri = uri.dup
Expand All @@ -855,6 +862,7 @@ def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block)
# - Net::HTTP#post: convenience method for \HTTP method +POST+.
#
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)
Expand All @@ -863,7 +871,7 @@ def HTTP.post(url, data, header = nil)

# Posts data to a host; returns a Net::HTTPResponse object.
#
# Argument +url+ must be a URI;
# Argument +url+ must be a URI object or a URI string;
# argument +data+ must be a hash:
#
# _uri = uri.dup
Expand All @@ -882,6 +890,7 @@ def HTTP.post(url, data, header = nil)
# }
#
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 All @@ -893,7 +902,7 @@ def HTTP.post_form(url, params)

# Sends a PUT request to the server; returns a Net::HTTPResponse object.
#
# Argument +url+ must be a URL;
# Argument +url+ must be a URL object or a URI string;
# argument +data+ must be a string:
#
# _uri = uri.dup
Expand All @@ -918,6 +927,7 @@ def HTTP.post_form(url, params)
# - Net::HTTP#put: convenience method for \HTTP method +PUT+.
#
def HTTP.put(url, data, header = nil)
url = URI(url) if url.is_a?(String)
start(url.hostname, url.port,
:use_ssl => url.scheme == 'https' ) {|http|
http.put(url, data, header)
Expand Down
73 changes: 70 additions & 3 deletions test/net/http/test_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,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 @@ -311,7 +318,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 @@ -492,7 +515,24 @@ 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 = Net::HTTP.post(
url,
"a=x")
assert_equal "application/octet-stream", 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 = Net::HTTP.post(
URI.parse(url),
Expand All @@ -509,7 +549,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