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

added the ability to pass advanced options to the request (like timeout…) #4

Open
wants to merge 1 commit into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions lib/airborne/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 20 additions & 15 deletions lib/airborne/rest_client_requester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 28 additions & 8 deletions spec/airborne/client_requester_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,55 @@

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
Airborne.configure { |config| config.headers = { content_type: 'text/plain' } }

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