Skip to content

Commit

Permalink
handle default connection options set with hash (#629)
Browse files Browse the repository at this point in the history
* handle default connection options set with hash
  • Loading branch information
Cohen-Carlisle authored and iMacTia committed Dec 5, 2016
1 parent d2f000b commit e66210b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/faraday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions test/connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit e66210b

Please sign in to comment.