Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow configuring Faraday adapter with configure_connection block #683

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/twilio-ruby/http/http_client.rb
Expand Up @@ -48,8 +48,8 @@ def _request(request) # rubocop:disable Metrics/MethodLength
f.options.timeout = request.timeout || @timeout
f.params = request.params.nil? ? {} : request.params

@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
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