2010-08-19 23:21:45 +00:00
|
|
|
# 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'
|
|
|
|
|
2012-01-27 14:57:57 +00:00
|
|
|
require 'faraday'
|
|
|
|
require 'faraday/utils'
|
2010-09-13 21:54:43 +00:00
|
|
|
require 'signet/oauth_1/client'
|
2010-08-19 23:21:45 +00:00
|
|
|
require 'google/api_client'
|
|
|
|
require 'google/api_client/version'
|
|
|
|
|
2011-01-05 01:09:52 +00:00
|
|
|
shared_examples_for 'configurable user agent' do
|
2012-09-24 23:09:17 +00:00
|
|
|
include ConnectionHelpers
|
|
|
|
|
2011-01-05 01:09:52 +00:00
|
|
|
it 'should allow the user agent to be modified' do
|
2012-07-31 20:15:45 +00:00
|
|
|
client.user_agent = 'Custom User Agent/1.2.3'
|
|
|
|
client.user_agent.should == 'Custom User Agent/1.2.3'
|
2011-01-05 01:09:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should allow the user agent to be set to nil' do
|
2012-07-31 20:15:45 +00:00
|
|
|
client.user_agent = nil
|
|
|
|
client.user_agent.should == nil
|
2011-01-05 01:09:52 +00:00
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should not allow the user agent to be used with bogus values' do
|
2011-01-05 01:09:52 +00:00
|
|
|
(lambda do
|
2012-07-31 20:15:45 +00:00
|
|
|
client.user_agent = 42
|
2012-09-24 23:09:17 +00:00
|
|
|
client.execute(:uri=>'http://www.google.com/')
|
2011-01-05 01:09:52 +00:00
|
|
|
end).should raise_error(TypeError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should transmit a User-Agent header when sending requests' do
|
2012-07-31 20:15:45 +00:00
|
|
|
client.user_agent = 'Custom User Agent/1.2.3'
|
2012-09-05 21:56:52 +00:00
|
|
|
|
2012-09-24 23:09:17 +00:00
|
|
|
conn = stub_connection do |stub|
|
2012-01-27 14:57:57 +00:00
|
|
|
stub.get('/') do |env|
|
|
|
|
headers = env[:request_headers]
|
|
|
|
headers.should have_key('User-Agent')
|
2012-07-31 20:15:45 +00:00
|
|
|
headers['User-Agent'].should == client.user_agent
|
2012-01-27 14:57:57 +00:00
|
|
|
[200, {}, ['']]
|
2011-01-05 01:09:52 +00:00
|
|
|
end
|
|
|
|
end
|
2012-09-24 23:09:17 +00:00
|
|
|
client.execute(:uri=>'http://www.google.com/', :connection => conn)
|
|
|
|
conn.verify
|
2011-01-05 01:09:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
describe Google::APIClient do
|
2012-09-24 23:09:17 +00:00
|
|
|
include ConnectionHelpers
|
|
|
|
|
2012-12-30 19:27:45 +00:00
|
|
|
let(:client) { Google::APIClient.new(:application_name => 'API Client Tests') }
|
2010-08-19 23:21:45 +00:00
|
|
|
|
2010-10-12 20:39:09 +00:00
|
|
|
it 'should make its version number available' do
|
2011-07-29 22:07:04 +00:00
|
|
|
Google::APIClient::VERSION::STRING.should be_instance_of(String)
|
2010-10-12 20:39:09 +00:00
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should default to OAuth 2' do
|
2012-07-31 20:15:45 +00:00
|
|
|
Signet::OAuth2::Client.should === client.authorization
|
2010-10-12 20:39:09 +00:00
|
|
|
end
|
2011-01-05 01:09:52 +00:00
|
|
|
|
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
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
describe 'configured for OAuth 1' do
|
|
|
|
before do
|
2012-07-31 20:15:45 +00:00
|
|
|
client.authorization = :oauth_1
|
2012-09-24 23:09:17 +00:00
|
|
|
client.authorization.token_credential_key = 'abc'
|
|
|
|
client.authorization.token_credential_secret = '123'
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
2010-10-12 20:39:09 +00:00
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should use the default OAuth1 client configuration' do
|
2012-07-31 20:15:45 +00:00
|
|
|
client.authorization.temporary_credential_uri.to_s.should ==
|
2011-05-04 11:44:35 +00:00
|
|
|
'https://www.google.com/accounts/OAuthGetRequestToken'
|
2012-07-31 20:15:45 +00:00
|
|
|
client.authorization.authorization_uri.to_s.should include(
|
2011-05-04 11:44:35 +00:00
|
|
|
'https://www.google.com/accounts/OAuthAuthorizeToken'
|
|
|
|
)
|
2012-07-31 20:15:45 +00:00
|
|
|
client.authorization.token_credential_uri.to_s.should ==
|
2011-05-04 11:44:35 +00:00
|
|
|
'https://www.google.com/accounts/OAuthGetAccessToken'
|
2012-07-31 20:15:45 +00:00
|
|
|
client.authorization.client_credential_key.should == 'anonymous'
|
|
|
|
client.authorization.client_credential_secret.should == 'anonymous'
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
2010-08-19 23:21:45 +00:00
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it_should_behave_like 'configurable user agent'
|
2010-08-19 23:21:45 +00:00
|
|
|
end
|
2010-09-13 21:54:43 +00:00
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
describe 'configured for OAuth 2' do
|
|
|
|
before do
|
2012-07-31 20:15:45 +00:00
|
|
|
client.authorization = :oauth_2
|
2012-09-24 23:09:17 +00:00
|
|
|
client.authorization.access_token = '12345'
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# TODO
|
|
|
|
it_should_behave_like 'configurable user agent'
|
2010-09-13 21:54:43 +00:00
|
|
|
end
|
2012-06-07 00:27:20 +00:00
|
|
|
|
|
|
|
describe 'when executing requests' do
|
|
|
|
before do
|
2012-10-04 22:46:48 +00:00
|
|
|
@prediction = client.discovered_api('prediction', 'v1.2')
|
2012-07-31 20:15:45 +00:00
|
|
|
client.authorization = :oauth_2
|
2012-09-24 23:09:17 +00:00
|
|
|
@connection = stub_connection do |stub|
|
2012-10-04 22:46:48 +00:00
|
|
|
stub.post('/prediction/v1.2/training?data=12345') do |env|
|
2012-09-24 23:09:17 +00:00
|
|
|
env[:request_headers]['Authorization'].should == 'Bearer 12345'
|
2012-06-07 00:27:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-10-04 22:46:48 +00:00
|
|
|
|
|
|
|
after do
|
|
|
|
@connection.verify
|
|
|
|
end
|
|
|
|
|
2012-06-07 00:27:20 +00:00
|
|
|
it 'should use default authorization' do
|
2012-07-31 20:15:45 +00:00
|
|
|
client.authorization.access_token = "12345"
|
2012-10-04 22:46:48 +00:00
|
|
|
client.execute(
|
|
|
|
:api_method => @prediction.training.insert,
|
|
|
|
:parameters => {'data' => '12345'},
|
|
|
|
:connection => @connection
|
|
|
|
)
|
2012-06-07 00:27:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should use request scoped authorization when provided' do
|
2012-07-31 20:15:45 +00:00
|
|
|
client.authorization.access_token = "abcdef"
|
2012-06-07 00:27:20 +00:00
|
|
|
new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
|
2012-10-04 22:46:48 +00:00
|
|
|
client.execute(
|
|
|
|
:api_method => @prediction.training.insert,
|
|
|
|
:parameters => {'data' => '12345'},
|
|
|
|
:authorization => new_auth,
|
|
|
|
:connection => @connection
|
|
|
|
)
|
2012-06-07 00:27:20 +00:00
|
|
|
end
|
2012-10-04 22:46:48 +00:00
|
|
|
|
2013-05-13 22:19:25 +00:00
|
|
|
it 'should accept options with batch/request style execute' do
|
|
|
|
client.authorization.access_token = "abcdef"
|
|
|
|
new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
|
|
|
|
request = client.generate_request(
|
|
|
|
:api_method => @prediction.training.insert,
|
|
|
|
:parameters => {'data' => '12345'}
|
|
|
|
)
|
|
|
|
client.execute(
|
|
|
|
request,
|
|
|
|
:authorization => new_auth,
|
|
|
|
:connection => @connection
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-10-04 22:46:48 +00:00
|
|
|
it 'should accept options in array style execute' do
|
|
|
|
client.authorization.access_token = "abcdef"
|
|
|
|
new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
|
|
|
|
client.execute(
|
|
|
|
@prediction.training.insert, {'data' => '12345'}, '', {},
|
|
|
|
{ :authorization => new_auth, :connection => @connection }
|
|
|
|
)
|
|
|
|
end
|
2012-06-07 00:27:20 +00:00
|
|
|
end
|
2010-08-19 23:21:45 +00:00
|
|
|
end
|