From b8c06e3cddc4984f330d077fca5a7f9ba38d19f6 Mon Sep 17 00:00:00 2001 From: Kouhei Yanagita Date: Sat, 1 May 2021 12:01:58 +0900 Subject: [PATCH 1/3] Net::HTTP.get_response can receive URI as string --- lib/net/http.rb | 7 +++++++ test/net/http/test_http.rb | 25 ++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/net/http.rb b/lib/net/http.rb index f64f7ba..9bfa908 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -796,6 +796,12 @@ def HTTP.get_print(uri_or_host, path_or_headers = nil, port = nil) # headers = {'Content-type' => 'application/json; charset=UTF-8'} # Net::HTTP.get(uri, headers) # + # Alternatively, +uri+ may be a String: + # + # uri = 'https://jsonplaceholder.typicode.com/todos/1' + # headers = {'Content-type' => 'application/json; charset=UTF-8'} + # Net::HTTP.get(uri, headers) + # # Related: # # - Net::HTTP::Get: request class for \HTTP method +GET+. @@ -820,6 +826,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 366b4cd..a947257 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -303,6 +303,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')}") ) @@ -311,7 +318,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 63398f3c0016d8ec7abfef7a55e29e4f253bb0ad Mon Sep 17 00:00:00 2001 From: Kouhei Yanagita Date: Sat, 1 May 2021 20:37:15 +0900 Subject: [PATCH 2/3] Add URI-string support for Net::HTTP.post and Net::HTTP.post_form --- lib/net/http.rb | 6 +++-- test/net/http/test_http.rb | 48 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/net/http.rb b/lib/net/http.rb index 9bfa908..3d76c62 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -837,7 +837,7 @@ def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block) # Posts data to a host; returns a Net::HTTPResponse object. # - # Argument +url+ must be a URL; + # Argument +url+ must be a URI object or a URI string; # argument +data+ must be a string: # # _uri = uri.dup @@ -862,6 +862,7 @@ def HTTP.get_response(uri_or_host, path_or_headers = nil, port = nil, &block) # - Net::HTTP#post: convenience method for \HTTP method +POST+. # 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) @@ -870,7 +871,7 @@ def HTTP.post(url, data, header = nil) # Posts data to a host; returns a Net::HTTPResponse object. # - # Argument +url+ must be a URI; + # Argument +url+ must be a URI object or a URI string; # argument +data+ must be a hash: # # _uri = uri.dup @@ -889,6 +890,7 @@ def HTTP.post(url, data, header = nil) # } # 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 a947257..ba53cbd 100644 --- a/test/net/http/test_http.rb +++ b/test/net/http/test_http.rb @@ -515,7 +515,24 @@ 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 = Net::HTTP.post( + url, + "a=x") + assert_equal "application/octet-stream", 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 = Net::HTTP.post( URI.parse(url), @@ -532,7 +549,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), From 0674aef962b153662b8bc85f1509a8f871a1dded Mon Sep 17 00:00:00 2001 From: Kouhei Yanagita Date: Wed, 25 Jun 2025 10:04:42 +0900 Subject: [PATCH 3/3] Add URI-string support for Net::HTTP.put --- lib/net/http.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/net/http.rb b/lib/net/http.rb index 3d76c62..56e1510 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -902,7 +902,7 @@ def HTTP.post_form(url, params) # Sends a PUT request to the server; returns a Net::HTTPResponse object. # - # Argument +url+ must be a URL; + # Argument +url+ must be a URL object or a URI string; # argument +data+ must be a string: # # _uri = uri.dup @@ -927,6 +927,7 @@ def HTTP.post_form(url, params) # - Net::HTTP#put: convenience method for \HTTP method +PUT+. # def HTTP.put(url, data, header = nil) + url = URI(url) if url.is_a?(String) start(url.hostname, url.port, :use_ssl => url.scheme == 'https' ) {|http| http.put(url, data, header)