1 # Copyright 2010 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 require 'signet/oauth_1/client'
19 require 'google/api_client'
20 require 'google/api_client/version'
22 shared_examples_for 'configurable user agent' do
23 include ConnectionHelpers
25 it 'should allow the user agent to be modified' do
26 client.user_agent = 'Custom User Agent/1.2.3'
27 client.user_agent.should == 'Custom User Agent/1.2.3'
30 it 'should allow the user agent to be set to nil' do
31 client.user_agent = nil
32 client.user_agent.should == nil
35 it 'should not allow the user agent to be used with bogus values' do
37 client.user_agent = 42
38 client.execute(:uri=>'https://www.google.com/')
39 end).should raise_error(TypeError)
42 it 'should transmit a User-Agent header when sending requests' do
43 client.user_agent = 'Custom User Agent/1.2.3'
45 conn = stub_connection do |stub|
46 stub.get('/') do |env|
47 headers = env[:request_headers]
48 headers.should have_key('User-Agent')
49 headers['User-Agent'].should == client.user_agent
53 client.execute(:uri=>'https://www.google.com/', :connection => conn)
58 describe Google::APIClient do
59 include ConnectionHelpers
61 let(:client) { Google::APIClient.new(:application_name => 'API Client Tests') }
63 it 'should make its version number available' do
64 Google::APIClient::VERSION::STRING.should be_instance_of(String)
67 it 'should default to OAuth 2' do
68 Signet::OAuth2::Client.should === client.authorization
71 describe 'configure for no authentication' do
73 client.authorization = nil
75 it_should_behave_like 'configurable user agent'
78 describe 'configured for OAuth 1' do
80 client.authorization = :oauth_1
81 client.authorization.token_credential_key = 'abc'
82 client.authorization.token_credential_secret = '123'
85 it 'should use the default OAuth1 client configuration' do
86 client.authorization.temporary_credential_uri.to_s.should ==
87 'https://www.google.com/accounts/OAuthGetRequestToken'
88 client.authorization.authorization_uri.to_s.should include(
89 'https://www.google.com/accounts/OAuthAuthorizeToken'
91 client.authorization.token_credential_uri.to_s.should ==
92 'https://www.google.com/accounts/OAuthGetAccessToken'
93 client.authorization.client_credential_key.should == 'anonymous'
94 client.authorization.client_credential_secret.should == 'anonymous'
97 it_should_behave_like 'configurable user agent'
100 describe 'configured for OAuth 2' do
102 client.authorization = :oauth_2
103 client.authorization.access_token = '12345'
107 it_should_behave_like 'configurable user agent'
110 describe 'when executing requests' do
112 @prediction = client.discovered_api('prediction', 'v1.2')
113 client.authorization = :oauth_2
114 @connection = stub_connection do |stub|
115 stub.post('/prediction/v1.2/training?data=12345') do |env|
116 env[:request_headers]['Authorization'].should == 'Bearer 12345'
126 it 'should use default authorization' do
127 client.authorization.access_token = "12345"
129 :api_method => @prediction.training.insert,
130 :parameters => {'data' => '12345'},
131 :connection => @connection
135 it 'should use request scoped authorization when provided' do
136 client.authorization.access_token = "abcdef"
137 new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
139 :api_method => @prediction.training.insert,
140 :parameters => {'data' => '12345'},
141 :authorization => new_auth,
142 :connection => @connection
146 it 'should accept options with batch/request style execute' do
147 client.authorization.access_token = "abcdef"
148 new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
149 request = client.generate_request(
150 :api_method => @prediction.training.insert,
151 :parameters => {'data' => '12345'}
155 :authorization => new_auth,
156 :connection => @connection
161 it 'should accept options in array style execute' do
162 client.authorization.access_token = "abcdef"
163 new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
165 @prediction.training.insert, {'data' => '12345'}, '', {},
166 { :authorization => new_auth, :connection => @connection }
171 describe 'when retiries enabled' do
180 it 'should follow redirects' do
181 client.authorization = nil
182 @connection = stub_connection do |stub|
183 stub.get('/foo') do |env|
184 [302, {'location' => 'https://www.google.com/bar'}, '{}']
186 stub.get('/bar') do |env|
192 :uri => 'https://www.gogole.com/foo',
193 :connection => @connection
197 it 'should refresh tokens on 401 tokens' do
198 client.authorization.access_token = '12345'
199 expect(client.authorization).to receive(:fetch_access_token!)
201 @connection = stub_connection do |stub|
202 stub.get('/foo') do |env|
205 stub.get('/foo') do |env|
211 :uri => 'https://www.gogole.com/foo',
212 :connection => @connection
216 it 'should retry on 500 errors' do
217 client.authorization = nil
219 @connection = stub_connection do |stub|
220 stub.get('/foo') do |env|
223 stub.get('/foo') do |env|
229 :uri => 'https://www.gogole.com/foo',
230 :connection => @connection
231 ).status.should == 200
235 it 'should fail after max retries' do
236 client.authorization = nil
238 @connection = stub_connection do |stub|
239 stub.get('/foo') do |env|
246 :uri => 'https://www.gogole.com/foo',
247 :connection => @connection
248 ).status.should == 500