Skip to content

Commit 7a4dc8d

Browse files
committed
Add URI-string support for Net::HTTP.post and Net::HTTP.post_form
1 parent ba69937 commit 7a4dc8d

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

lib/net/http.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ class HTTPHeaderSyntaxError < StandardError; end
7474
#
7575
# === POST
7676
#
77-
# uri = URI('http://www.example.com/search.cgi')
77+
# uri = 'http://www.example.com/search.cgi'
7878
# res = Net::HTTP.post_form(uri, 'q' => 'ruby', 'max' => '50')
7979
# puts res.body
8080
#
8181
# === POST with Multiple Values
8282
#
83-
# uri = URI('http://www.example.com/search.cgi')
83+
# uri = 'http://www.example.com/search.cgi'
8484
# res = Net::HTTP.post_form(uri, 'q' => ['ruby', 'perl'], 'max' => '50')
8585
# puts res.body
8686
#
@@ -497,25 +497,26 @@ def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block)
497497
end
498498
end
499499

500-
# Posts data to the specified URI object.
500+
# Posts data to the specified URI.
501501
#
502502
# Example:
503503
#
504504
# require 'net/http'
505505
# require 'uri'
506506
#
507-
# Net::HTTP.post URI('http://www.example.com/api/search'),
507+
# Net::HTTP.post 'http://www.example.com/api/search',
508508
# { "q" => "ruby", "max" => "50" }.to_json,
509509
# "Content-Type" => "application/json"
510510
#
511511
def HTTP.post(url, data, header = nil)
512+
url = URI(url) if url.is_a?(String)
512513
start(url.hostname, url.port,
513514
:use_ssl => url.scheme == 'https' ) {|http|
514515
http.post(url, data, header)
515516
}
516517
end
517518

518-
# Posts HTML form data to the specified URI object.
519+
# Posts HTML form data to the specified URI.
519520
# The form data must be provided as a Hash mapping from String to String.
520521
# Example:
521522
#
@@ -529,10 +530,11 @@ def HTTP.post(url, data, header = nil)
529530
#
530531
# require 'net/http'
531532
#
532-
# Net::HTTP.post_form URI('http://www.example.com/search.cgi'),
533+
# Net::HTTP.post_form 'http://www.example.com/search.cgi',
533534
# { "q" => "ruby", "max" => "50" }
534535
#
535536
def HTTP.post_form(url, params)
537+
url = URI(url) if url.is_a?(String)
536538
req = Post.new(url)
537539
req.form_data = params
538540
req.basic_auth url.user, url.password if url.user

test/net/http/test_http.rb

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,26 @@ def _test_post__no_data(http)
509509
end
510510
end
511511

512-
def test_s_post
512+
def test_s_post_with_uri_string
513+
url = "http://#{config('host')}:#{config('port')}/?q=a"
514+
res = assert_warning(/Content-Type did not set/) do
515+
Net::HTTP.post(
516+
url,
517+
"a=x")
518+
end
519+
assert_equal "application/x-www-form-urlencoded", res["Content-Type"]
520+
assert_equal "a=x", res.body
521+
assert_equal url, res["X-request-uri"]
522+
523+
res = Net::HTTP.post(
524+
url,
525+
"hello world",
526+
"Content-Type" => "text/plain; charset=US-ASCII")
527+
assert_equal "text/plain; charset=US-ASCII", res["Content-Type"]
528+
assert_equal "hello world", res.body
529+
end
530+
531+
def test_s_post_with_uri
513532
url = "http://#{config('host')}:#{config('port')}/?q=a"
514533
res = assert_warning(/Content-Type did not set/) do
515534
Net::HTTP.post(
@@ -528,7 +547,34 @@ def test_s_post
528547
assert_equal "hello world", res.body
529548
end
530549

531-
def test_s_post_form
550+
def test_s_post_form_with_uri_string
551+
url = "http://#{config('host')}:#{config('port')}/"
552+
res = Net::HTTP.post_form(
553+
url,
554+
"a" => "x")
555+
assert_equal ["a=x"], res.body.split(/[;&]/).sort
556+
557+
res = Net::HTTP.post_form(
558+
url,
559+
"a" => "x",
560+
"b" => "y")
561+
assert_equal ["a=x", "b=y"], res.body.split(/[;&]/).sort
562+
563+
res = Net::HTTP.post_form(
564+
url,
565+
"a" => ["x1", "x2"],
566+
"b" => "y")
567+
assert_equal url, res['X-request-uri']
568+
assert_equal ["a=x1", "a=x2", "b=y"], res.body.split(/[;&]/).sort
569+
570+
res = Net::HTTP.post_form(
571+
url + '?a=x',
572+
"b" => "y")
573+
assert_equal url + '?a=x', res['X-request-uri']
574+
assert_equal ["b=y"], res.body.split(/[;&]/).sort
575+
end
576+
577+
def test_s_post_form_with_uri
532578
url = "http://#{config('host')}:#{config('port')}/"
533579
res = Net::HTTP.post_form(
534580
URI.parse(url),

0 commit comments

Comments
 (0)