Permalink
Browse files

handle default connection options set with hash (#629)

* handle default connection options set with hash
  • Loading branch information...
1 parent d2f000b commit e66210bd9dca6ad4628d880a038381baa0bccf0b @Cohen-Carlisle Cohen-Carlisle committed with iMacTia Dec 5, 2016
Showing with 27 additions and 3 deletions.
  1. +5 −3 lib/faraday.rb
  2. +22 −0 test/connection_test.rb
View
@@ -34,9 +34,6 @@ class << self
# Faraday.get "https://faraday.com"
attr_writer :default_connection
- # Public: Sets the default options used when calling Faraday#new.
- attr_writer :default_connection_options
-
# Public: Initializes a new Faraday::Connection.
#
# url - The optional String base URL to use as a prefix for all
@@ -122,6 +119,11 @@ def self.default_connection_options
@default_connection_options ||= ConnectionOptions.new
end
+ # Public: Sets the default options used when calling Faraday#new.
+ def self.default_connection_options=(options)
+ @default_connection_options = ConnectionOptions.from(options)
+ end
+
unless const_defined? :Timer
require 'timeout'
Timer = Timeout
@@ -2,6 +2,10 @@
class TestConnection < Faraday::TestCase
+ def teardown
+ Faraday.default_connection_options = nil
+ end
+
def with_env(key, proxy)
old_value = ENV.fetch(key, false)
ENV[key] = proxy
@@ -428,6 +432,24 @@ def test_default_connection_options_without_url
assert_equal 10, conn.options.timeout
end
+ def test_default_connection_options_as_hash
+ Faraday.default_connection_options = { request: { timeout: 10 } }
+ conn = Faraday.new 'http://sushi.com/foo'
+ assert_equal 10, conn.options.timeout
+ end
+
+ def test_default_connection_options_as_hash_without_url
+ Faraday.default_connection_options = { request: { timeout: 10 } }
+ conn = Faraday.new :url => 'http://sushi.com/foo'
+ assert_equal 10, conn.options.timeout
+ end
+
+ def test_default_connection_options_as_hash_with_instance_connection_options
+ Faraday.default_connection_options = { request: { timeout: 10 } }
+ conn = Faraday.new 'http://sushi.com/foo', request: { open_timeout: 1 }
+ assert_equal 1, conn.options.open_timeout
+ end
+
def env_url(url, params)
conn = Faraday::Connection.new(url, :params => params)
yield(conn) if block_given?

0 comments on commit e66210b

Please sign in to comment.