diff --git a/lib/airborne/base.rb b/lib/airborne/base.rb index 7f900eb..726a105 100644 --- a/lib/airborne/base.rb +++ b/lib/airborne/base.rb @@ -29,32 +29,32 @@ def self.configuration RSpec.configuration end - def get(url, headers = nil) - @response = make_request(:get, url, headers: headers) + def get(url, headers = nil, advanced_options = {}) + @response = make_request(:get, url, headers: headers, advanced_options: advanced_options) end - def post(url, post_body = nil, headers = nil) - @response = make_request(:post, url, body: post_body, headers: headers) + def post(url, post_body = nil, headers = nil, advanced_options = {}) + @response = make_request(:post, url, body: post_body, headers: headers, advanced_options: advanced_options) end - def patch(url, patch_body = nil, headers = nil) - @response = make_request(:patch, url, body: patch_body, headers: headers) + def patch(url, patch_body = nil, headers = nil, advanced_options = {}) + @response = make_request(:patch, url, body: patch_body, headers: headers, advanced_options: advanced_options) end - def put(url, put_body = nil, headers = nil) - @response = make_request(:put, url, body: put_body, headers: headers) + def put(url, put_body = nil, headers = nil, advanced_options = {}) + @response = make_request(:put, url, body: put_body, headers: headers, advanced_options: advanced_options) end - def delete(url, delete_body = nil, headers = nil) - @response = make_request(:delete, url, body: delete_body, headers: headers) + def delete(url, delete_body = nil, headers = nil, advanced_options = {}) + @response = make_request(:delete, url, body: delete_body, headers: headers, advanced_options: advanced_options) end - def head(url, headers = nil) - @response = make_request(:head, url, headers: headers) + def head(url, headers = nil, advanced_options = {}) + @response = make_request(:head, url, headers: headers, advanced_options: advanced_options) end - def options(url, headers = nil) - @response = make_request(:options, url, headers: headers) + def options(url, headers = nil, advanced_options = {}) + @response = make_request(:options, url, headers: headers, advanced_options: advanced_options) end def response diff --git a/lib/airborne/rest_client_requester.rb b/lib/airborne/rest_client_requester.rb index 3ebe68f..282c3d4 100644 --- a/lib/airborne/rest_client_requester.rb +++ b/lib/airborne/rest_client_requester.rb @@ -3,23 +3,28 @@ module Airborne module RestClientRequester def make_request(method, url, options = {}) - headers = base_headers.merge(options[:headers] || {}) - res = if method == :post || method == :patch || method == :put - begin - request_body = options[:body].nil? ? '' : options[:body] - request_body = request_body.to_json if options[:body].is_a?(Hash) - RestClient.send(method, get_url(url), request_body, headers) - rescue RestClient::Exception => e - e.response - end - else - begin - RestClient.send(method, get_url(url), headers) - rescue RestClient::Exception => e - e.response + rest_client_params = { + method: method, + url: get_url(url), + headers: base_headers.merge(options[:headers] || {}) + } + rest_client_params.merge!(options[:advanced_options]) unless options[:advanced_options].nil? + + if method == :post || method == :patch || method == :put + rest_client_params[:payload] = if options[:body].nil? + '' + elsif options[:body].is_a?(Hash) + options[:body].to_json + else + options[:body] end end - res + + begin + RestClient::Request.execute(rest_client_params) + rescue RestClient::Exception => e + e.response + end end private diff --git a/spec/airborne/client_requester_spec.rb b/spec/airborne/client_requester_spec.rb index 3023801..ab1b9f2 100644 --- a/spec/airborne/client_requester_spec.rb +++ b/spec/airborne/client_requester_spec.rb @@ -2,27 +2,33 @@ describe 'client requester' do before do - allow(RestClient).to receive(:send) + allow(RestClient::Request).to receive(:execute) RSpec::Mocks.space.proxy_for(self).remove_stub_if_present(:get) end after do - allow(RestClient).to receive(:send).and_call_original + allow(RestClient::Request).to receive(:execute).and_call_original Airborne.configure { |config| config.headers = {} } end it 'should set :content_type to :json by default' do get '/foo' - expect(RestClient).to have_received(:send) - .with(:get, 'http://www.example.com/foo', { content_type: :json }) + expect(RestClient::Request).to have_received(:execute).with({ + method: :get, + url: 'http://www.example.com/foo', + headers: { content_type: :json } + }) end it 'should override headers with option[:headers]' do get '/foo', { content_type: 'application/x-www-form-urlencoded' } - expect(RestClient).to have_received(:send) - .with(:get, 'http://www.example.com/foo', { content_type: 'application/x-www-form-urlencoded' }) + expect(RestClient::Request).to have_received(:execute).with({ + method: :get, + url: 'http://www.example.com/foo', + headers: { content_type: 'application/x-www-form-urlencoded' } + }) end it 'should override headers with airborne config headers' do @@ -30,7 +36,21 @@ get '/foo' - expect(RestClient).to have_received(:send) - .with(:get, 'http://www.example.com/foo', { content_type: 'text/plain' }) + expect(RestClient::Request).to have_received(:execute).with({ + method: :get, + url: 'http://www.example.com/foo', + headers: { content_type: 'text/plain' } + }) + end + + it 'should set :timeout' do + get '/foo', nil, timeout: 120 + + expect(RestClient::Request).to have_received(:execute).with({ + method: :get, + url: 'http://www.example.com/foo', + headers: { content_type: :json }, + timeout: 120 + }) end end