summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-10-10 19:15:56 +0000
committernormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-10-10 19:15:56 +0000
commit9559b04e3dd6ee3afadb71a35956ccff4d8ef33e (patch)
treed06fd0bd57b372e758ac90426fd5e4bb25467121
parentadfce27ee0c3c31fbbfe9590e877f2298b809ae0 (diff)
open-uri: accept :open_timeout option
* lib/open-uri.rb (OpenURI::Options): add :open_timeout default * (def OpenURI.open_http): check :open_timeout option * (module OpenURI): rdoc for :open_timeout * test/open-uri/test_open-uri.rb (test_open_timeout): new test [Feature #10361] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47869 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog8
-rw-r--r--lib/open-uri.rb11
-rw-r--r--test/open-uri/test_open-uri.rb18
3 files changed, 37 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index ffac7d8492..4894a8b036 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Sat Oct 11 04:14:41 2014 Kir Shatrov <shatrov@me.com>
+
+ * lib/open-uri.rb (OpenURI::Options): add :open_timeout default
+ * (def OpenURI.open_http): check :open_timeout option
+ * (module OpenURI): rdoc for :open_timeout
+ * test/open-uri/test_open-uri.rb (test_open_timeout): new test
+ [Feature #10361]
+
Fri Oct 10 11:27:49 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
* include/ruby/io.h (rb_io_mode_flags, rb_io_modenum_flags):
diff --git a/lib/open-uri.rb b/lib/open-uri.rb
index b793828063..829759ab7c 100644
--- a/lib/open-uri.rb
+++ b/lib/open-uri.rb
@@ -102,6 +102,7 @@ module OpenURI
:content_length_proc => true,
:http_basic_authentication => true,
:read_timeout => true,
+ :open_timeout => true,
:ssl_ca_cert => nil,
:ssl_verify_mode => nil,
:ftp_active_mode => false,
@@ -307,6 +308,9 @@ module OpenURI
if options.include? :read_timeout
http.read_timeout = options[:read_timeout]
end
+ if options.include? :open_timeout
+ http.open_timeout = options[:open_timeout]
+ end
resp = nil
http.start {
@@ -667,6 +671,13 @@ module OpenURI
#
# :read_timeout option specifies a timeout of read for http connections.
#
+ # [:open_timeout]
+ # Synopsis:
+ # :open_timeout=>nil (no timeout)
+ # :open_timeout=>10 (10 second)
+ #
+ # :open_timeout option specifies a timeout of open for http connections.
+ #
# [:ssl_ca_cert]
# Synopsis:
# :ssl_ca_cert=>filename
diff --git a/test/open-uri/test_open-uri.rb b/test/open-uri/test_open-uri.rb
index 4073754bd7..51af8842d1 100644
--- a/test/open-uri/test_open-uri.rb
+++ b/test/open-uri/test_open-uri.rb
@@ -122,6 +122,24 @@ class TestOpenURI < Test::Unit::TestCase
}
end
+ def test_open_timeout
+ assert_raises(Net::OpenTimeout) do
+ URI("http://example.com/").read(open_timeout: 0.000001)
+ end if false # avoid external resources in tests
+
+ with_http {|srv, dr, url|
+ url += '/'
+ srv.mount_proc('/', lambda { |_, res| res.body = 'hi' })
+ begin
+ URI(url).read(open_timeout: 0.000001)
+ rescue Net::OpenTimeout
+ # not guaranteed to fire, since the kernel negotiates the
+ # TCP connection even if the server thread is sleeping
+ end
+ assert_equal 'hi', URI(url).read(open_timeout: 60), 'should not timeout'
+ }
+ end
+
def test_invalid_option
assert_raise(ArgumentError) { open("http://127.0.0.1/", :invalid_option=>true) {} }
end