Skip to content

Commit

Permalink
Refactor Twitter::REST::Client and OAuth specs
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jan 30, 2014
1 parent 02b6cb3 commit 88d7b19
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
18 changes: 8 additions & 10 deletions lib/twitter/rest/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ def middleware

# Perform an HTTP GET request
def get(path, params = {})
request(:get, path, params)
headers = request_headers(:get, path, params)
request(:get, path, params, headers)
end

# Perform an HTTP POST request
def post(path, params = {})
signature_params = params.values.any? { |value| value.respond_to?(:to_io) } ? {} : params
request(:post, path, params, signature_params)
headers = params.values.any? { |value| value.respond_to?(:to_io) } ? request_headers(:post, path, params, {}) : request_headers(:post, path, params)
request(:post, path, params, headers)
end

# @return [Boolean]
Expand All @@ -87,11 +88,8 @@ def connection
@connection ||= Faraday.new(ENDPOINT, connection_options)
end

def request(method, path, params = {}, signature_params = params)
response = connection.send(method.to_sym, path, params) do |request|
request.headers.update(request_headers(method, path, params, signature_params))
end
response.env
def request(method, path, params = {}, headers = {})
connection.send(method.to_sym, path, params) { |request| request.headers.update(headers) }.env
rescue Faraday::Error::TimeoutError, Timeout::Error => error
raise(Twitter::Error::RequestTimeout.new(error))
rescue Faraday::Error::ClientError, JSON::ParserError => error
Expand All @@ -106,12 +104,12 @@ def request_headers(method, path, params = {}, signature_params = params)
headers[:authorization] = bearer_token_credentials_auth_header
headers[:content_type] = 'application/x-www-form-urlencoded; charset=UTF-8'
else
headers[:authorization] = auth_token(method, path, params, signature_params)
headers[:authorization] = auth_header(method, path, params, signature_params)
end
headers
end

def auth_token(method, path, params = {}, signature_params = params)
def auth_header(method, path, params = {}, signature_params = params)
if !user_token?
@bearer_token = token unless bearer_token?
bearer_auth_header
Expand Down
10 changes: 3 additions & 7 deletions spec/twitter/rest/oauth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@

describe '#token' do
before do
# WebMock treats Basic Auth differently so we have to chack against the full URL with credentials.
# Faraday treats Basic Auth differently so we have to use the full URL with credentials
@oauth2_token_url = 'https://CK:CS@api.twitter.com/oauth2/token'
stub_request(:post, @oauth2_token_url).with(:body => 'grant_type=client_credentials').to_return(:body => fixture('bearer_token.json'), :headers => {:content_type => 'application/json; charset=utf-8'})
stub_request(:post, @oauth2_token_url).with(:body => {'grant_type' => 'client_credentials'}).to_return(:body => fixture('bearer_token.json'), :headers => {:content_type => 'application/json; charset=utf-8'})
end
it 'requests the correct resource' do
@client.token
expect(a_request(:post, @oauth2_token_url).with(:body => {:grant_type => 'client_credentials'})).to have_been_made
end
it 'requests with the correct headers' do
@client.token
expect(a_request(:post, @oauth2_token_url).with(:headers => {:content_type => 'application/x-www-form-urlencoded; charset=UTF-8', :accept => '*/*'})).to have_been_made
expect(a_request(:post, @oauth2_token_url).with(:body => {:grant_type => 'client_credentials'}, :headers => {:content_type => 'application/x-www-form-urlencoded; charset=UTF-8', :accept => '*/*'})).to have_been_made
end
it 'returns the bearer token' do
bearer_token = @client.token
Expand Down

0 comments on commit 88d7b19

Please sign in to comment.