From f8b754ddd827d1513cb985c873a4e4572a21246e Mon Sep 17 00:00:00 2001 From: Christopher Schramm Date: Thu, 2 Oct 2014 10:15:10 +0200 Subject: [PATCH] Allow request body and headers --- lib/xing_api/client.rb | 10 +++++++-- spec/xing_api/client_spec.rb | 40 ++++++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/lib/xing_api/client.rb b/lib/xing_api/client.rb index 05b15bc..b985483 100644 --- a/lib/xing_api/client.rb +++ b/lib/xing_api/client.rb @@ -24,8 +24,14 @@ def initialize(options={}) end def request(http_verb, url, options={}) - full_url = url + hash_to_params(options) - handle(access_token.request(http_verb, full_url)) + legacy = options.keys.none? do |key| + [:params, :headers, :body].include? key + end + params = legacy && options || options[:params] || {} + full_url = url + hash_to_params(params) + arguments = options[:headers] && [options[:headers]] || [] + arguments.unshift options[:body] if [:post, :put].include? http_verb + handle(access_token.request(http_verb, full_url, *arguments)) end def get_request_token(oauth_callback='oob') diff --git a/spec/xing_api/client_spec.rb b/spec/xing_api/client_spec.rb index 6cc20d0..6b2e92e 100644 --- a/spec/xing_api/client_spec.rb +++ b/spec/xing_api/client_spec.rb @@ -48,10 +48,10 @@ describe '#request' do subject { described_class.new } - def set_expectaction(verb, url, body='{}') - response_stub = stub(:code => 200, :body => body) + def set_expectaction(*params) + response_stub = stub(:code => 200, :body => params.first.is_a?(String) && params.shift || '{}') token_stub = mock do - expects(:request).with(verb, url).returns(response_stub) + expects(:request).with(*params).returns(response_stub) end subject.stubs(:access_token).returns(token_stub) end @@ -68,8 +68,40 @@ def set_expectaction(verb, url, body='{}') subject.request(:get, '/v1/some_resource/123', :param => 'some text & more') end + it 'passes verb, url, and params as query_string to the access_token' do + set_expectaction(:get, '/v1/some_resource/123?param1=1¶m2=foobar') + + subject.request(:get, '/v1/some_resource/123', params: { param1: 1, param2: 'foobar' }) + end + + it 'passes :post verb, url, params, body, and headers to the access_token' do + set_expectaction(:post, '/v1/some_resource/123?param1=1¶m2=foobar', 'body', { header1: 'foobar', header2: 2 }) + + subject.request(:post, '/v1/some_resource/123', params: { param1: 1, param2: 'foobar' }, body: 'body', + headers: { header1: 'foobar', header2: 2 }) + end + + it 'passes :put verb, url, params, body, and headers to the access_token' do + set_expectaction(:put, '/v1/some_resource/123?param1=1¶m2=foobar', 'body', { header1: 'foobar', header2: 2 }) + + subject.request(:put, '/v1/some_resource/123', params: { param1: 1, param2: 'foobar' }, body: 'body', + headers: { header1: 'foobar', header2: 2 }) + end + + it 'passes verb, url, and headers to the access_token without requiring a body' do + set_expectaction(:post, '/v1/some_resource/123', nil, { header1: 'foobar', header2: 2 }) + + subject.request(:post, '/v1/some_resource/123', headers: { header1: 'foobar', header2: 2 }) + end + + it 'does not pass a body to the access_token for :get requests' do + set_expectaction(:get, '/v1/some_resource/123', { header1: 'foobar', header2: 2 }) + + subject.request(:get, '/v1/some_resource/123', headers: { header1: 'foobar', header2: 2 }) + end + it 'parses the response' do - set_expectaction(:get, '/v1/some_resource', '{"some": "content"}') + set_expectaction('{"some": "content"}', :get, '/v1/some_resource') expect(subject.request(:get, '/v1/some_resource')).to eql({:some => 'content'}) end