From e66210bd9dca6ad4628d880a038381baa0bccf0b Mon Sep 17 00:00:00 2001 From: Cohen Carlisle Date: Mon, 5 Dec 2016 13:11:47 -0500 Subject: [PATCH] handle default connection options set with hash (#629) * handle default connection options set with hash --- lib/faraday.rb | 8 +++++--- test/connection_test.rb | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/faraday.rb b/lib/faraday.rb index 0a4d55a4e..090de302b 100644 --- a/lib/faraday.rb +++ b/lib/faraday.rb @@ -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 diff --git a/test/connection_test.rb b/test/connection_test.rb index 022fecc0d..bda7f6053 100644 --- a/test/connection_test.rb +++ b/test/connection_test.rb @@ -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?