Skip to content

Commit

Permalink
Configure connection after setting adapter
Browse files Browse the repository at this point in the history
Setting the adapter after calling the configure_connection blocks makes
it impossible to configure the adapter with options.
  • Loading branch information
doryphores committed Nov 15, 2023
1 parent ff04478 commit a821e31
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/twilio-ruby/http/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 21 additions & 2 deletions spec/http/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,34 @@
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'])

expect(blocks_spy).to have_received(:first_block_called).with(@connection)
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
Expand Down

0 comments on commit a821e31

Please sign in to comment.