diff --git a/UPGRADING.md b/UPGRADING.md index bf362f6a1..8acd82401 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -87,6 +87,7 @@ For more details, see https://github.com/lostisland/faraday/pull/1306 * Drop `Faraday::UploadIO` in favour of `Faraday::FilePart`. * `Faraday.default_connection_options` will now be deep-merged into new connections to avoid overriding them (e.g. headers). * Retry middleware `retry_block` is not called if retry will not happen due to `max_interval`. (#1350) +* `Faraday::Builder#build` method is not exposed through `Faraday::Connection` anymore and does not reset the handlers if called multiple times. This method should be used internally only. ## Faraday 1.0 diff --git a/lib/faraday.rb b/lib/faraday.rb index 687d70a4d..4b1354087 100644 --- a/lib/faraday.rb +++ b/lib/faraday.rb @@ -150,5 +150,4 @@ def method_missing(name, *args, &block) self.ignore_env_proxy = false self.root_path = File.expand_path __dir__ self.lib_path = File.expand_path 'faraday', __dir__ - self.default_adapter = :test end diff --git a/lib/faraday/connection.rb b/lib/faraday/connection.rb index 20551a749..9dab543ca 100644 --- a/lib/faraday/connection.rb +++ b/lib/faraday/connection.rb @@ -117,7 +117,7 @@ def headers=(hash) extend Forwardable - def_delegators :builder, :build, :use, :request, :response, :adapter, :app + def_delegators :builder, :use, :request, :response, :adapter, :app # Closes the underlying resources and/or connections. In the case of # persistent connections, this closes all currently open connections diff --git a/lib/faraday/rack_builder.rb b/lib/faraday/rack_builder.rb index c000d51f9..aa433c10b 100644 --- a/lib/faraday/rack_builder.rb +++ b/lib/faraday/rack_builder.rb @@ -58,22 +58,21 @@ def build(app = nil) end end - def initialize(handlers = [], adapter = nil, &block) - @adapter = adapter - @handlers = handlers - if block - build(&block) - elsif @handlers.empty? - # default stack, if nothing else is configured - request :url_encoded - self.adapter Faraday.default_adapter - end + def initialize(&block) + @adapter = nil + @handlers = [] + build(&block) + end + + def initialize_dup(original) + super + @adapter = original.adapter + @handlers = original.handlers.dup end - def build(options = {}) + def build raise_if_locked - @handlers.clear unless options[:keep] - yield(self) if block_given? + block_given? ? yield(self) : request(:url_encoded) adapter(Faraday.default_adapter) unless @adapter end @@ -109,7 +108,7 @@ def locked? end ruby2_keywords def adapter(klass = NO_ARGUMENT, *args, &block) - return @adapter if klass == NO_ARGUMENT + return @adapter if klass == NO_ARGUMENT || klass.nil? klass = Faraday::Adapter.lookup_middleware(klass) if klass.is_a?(Symbol) @adapter = self.class::Handler.new(klass, *args, &block) @@ -164,6 +163,7 @@ def build_response(connection, request) def app @app ||= begin lock! + ensure_adapter! to_app end end @@ -182,10 +182,6 @@ def ==(other) @adapter == other.adapter end - def dup - self.class.new(@handlers.dup, @adapter.dup) - end - # ENV Keys # :http_method - a symbolized request HTTP method (:get, :post) # :body - the request body that will eventually be converted to a string. @@ -216,6 +212,9 @@ def build_env(connection, request) private LOCK_ERR = "can't modify middleware stack after making a request" + MISSING_ADAPTER_ERROR = "An attempt to run a request with a Faraday::Connection without adapter has been made.\n" \ + "Please set Faraday.default_adapter or provide one when initializing the connection.\n" \ + 'For more info, check https://lostisland.github.io/faraday/usage/.' def raise_if_locked raise StackLocked, LOCK_ERR if locked? @@ -227,6 +226,10 @@ def raise_if_adapter(klass) raise 'Adapter should be set using the `adapter` method, not `use`' end + def ensure_adapter! + raise MISSING_ADAPTER_ERROR unless @adapter + end + def adapter_set? !@adapter.nil? end diff --git a/spec/faraday/connection_spec.rb b/spec/faraday/connection_spec.rb index 7111a6a7d..fddcac2b9 100644 --- a/spec/faraday/connection_spec.rb +++ b/spec/faraday/connection_spec.rb @@ -151,6 +151,9 @@ def decode(params) end describe '#close' do + before { Faraday.default_adapter = :test } + after { Faraday.default_adapter = nil } + it 'can close underlying app' do expect(conn.app).to receive(:close) conn.close diff --git a/spec/faraday/rack_builder_spec.rb b/spec/faraday/rack_builder_spec.rb index b9a4ef957..6a0a44790 100644 --- a/spec/faraday/rack_builder_spec.rb +++ b/spec/faraday/rack_builder_spec.rb @@ -20,6 +20,8 @@ class Banana < Handler end subject { conn.builder } + before { Faraday.default_adapter = :test } + after { Faraday.default_adapter = nil } context 'with default stack' do let(:conn) { Faraday::Connection.new } @@ -86,13 +88,6 @@ class Banana < Handler it { expect(subject.handlers).to eq([Apple]) } - it 'allows rebuilding' do - subject.build do |builder| - builder.use(Orange) - end - expect(subject.handlers).to eq([Orange]) - end - it 'allows use' do subject.use(Orange) expect(subject.handlers).to eq([Apple, Orange])