From 7a4dc8d0548665f650a11aac39eb51e553250d76 Mon Sep 17 00:00:00 2001 From: Kouhei Yanagita Date: Sat, 1 May 2021 20:37:15 +0900 Subject: [PATCH] Add URI-string support for Net::HTTP.post and Net::HTTP.post_form --- lib/net/http.rb | 14 ++++++----- test/net/http/test_http.rb | 50 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/lib/net/http.rb b/lib/net/http.rb index fc1105cf..3fdda659 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -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 # @@ -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: # @@ -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 diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index f437010e..5263ffa9 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -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( @@ -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),