Skip to content

Commit

Permalink
Add URI-string support for Net::HTTP.post and Net::HTTP.post_form
Browse files Browse the repository at this point in the history
  • Loading branch information
kyanagi committed May 1, 2021
1 parent ba69937 commit 7a4dc8d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
14 changes: 8 additions & 6 deletions lib/net/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,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 @@ -497,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 @@ -529,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
50 changes: 48 additions & 2 deletions test/net/http/test_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -509,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 @@ -528,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

0 comments on commit 7a4dc8d

Please sign in to comment.