Misc #15893 ยป deprecate-open-uri-kernel-open.patch
lib/open-uri.rb | ||
---|---|---|
alias open_uri_original_open open # :nodoc:
|
||
end
|
||
def open(name, *rest, &block) # :nodoc:
|
||
if (name.respond_to?(:open) && !name.respond_to?(:to_path)) ||
|
||
(name.respond_to?(:to_str) &&
|
||
%r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ name &&
|
||
(uri = URI.parse(name)).respond_to?(:open))
|
||
warn('calling URI.open via Kernel#open is deprecated, call URI.open directly', uplevel: 1)
|
||
URI.open(name, *rest, &block)
|
||
else
|
||
open_uri_original_open(name, *rest, &block)
|
||
end
|
||
end
|
||
module_function :open
|
||
end
|
||
module URI
|
||
# Allows the opening of various resources including URIs.
|
||
#
|
||
# If the first argument responds to the 'open' method, 'open' is called on
|
||
... | ... | |
#
|
||
# We can accept URIs and strings that begin with http://, https:// and
|
||
# ftp://. In these cases, the opened file object is extended by OpenURI::Meta.
|
||
def open(name, *rest, &block) # :doc:
|
||
def self.open(name, *rest, &block)
|
||
if name.respond_to?(:open)
|
||
name.open(*rest, &block)
|
||
elsif name.respond_to?(:to_str) &&
|
||
... | ... | |
uri.open(*rest, &block)
|
||
else
|
||
open_uri_original_open(name, *rest, &block)
|
||
# After Kernel#open override is removed:
|
||
#super
|
||
end
|
||
end
|
||
module_function :open
|
||
end
|
||
module URI #:nodoc:
|
||
# alias for Kernel.open defined in open-uri.
|
||
def self.open(name, *rest, &block)
|
||
Kernel.open(name, *rest, &block)
|
||
end
|
||
end
|
||
# OpenURI is an easy-to-use wrapper for Net::HTTP, Net::HTTPS and Net::FTP.
|
test/open-uri/test_open-uri.rb | ||
---|---|---|
@proxies.each_with_index {|k, i| ENV[k] = @old_proxies[i] }
|
||
end
|
||
def test_deprecated_kernel_open
|
||
with_http {|srv, dr, url|
|
||
srv.mount_proc("/foo200", lambda { |req, res| res.body = "foo200" } )
|
||
assert_warning(/calling URI.open via Kernel#open is deprecated, call URI.open directly/) {
|
||
open("#{url}/foo200") {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("foo200", f.read)
|
||
}
|
||
}
|
||
}
|
||
end
|
||
def test_200_uri_open
|
||
with_http {|srv, dr, url|
|
||
srv.mount_proc("/urifoo200", lambda { |req, res| res.body = "urifoo200" } )
|
||
... | ... | |
def test_200
|
||
with_http {|srv, dr, url|
|
||
srv.mount_proc("/foo200", lambda { |req, res| res.body = "foo200" } )
|
||
open("#{url}/foo200") {|f|
|
||
URI.open("#{url}/foo200") {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("foo200", f.read)
|
||
}
|
||
... | ... | |
with_http {|srv, dr, url|
|
||
content = "foo200big"*10240
|
||
srv.mount_proc("/foo200big", lambda { |req, res| res.body = content } )
|
||
open("#{url}/foo200big") {|f|
|
||
URI.open("#{url}/foo200big") {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal(content, f.read)
|
||
}
|
||
... | ... | |
assert_match(%r{ERROR `/not-exist' not found}, server_log[0])
|
||
}
|
||
with_http(log_tester) {|srv, dr, url, server_thread, server_log|
|
||
exc = assert_raise(OpenURI::HTTPError) { open("#{url}/not-exist") {} }
|
||
exc = assert_raise(OpenURI::HTTPError) { URI.open("#{url}/not-exist") {} }
|
||
assert_equal("404", exc.io.status[0])
|
||
}
|
||
end
|
||
... | ... | |
with_http {|srv, dr, url|
|
||
srv.mount_proc("/foo_ou", lambda { |req, res| res.body = "foo_ou" } )
|
||
u = URI("#{url}/foo_ou")
|
||
open(u) {|f|
|
||
URI.open(u) {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("foo_ou", f.read)
|
||
}
|
||
... | ... | |
end
|
||
def test_open_too_many_arg
|
||
assert_raise(ArgumentError) { open("http://192.0.2.1/tma", "r", 0666, :extra) {} }
|
||
assert_raise(ArgumentError) { URI.open("http://192.0.2.1/tma", "r", 0666, :extra) {} }
|
||
end
|
||
def test_read_timeout
|
||
... | ... | |
end
|
||
def test_invalid_option
|
||
assert_raise(ArgumentError) { open("http://127.0.0.1/", :invalid_option=>true) {} }
|
||
assert_raise(ArgumentError) { URI.open("http://127.0.0.1/", :invalid_option=>true) {} }
|
||
end
|
||
def test_mode
|
||
with_http {|srv, dr, url|
|
||
srv.mount_proc("/mode", lambda { |req, res| res.body = "mode" } )
|
||
open("#{url}/mode", "r") {|f|
|
||
URI.open("#{url}/mode", "r") {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("mode", f.read)
|
||
}
|
||
open("#{url}/mode", "r", 0600) {|f|
|
||
URI.open("#{url}/mode", "r", 0600) {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("mode", f.read)
|
||
}
|
||
assert_raise(ArgumentError) { open("#{url}/mode", "a") {} }
|
||
open("#{url}/mode", "r:us-ascii") {|f|
|
||
assert_raise(ArgumentError) { URI.open("#{url}/mode", "a") {} }
|
||
URI.open("#{url}/mode", "r:us-ascii") {|f|
|
||
assert_equal(Encoding::US_ASCII, f.read.encoding)
|
||
}
|
||
open("#{url}/mode", "r:utf-8") {|f|
|
||
URI.open("#{url}/mode", "r:utf-8") {|f|
|
||
assert_equal(Encoding::UTF_8, f.read.encoding)
|
||
}
|
||
assert_raise(ArgumentError) { open("#{url}/mode", "r:invalid-encoding") {} }
|
||
assert_raise(ArgumentError) { URI.open("#{url}/mode", "r:invalid-encoding") {} }
|
||
}
|
||
end
|
||
... | ... | |
with_http {|srv, dr, url|
|
||
srv.mount_proc("/without_block", lambda { |req, res| res.body = "without_block" } )
|
||
begin
|
||
f = open("#{url}/without_block")
|
||
f = URI.open("#{url}/without_block")
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("without_block", f.read)
|
||
ensure
|
||
... | ... | |
with_http {|srv, dr, url|
|
||
srv.mount_proc("/close200", lambda { |req, res| res.body = "close200" } )
|
||
assert_nothing_raised {
|
||
open("#{url}/close200") {|f|
|
||
URI.open("#{url}/close200") {|f|
|
||
f.close
|
||
}
|
||
}
|
||
... | ... | |
content = "close200big"*10240
|
||
srv.mount_proc("/close200big", lambda { |req, res| res.body = content } )
|
||
assert_nothing_raised {
|
||
open("#{url}/close200big") {|f|
|
||
URI.open("#{url}/close200big") {|f|
|
||
f.close
|
||
}
|
||
}
|
||
... | ... | |
myheader2 = nil
|
||
with_http {|srv, dr, url|
|
||
srv.mount_proc("/h/") {|req, res| myheader2 = req['myheader']; res.body = "foo" }
|
||
open("#{url}/h/", 'MyHeader'=>myheader1) {|f|
|
||
URI.open("#{url}/h/", 'MyHeader'=>myheader1) {|f|
|
||
assert_equal("foo", f.read)
|
||
assert_equal(myheader1, myheader2)
|
||
}
|
||
... | ... | |
def test_multi_proxy_opt
|
||
assert_raise(ArgumentError) {
|
||
open("http://127.0.0.1/", :proxy_http_basic_authentication=>true, :proxy=>true) {}
|
||
URI.open("http://127.0.0.1/", :proxy_http_basic_authentication=>true, :proxy=>true) {}
|
||
}
|
||
end
|
||
def test_non_http_proxy
|
||
assert_raise(RuntimeError) {
|
||
open("http://127.0.0.1/", :proxy=>URI("ftp://127.0.0.1/")) {}
|
||
URI.open("http://127.0.0.1/", :proxy=>URI("ftp://127.0.0.1/")) {}
|
||
}
|
||
end
|
||
... | ... | |
begin
|
||
proxy_thread = proxy.start
|
||
srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
|
||
open("#{url}/proxy", :proxy=>proxy_url) {|f|
|
||
URI.open("#{url}/proxy", :proxy=>proxy_url) {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("proxy", f.read)
|
||
}
|
||
assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
|
||
open("#{url}/proxy", :proxy=>URI(proxy_url)) {|f|
|
||
URI.open("#{url}/proxy", :proxy=>URI(proxy_url)) {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("proxy", f.read)
|
||
}
|
||
assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
|
||
open("#{url}/proxy", :proxy=>nil) {|f|
|
||
URI.open("#{url}/proxy", :proxy=>nil) {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("proxy", f.read)
|
||
}
|
||
assert_equal("", proxy_auth_log); proxy_auth_log.clear
|
||
assert_raise(ArgumentError) {
|
||
open("#{url}/proxy", :proxy=>:invalid) {}
|
||
URI.open("#{url}/proxy", :proxy=>:invalid) {}
|
||
}
|
||
assert_equal("", proxy_auth_log); proxy_auth_log.clear
|
||
with_env("http_proxy"=>proxy_url) {
|
||
# should not use proxy for 127.0.0.0/8.
|
||
open("#{url}/proxy") {|f|
|
||
URI.open("#{url}/proxy") {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("proxy", f.read)
|
||
}
|
||
... | ... | |
begin
|
||
th = proxy.start
|
||
srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
|
||
exc = assert_raise(OpenURI::HTTPError) { open("#{url}/proxy", :proxy=>proxy_url) {} }
|
||
exc = assert_raise(OpenURI::HTTPError) { URI.open("#{url}/proxy", :proxy=>proxy_url) {} }
|
||
assert_equal("407", exc.io.status[0])
|
||
assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
|
||
ensure
|
||
... | ... | |
begin
|
||
th = proxy.start
|
||
srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
|
||
open("#{url}/proxy",
|
||
URI.open("#{url}/proxy",
|
||
:proxy_http_basic_authentication=>[proxy_url, "user", "pass"]) {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("proxy", f.read)
|
||
}
|
||
assert_match(/#{Regexp.quote url}/, proxy_auth_log); proxy_auth_log.clear
|
||
assert_raise(ArgumentError) {
|
||
open("#{url}/proxy",
|
||
URI.open("#{url}/proxy",
|
||
:proxy_http_basic_authentication=>[true, "user", "pass"]) {}
|
||
}
|
||
assert_equal("", proxy_auth_log); proxy_auth_log.clear
|
||
... | ... | |
begin
|
||
th = proxy.start
|
||
srv.mount_proc("/proxy", lambda { |req, res| res.body = "proxy" } )
|
||
open("#{url}/proxy", :proxy => proxy_url) {|f|
|
||
URI.open("#{url}/proxy", :proxy => proxy_url) {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("proxy", f.read)
|
||
}
|
||
... | ... | |
srv.mount_proc("/r1/") {|req, res| res.status = 301; res["location"] = "#{url}/r2"; res.body = "r1" }
|
||
srv.mount_proc("/r2/") {|req, res| res.body = "r2" }
|
||
srv.mount_proc("/to-file/") {|req, res| res.status = 301; res["location"] = "file:///foo" }
|
||
open("#{url}/r1/") {|f|
|
||
URI.open("#{url}/r1/") {|f|
|
||
assert_equal("#{url}/r2", f.base_uri.to_s)
|
||
assert_equal("r2", f.read)
|
||
}
|
||
assert_raise(OpenURI::HTTPRedirect) { open("#{url}/r1/", :redirect=>false) {} }
|
||
assert_raise(RuntimeError) { open("#{url}/to-file/") {} }
|
||
assert_raise(OpenURI::HTTPRedirect) { URI.open("#{url}/r1/", :redirect=>false) {} }
|
||
assert_raise(RuntimeError) { URI.open("#{url}/to-file/") {} }
|
||
}
|
||
end
|
||
... | ... | |
with_http {|srv, dr, url|
|
||
srv.mount_proc("/r1/") {|req, res| res.status = 301; res["location"] = "#{url}/r2"; res.body = "r1" }
|
||
srv.mount_proc("/r2/") {|req, res| res.status = 301; res["location"] = "#{url}/r1"; res.body = "r2" }
|
||
assert_raise(RuntimeError) { open("#{url}/r1/") {} }
|
||
assert_raise(RuntimeError) { URI.open("#{url}/r1/") {} }
|
||
}
|
||
end
|
||
... | ... | |
def test_redirect_auth_success
|
||
with_http {|srv, dr, url|
|
||
setup_redirect_auth(srv, url)
|
||
open("#{url}/r2/", :http_basic_authentication=>['user', 'pass']) {|f|
|
||
URI.open("#{url}/r2/", :http_basic_authentication=>['user', 'pass']) {|f|
|
||
assert_equal("r2", f.read)
|
||
}
|
||
}
|
||
... | ... | |
}
|
||
with_http(log_tester) {|srv, dr, url, server_thread, server_log|
|
||
setup_redirect_auth(srv, url)
|
||
exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r2/") {} }
|
||
exc = assert_raise(OpenURI::HTTPError) { URI.open("#{url}/r2/") {} }
|
||
assert_equal("401", exc.io.status[0])
|
||
}
|
||
end
|
||
... | ... | |
}
|
||
with_http(log_tester) {|srv, dr, url, server_thread, server_log|
|
||
setup_redirect_auth(srv, url)
|
||
exc = assert_raise(OpenURI::HTTPError) { open("#{url}/r1/", :http_basic_authentication=>['user', 'pass']) {} }
|
||
exc = assert_raise(OpenURI::HTTPError) { URI.open("#{url}/r1/", :http_basic_authentication=>['user', 'pass']) {} }
|
||
assert_equal("401", exc.io.status[0])
|
||
}
|
||
end
|
||
def test_userinfo
|
||
assert_raise(ArgumentError) { open("http://user:pass@127.0.0.1/") {} }
|
||
assert_raise(ArgumentError) { URI.open("http://user:pass@127.0.0.1/") {} }
|
||
end
|
||
def test_progress
|
||
... | ... | |
srv.mount_proc("/data/") {|req, res| res.body = content }
|
||
length = []
|
||
progress = []
|
||
open("#{url}/data/",
|
||
URI.open("#{url}/data/",
|
||
:content_length_proc => lambda {|n| length << n },
|
||
:progress_proc => lambda {|n| progress << n }
|
||
) {|f|
|
||
... | ... | |
srv.mount_proc("/data/") {|req, res| res.body = content; res.chunked = true }
|
||
length = []
|
||
progress = []
|
||
open("#{url}/data/",
|
||
URI.open("#{url}/data/",
|
||
:content_length_proc => lambda {|n| length << n },
|
||
:progress_proc => lambda {|n| progress << n }
|
||
) {|f|
|
||
... | ... | |
srv.mount_proc("/u8/") {|req, res| res.body = content_u8; res['content-type'] = 'text/plain; charset=utf-8' }
|
||
srv.mount_proc("/ej/") {|req, res| res.body = content_ej; res['content-type'] = 'TEXT/PLAIN; charset=EUC-JP' }
|
||
srv.mount_proc("/nc/") {|req, res| res.body = "aa"; res['content-type'] = 'Text/Plain' }
|
||
open("#{url}/u8/") {|f|
|
||
URI.open("#{url}/u8/") {|f|
|
||
assert_equal(content_u8, f.read)
|
||
assert_equal("text/plain", f.content_type)
|
||
assert_equal("utf-8", f.charset)
|
||
}
|
||
open("#{url}/ej/") {|f|
|
||
URI.open("#{url}/ej/") {|f|
|
||
assert_equal(content_ej, f.read)
|
||
assert_equal("text/plain", f.content_type)
|
||
assert_equal("euc-jp", f.charset)
|
||
assert_equal(Encoding::EUC_JP, f.read.encoding)
|
||
}
|
||
open("#{url}/ej/", 'r:utf-8') {|f|
|
||
URI.open("#{url}/ej/", 'r:utf-8') {|f|
|
||
# override charset with encoding option
|
||
assert_equal(content_ej.dup.force_encoding('utf-8'), f.read)
|
||
assert_equal("text/plain", f.content_type)
|
||
assert_equal("euc-jp", f.charset)
|
||
assert_equal(Encoding::UTF_8, f.read.encoding)
|
||
}
|
||
open("#{url}/ej/", :encoding=>'utf-8') {|f|
|
||
URI.open("#{url}/ej/", :encoding=>'utf-8') {|f|
|
||
# override charset with encoding option
|
||
assert_equal(content_ej.dup.force_encoding('utf-8'), f.read)
|
||
assert_equal("text/plain", f.content_type)
|
||
... | ... | |
assert_equal(Encoding::UTF_8, f.read.encoding)
|
||
}
|
||
assert_raise(ArgumentError) {
|
||
open("#{url}/ej/", 'r:utf-8', :encoding=>'utf-8') {|f| }
|
||
URI.open("#{url}/ej/", 'r:utf-8', :encoding=>'utf-8') {|f| }
|
||
}
|
||
open("#{url}/nc/") {|f|
|
||
URI.open("#{url}/nc/") {|f|
|
||
assert_equal("aa", f.read)
|
||
assert_equal("text/plain", f.content_type)
|
||
assert_equal("iso-8859-1", f.charset)
|
||
... | ... | |
with_http {|srv, dr, url|
|
||
content_u8 = "\u3042"
|
||
srv.mount_proc("/qu8/") {|req, res| res.body = content_u8; res['content-type'] = 'text/plain; charset="utf\-8"' }
|
||
open("#{url}/qu8/") {|f|
|
||
URI.open("#{url}/qu8/") {|f|
|
||
assert_equal(content_u8, f.read)
|
||
assert_equal("text/plain", f.content_type)
|
||
assert_equal("utf-8", f.charset)
|
||
... | ... | |
def test_last_modified
|
||
with_http {|srv, dr, url|
|
||
srv.mount_proc("/data/") {|req, res| res.body = "foo"; res['last-modified'] = 'Fri, 07 Aug 2009 06:05:04 GMT' }
|
||
open("#{url}/data/") {|f|
|
||
URI.open("#{url}/data/") {|f|
|
||
assert_equal("foo", f.read)
|
||
assert_equal(Time.utc(2009,8,7,6,5,4), f.last_modified)
|
||
}
|
||
... | ... | |
srv.mount_proc("/data/") {|req, res| res.body = content_gz; res['content-encoding'] = 'gzip' }
|
||
srv.mount_proc("/data2/") {|req, res| res.body = content_gz; res['content-encoding'] = 'gzip'; res.chunked = true }
|
||
srv.mount_proc("/noce/") {|req, res| res.body = content_gz }
|
||
open("#{url}/data/") {|f|
|
||
URI.open("#{url}/data/") {|f|
|
||
assert_equal [], f.content_encoding
|
||
assert_equal(content, f.read)
|
||
}
|
||
open("#{url}/data2/") {|f|
|
||
URI.open("#{url}/data2/") {|f|
|
||
assert_equal [], f.content_encoding
|
||
assert_equal(content, f.read)
|
||
}
|
||
open("#{url}/noce/") {|f|
|
||
URI.open("#{url}/noce/") {|f|
|
||
assert_equal [], f.content_encoding
|
||
assert_equal(content_gz, f.read.force_encoding("ascii-8bit"))
|
||
}
|
||
... | ... | |
res.cookies << "name2=value2; blabla"
|
||
res.body = "foo"
|
||
}
|
||
open("#{url}/mcookie/") {|f|
|
||
URI.open("#{url}/mcookie/") {|f|
|
||
assert_equal("foo", f.read)
|
||
assert_equal(["name1=value1; blabla", "name2=value2; blabla"],
|
||
f.metas['set-cookie'].sort)
|
test/open-uri/test_ssl.rb | ||
---|---|---|
def setup_validation(srv, dr)
|
||
cacert_filename = "#{dr}/cacert.pem"
|
||
open(cacert_filename, "w") {|f| f << CA_CERT }
|
||
URI.open(cacert_filename, "w") {|f| f << CA_CERT }
|
||
srv.mount_proc("/data", lambda { |req, res| res.body = "ddd" } )
|
||
cacert_filename
|
||
end
|
||
... | ... | |
def test_validation_success
|
||
with_https {|srv, dr, url|
|
||
cacert_filename = setup_validation(srv, dr)
|
||
open("#{url}/data", :ssl_ca_cert => cacert_filename) {|f|
|
||
URI.open("#{url}/data", :ssl_ca_cert => cacert_filename) {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("ddd", f.read)
|
||
}
|
||
... | ... | |
def test_validation_noverify
|
||
with_https {|srv, dr, url|
|
||
setup_validation(srv, dr)
|
||
open("#{url}/data", :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE) {|f|
|
||
URI.open("#{url}/data", :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE) {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("ddd", f.read)
|
||
}
|
||
... | ... | |
end
|
||
with_https(log_tester) {|srv, dr, url, server_thread, server_log|
|
||
setup_validation(srv, dr)
|
||
assert_raise(OpenSSL::SSL::SSLError) { open("#{url}/data") {} }
|
||
assert_raise(OpenSSL::SSL::SSLError) { URI.open("#{url}/data") {} }
|
||
}
|
||
end
|
||
... | ... | |
}
|
||
with_https_proxy(proxy_log_tester) {|srv, dr, url_, cacert_filename, cacert_directory, proxy_host, proxy_port|
|
||
url = url_
|
||
open("#{url}/proxy", :proxy=>"http://#{proxy_host}:#{proxy_port}/", :ssl_ca_cert => cacert_filename) {|f|
|
||
URI.open("#{url}/proxy", :proxy=>"http://#{proxy_host}:#{proxy_port}/", :ssl_ca_cert => cacert_filename) {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("proxy", f.read)
|
||
}
|
||
... | ... | |
}
|
||
with_https_proxy(proxy_log_tester) {|srv, dr, url_, cacert_filename, cacert_directory, proxy_host, proxy_port|
|
||
url = url_
|
||
open("#{url}/proxy", :proxy=>"http://#{proxy_host}:#{proxy_port}/", :ssl_ca_cert => cacert_directory) {|f|
|
||
URI.open("#{url}/proxy", :proxy=>"http://#{proxy_host}:#{proxy_port}/", :ssl_ca_cert => cacert_directory) {|f|
|
||
assert_equal("200", f.status[0])
|
||
assert_equal("proxy", f.read)
|
||
}
|