google-api-ruby-client/spec/google/api_client_spec.rb

138 lines
4.3 KiB
Ruby
Raw Normal View History

# Copyright 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'spec_helper'
require 'faraday'
require 'faraday/utils'
require 'signet/oauth_1/client'
require 'google/api_client'
require 'google/api_client/version'
shared_examples_for 'configurable user agent' do
2012-09-24 23:09:17 +00:00
include ConnectionHelpers
it 'should allow the user agent to be modified' do
client.user_agent = 'Custom User Agent/1.2.3'
client.user_agent.should == 'Custom User Agent/1.2.3'
end
it 'should allow the user agent to be set to nil' do
client.user_agent = nil
client.user_agent.should == nil
end
it 'should not allow the user agent to be used with bogus values' do
(lambda do
client.user_agent = 42
2012-09-24 23:09:17 +00:00
client.execute(:uri=>'http://www.google.com/')
end).should raise_error(TypeError)
end
it 'should transmit a User-Agent header when sending requests' do
client.user_agent = 'Custom User Agent/1.2.3'
2012-09-24 23:09:17 +00:00
conn = stub_connection do |stub|
stub.get('/') do |env|
headers = env[:request_headers]
headers.should have_key('User-Agent')
headers['User-Agent'].should == client.user_agent
[200, {}, ['']]
end
end
2012-09-24 23:09:17 +00:00
client.execute(:uri=>'http://www.google.com/', :connection => conn)
conn.verify
end
end
describe Google::APIClient do
2012-09-24 23:09:17 +00:00
include ConnectionHelpers
let(:client) { Google::APIClient.new }
it 'should make its version number available' do
Google::APIClient::VERSION::STRING.should be_instance_of(String)
end
it 'should default to OAuth 2' do
Signet::OAuth2::Client.should === client.authorization
end
2012-09-24 23:09:17 +00:00
describe 'configure for no authentication' do
before do
client.authorization = nil
end
it_should_behave_like 'configurable user agent'
end
describe 'configured for OAuth 1' do
before do
client.authorization = :oauth_1
2012-09-24 23:09:17 +00:00
client.authorization.token_credential_key = 'abc'
client.authorization.token_credential_secret = '123'
end
it 'should use the default OAuth1 client configuration' do
client.authorization.temporary_credential_uri.to_s.should ==
'https://www.google.com/accounts/OAuthGetRequestToken'
client.authorization.authorization_uri.to_s.should include(
'https://www.google.com/accounts/OAuthAuthorizeToken'
)
client.authorization.token_credential_uri.to_s.should ==
'https://www.google.com/accounts/OAuthGetAccessToken'
client.authorization.client_credential_key.should == 'anonymous'
client.authorization.client_credential_secret.should == 'anonymous'
end
it_should_behave_like 'configurable user agent'
end
describe 'configured for OAuth 2' do
before do
client.authorization = :oauth_2
2012-09-24 23:09:17 +00:00
client.authorization.access_token = '12345'
end
# TODO
it_should_behave_like 'configurable user agent'
end
2012-06-07 00:27:20 +00:00
describe 'when executing requests' do
before do
client.authorization = :oauth_2
2012-09-24 23:09:17 +00:00
@connection = stub_connection do |stub|
stub.get('/test') do |env|
env[:request_headers]['Authorization'].should == 'Bearer 12345'
2012-06-07 00:27:20 +00:00
end
end
end
it 'should use default authorization' do
client.authorization.access_token = "12345"
client.execute(:http_method => :get,
2012-06-07 00:27:20 +00:00
:uri => 'https://www.googleapis.com/test',
:connection => @connection)
end
it 'should use request scoped authorization when provided' do
client.authorization.access_token = "abcdef"
2012-06-07 00:27:20 +00:00
new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
client.execute(:http_method => :get,
2012-06-07 00:27:20 +00:00
:uri => 'https://www.googleapis.com/test',
:connection => @connection,
:authorization => new_auth)
end
end
end