Skip to content

Commit 63398f3

Browse files
committed
Add URI-string support for Net::HTTP.post and Net::HTTP.post_form
1 parent b8c06e3 commit 63398f3

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

lib/net/http.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block)
837837

838838
# Posts data to a host; returns a Net::HTTPResponse object.
839839
#
840-
# Argument +url+ must be a URL;
840+
# Argument +url+ must be a URI object or a URI string;
841841
# argument +data+ must be a string:
842842
#
843843
# _uri = uri.dup
@@ -862,6 +862,7 @@ def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block)
862862
# - Net::HTTP#post: convenience method for \HTTP method +POST+.
863863
#
864864
def HTTP.post(url, data, header = nil)
865+
url = URI(url) if url.is_a?(String)
865866
start(url.hostname, url.port,
866867
:use_ssl => url.scheme == 'https' ) {|http|
867868
http.post(url, data, header)
@@ -870,7 +871,7 @@ def HTTP.post(url, data, header = nil)
870871

871872
# Posts data to a host; returns a Net::HTTPResponse object.
872873
#
873-
# Argument +url+ must be a URI;
874+
# Argument +url+ must be a URI object or a URI string;
874875
# argument +data+ must be a hash:
875876
#
876877
# _uri = uri.dup
@@ -889,6 +890,7 @@ def HTTP.post(url, data, header = nil)
889890
# }
890891
#
891892
def HTTP.post_form(url, params)
893+
url = URI(url) if url.is_a?(String)
892894
req = Post.new(url)
893895
req.form_data = params
894896
req.basic_auth url.user, url.password if url.user

test/net/http/test_http.rb

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,24 @@ def _test_post__no_data(http)
515515
end
516516
end
517517

518-
def test_s_post
518+
def test_s_post_with_uri_string
519+
url = "http://#{config('host')}:#{config('port')}/?q=a"
520+
res = Net::HTTP.post(
521+
url,
522+
"a=x")
523+
assert_equal "application/octet-stream", res["Content-Type"]
524+
assert_equal "a=x", res.body
525+
assert_equal url, res["X-request-uri"]
526+
527+
res = Net::HTTP.post(
528+
url,
529+
"hello world",
530+
"Content-Type" => "text/plain; charset=US-ASCII")
531+
assert_equal "text/plain; charset=US-ASCII", res["Content-Type"]
532+
assert_equal "hello world", res.body
533+
end
534+
535+
def test_s_post_with_uri
519536
url = "http://#{config('host')}:#{config('port')}/?q=a"
520537
res = Net::HTTP.post(
521538
URI.parse(url),
@@ -532,7 +549,34 @@ def test_s_post
532549
assert_equal "hello world", res.body
533550
end
534551

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

0 commit comments

Comments
 (0)