From ba6993743e9494189c1b8f03504f8a3fa4805d79 Mon Sep 17 00:00:00 2001 From: Kouhei Yanagita Date: Sat, 1 May 2021 12:01:58 +0900 Subject: [PATCH 1/2] Net::HTTP.get_response can receive URI as string --- lib/net/http.rb | 24 +++++++++++------------- test/net/http/test_http.rb | 25 ++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/lib/net/http.rb b/lib/net/http.rb index 1fcf1353..fc1105cf 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -61,8 +61,7 @@ class HTTPHeaderSyntaxError < StandardError; end # # === GET by URI # - # uri = URI('http://example.com/index.html?count=10') - # Net::HTTP.get(uri) # => String + # Net::HTTP.get('http://example.com/index.html?count=10') # => String # # === GET with Dynamic Parameters # @@ -117,8 +116,7 @@ class HTTPHeaderSyntaxError < StandardError; end # # === Response Data # - # uri = URI('http://example.com/index.html') - # res = Net::HTTP.get_response(uri) + # res = Net::HTTP.get_response('http://example.com/index.html') # # # Headers # res['Set-Cookie'] # => String @@ -150,7 +148,7 @@ class HTTPHeaderSyntaxError < StandardError; end # # You should choose a better exception. # raise ArgumentError, 'too many HTTP redirects' if limit == 0 # - # response = Net::HTTP.get_response(URI(uri_str)) + # response = Net::HTTP.get_response(uri_str) # # case response # when Net::HTTPSuccess then @@ -261,12 +259,11 @@ class HTTPHeaderSyntaxError < StandardError; end # response = http.request request # Net::HTTPResponse object # end # - # Or if you simply want to make a GET request, you may pass in an URI - # object that has an HTTPS URL. Net::HTTP automatically turns on TLS - # verification if the URI object has a 'https' URI scheme. + # Or if you simply want to make a GET request, you may pass in a HTTPS URL. + # Net::HTTP automatically turns on TLS verification if the URL has a 'https' + # scheme. # - # uri = URI('https://example.com/') - # Net::HTTP.get(uri) # => String + # Net::HTTP.get('https://example.com/') # => String # # In previous versions of Ruby you would need to require 'net/https' to use # HTTPS. This is no longer true. @@ -452,7 +449,7 @@ def HTTP.get_print(uri_or_host, path_or_headers = nil, port = nil) # as a string. The target can either be specified as # (+uri+, +headers+), or as (+host+, +path+, +port+ = 80); so: # - # print Net::HTTP.get(URI('http://www.example.com/index.html')) + # print Net::HTTP.get('http://www.example.com/index.html') # # or: # @@ -470,7 +467,7 @@ def HTTP.get(uri_or_host, path_or_headers = nil, port = nil) # as a Net::HTTPResponse object. The target can either be specified as # (+uri+, +headers+), or as (+host+, +path+, +port+ = 80); so: # - # res = Net::HTTP.get_response(URI('http://www.example.com/index.html')) + # res = Net::HTTP.get_response('http://www.example.com/index.html') # print res.body # # or: @@ -480,7 +477,7 @@ def HTTP.get(uri_or_host, path_or_headers = nil, port = nil) # # you can also specify request headers: # - # Net::HTTP.get_response(URI('http://www.example.com/index.html'), { 'Accept' => 'text/html' }) + # Net::HTTP.get_response('http://www.example.com/index.html', { 'Accept' => 'text/html' }) # def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block) if path_or_headers && !path_or_headers.is_a?(Hash) @@ -491,6 +488,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| diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb index 60b6d51f..f437010e 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -301,6 +301,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')}") ) @@ -309,7 +316,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')}") ) From 7a4dc8d0548665f650a11aac39eb51e553250d76 Mon Sep 17 00:00:00 2001 From: Kouhei Yanagita Date: Sat, 1 May 2021 20:37:15 +0900 Subject: [PATCH 2/2] 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),