From 496a1cff503e7e3a900b399e180e2a048631fbe6 Mon Sep 17 00:00:00 2001 From: Martin Laine Date: Thu, 5 Oct 2023 12:17:04 +0100 Subject: [PATCH] Configure connection after setting adapter Setting the adapter after calling the configure_connection blocks makes it impossible to configure the adapter with options. --- lib/twilio-ruby/http/http_client.rb | 2 +- spec/http/http_client_spec.rb | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/twilio-ruby/http/http_client.rb b/lib/twilio-ruby/http/http_client.rb index 99f3c7cc8..c178d3a07 100644 --- a/lib/twilio-ruby/http/http_client.rb +++ b/lib/twilio-ruby/http/http_client.rb @@ -40,8 +40,8 @@ def _request(request) # rubocop:disable Metrics/MethodLength f.options.open_timeout = request.timeout || @timeout f.options.timeout = request.timeout || @timeout - @configure_connection_blocks.each { |block| block.call(f) } f.adapter @adapter + @configure_connection_blocks.each { |block| block.call(f) } end @last_request = request diff --git a/spec/http/http_client_spec.rb b/spec/http/http_client_spec.rb index 27d290bec..8f9c57cf2 100644 --- a/spec/http/http_client_spec.rb +++ b/spec/http/http_client_spec.rb @@ -20,8 +20,8 @@ blocks_spy.second_block_called(f) end - expect(Faraday).to receive(:new).and_yield(@connection).and_return(@connection) - allow_any_instance_of(Faraday::Connection).to receive(:send).and_return(double('response', status: 301, body: {}, headers: {})) + allow(Faraday).to receive(:new).and_yield(@connection).and_return(@connection) + allow(@connection).to receive(:send).and_return(double('response', status: 301, body: {}, headers: {})) @client.request('host', 'port', 'GET', 'url', nil, nil, {}, ['a', 'b']) @@ -29,6 +29,25 @@ expect(blocks_spy).to have_received(:second_block_called).with(@connection) end + it 'should allow the configuration block to set the connection adapter' do + @client = Twilio::HTTP::Client.new + @connection = Faraday::Connection.new + + stub_const('TestAdapter', Class.new(Faraday::Adapter)) + Faraday::Adapter.register_middleware test_adapter: TestAdapter + + @client.configure_connection do |f| + f.adapter :test_adapter + end + + allow(Faraday).to receive(:new).and_yield(@connection).and_return(@connection) + allow(@connection).to receive(:send).and_return(double('response', status: 301, body: {}, headers: {})) + + @client.request('host', 'port', 'GET', 'url', nil, nil, {}, ['a', 'b']) + + expect(@connection.adapter).to eq TestAdapter + end + it 'should allow setting a global timeout' do @client = Twilio::HTTP::Client.new(timeout: 10) @connection = Faraday::Connection.new