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 expect(client.user_agent).to eq('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 expect(client.user_agent).to eq(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).to 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 expect(headers).to have_key('User-Agent')
49 expect(headers['User-Agent']).to eq(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 expect(Google::APIClient::VERSION::STRING).to be_instance_of(String)
67 it 'should default to OAuth 2' do
68 expect(Signet::OAuth2::Client).to be === 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 expect(client.authorization.temporary_credential_uri.to_s).to eq(
87 'https://www.google.com/accounts/OAuthGetRequestToken'
89 expect(client.authorization.authorization_uri.to_s).to include(
90 'https://www.google.com/accounts/OAuthAuthorizeToken'
92 expect(client.authorization.token_credential_uri.to_s).to eq(
93 'https://www.google.com/accounts/OAuthGetAccessToken'
95 expect(client.authorization.client_credential_key).to eq('anonymous')
96 expect(client.authorization.client_credential_secret).to eq('anonymous')
99 it_should_behave_like 'configurable user agent'
102 describe 'configured for OAuth 2' do
104 client.authorization = :oauth_2
105 client.authorization.access_token = '12345'
109 it_should_behave_like 'configurable user agent'
112 describe 'when executing requests' do
114 @prediction = client.discovered_api('prediction', 'v1.2')
115 client.authorization = :oauth_2
116 @connection = stub_connection do |stub|
117 stub.post('/prediction/v1.2/training?data=12345') do |env|
118 expect(env[:request_headers]['Authorization']).to eq('Bearer 12345')
128 it 'should use default authorization' do
129 client.authorization.access_token = "12345"
131 :api_method => @prediction.training.insert,
132 :parameters => {'data' => '12345'},
133 :connection => @connection
137 it 'should use request scoped authorization when provided' do
138 client.authorization.access_token = "abcdef"
139 new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
141 :api_method => @prediction.training.insert,
142 :parameters => {'data' => '12345'},
143 :authorization => new_auth,
144 :connection => @connection
148 it 'should accept options with batch/request style execute' do
149 client.authorization.access_token = "abcdef"
150 new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
151 request = client.generate_request(
152 :api_method => @prediction.training.insert,
153 :parameters => {'data' => '12345'}
157 :authorization => new_auth,
158 :connection => @connection
163 it 'should accept options in array style execute' do
164 client.authorization.access_token = "abcdef"
165 new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
167 @prediction.training.insert, {'data' => '12345'}, '', {},
168 { :authorization => new_auth, :connection => @connection }
173 describe 'when retries enabled' do
182 it 'should follow redirects' do
183 client.authorization = nil
184 @connection = stub_connection do |stub|
185 stub.get('/foo') do |env|
186 [302, {'location' => 'https://www.google.com/bar'}, '{}']
188 stub.get('/bar') do |env|
194 :uri => 'https://www.gogole.com/foo',
195 :connection => @connection
199 it 'should refresh tokens on 401 tokens' do
200 client.authorization.access_token = '12345'
201 expect(client.authorization).to receive(:fetch_access_token!)
203 @connection = stub_connection do |stub|
204 stub.get('/foo') do |env|
207 stub.get('/foo') do |env|
213 :uri => 'https://www.gogole.com/foo',
214 :connection => @connection
219 it 'should not attempt multiple token refreshes' do
220 client.authorization.access_token = '12345'
221 expect(client.authorization).to receive(:fetch_access_token!).once
223 @connection = stub_connection do |stub|
224 stub.get('/foo') do |env|
230 :uri => 'https://www.gogole.com/foo',
231 :connection => @connection
235 it 'should not retry on client errors' do
237 @connection = stub_connection do |stub|
238 stub.get('/foo') do |env|
239 expect(count).to eq(0)
246 :uri => 'https://www.gogole.com/foo',
247 :connection => @connection,
248 :authenticated => false
252 it 'should retry on 500 errors' do
253 client.authorization = nil
255 @connection = stub_connection do |stub|
256 stub.get('/foo') do |env|
259 stub.get('/foo') do |env|
264 expect(client.execute(
265 :uri => 'https://www.gogole.com/foo',
266 :connection => @connection
271 it 'should fail after max retries' do
272 client.authorization = nil
274 @connection = stub_connection do |stub|
275 stub.get('/foo') do |env|
281 expect(client.execute(
282 :uri => 'https://www.gogole.com/foo',
283 :connection => @connection
285 expect(count).to eq(3)