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'
21 shared_examples_for 'configurable user agent' do
22 include ConnectionHelpers
24 it 'should allow the user agent to be modified' do
25 client.user_agent = 'Custom User Agent/1.2.3'
26 expect(client.user_agent).to eq('Custom User Agent/1.2.3')
29 it 'should allow the user agent to be set to nil' do
30 client.user_agent = nil
31 expect(client.user_agent).to eq(nil)
34 it 'should not allow the user agent to be used with bogus values' do
36 client.user_agent = 42
37 client.execute(:uri=>'https://www.google.com/')
38 end).to raise_error(TypeError)
41 it 'should transmit a User-Agent header when sending requests' do
42 client.user_agent = 'Custom User Agent/1.2.3'
44 conn = stub_connection do |stub|
45 stub.get('/') do |env|
46 headers = env[:request_headers]
47 expect(headers).to have_key('User-Agent')
48 expect(headers['User-Agent']).to eq(client.user_agent)
52 client.execute(:uri=>'https://www.google.com/', :connection => conn)
57 RSpec.describe Google::APIClient do
58 include ConnectionHelpers
60 let(:client) { Google::APIClient.new(:application_name => 'API Client Tests') }
62 it "should pass the faraday options provided on initialization to FaraDay configuration block" do
63 client = Google::APIClient.new(faraday_option: {timeout: 999})
64 expect(client.connection.options.timeout).to be == 999
67 it 'should make its version number available' do
68 expect(Google::APIClient::VERSION::STRING).to be_instance_of(String)
71 it 'should default to OAuth 2' do
72 expect(Signet::OAuth2::Client).to be === client.authorization
75 describe 'configure for no authentication' do
77 client.authorization = nil
79 it_should_behave_like 'configurable user agent'
82 describe 'configured for OAuth 1' do
84 client.authorization = :oauth_1
85 client.authorization.token_credential_key = 'abc'
86 client.authorization.token_credential_secret = '123'
89 it 'should use the default OAuth1 client configuration' do
90 expect(client.authorization.temporary_credential_uri.to_s).to eq(
91 'https://www.google.com/accounts/OAuthGetRequestToken'
93 expect(client.authorization.authorization_uri.to_s).to include(
94 'https://www.google.com/accounts/OAuthAuthorizeToken'
96 expect(client.authorization.token_credential_uri.to_s).to eq(
97 'https://www.google.com/accounts/OAuthGetAccessToken'
99 expect(client.authorization.client_credential_key).to eq('anonymous')
100 expect(client.authorization.client_credential_secret).to eq('anonymous')
103 it_should_behave_like 'configurable user agent'
106 describe 'configured for OAuth 2' do
108 client.authorization = :oauth_2
109 client.authorization.access_token = '12345'
113 it_should_behave_like 'configurable user agent'
116 describe 'when executing requests' do
118 @prediction = client.discovered_api('prediction', 'v1.2')
119 client.authorization = :oauth_2
120 @connection = stub_connection do |stub|
121 stub.post('/prediction/v1.2/training?data=12345') do |env|
122 expect(env[:request_headers]['Authorization']).to eq('Bearer 12345')
132 it 'should use default authorization' do
133 client.authorization.access_token = "12345"
135 :api_method => @prediction.training.insert,
136 :parameters => {'data' => '12345'},
137 :connection => @connection
141 it 'should use request scoped authorization when provided' do
142 client.authorization.access_token = "abcdef"
143 new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
145 :api_method => @prediction.training.insert,
146 :parameters => {'data' => '12345'},
147 :authorization => new_auth,
148 :connection => @connection
152 it 'should accept options with batch/request style execute' do
153 client.authorization.access_token = "abcdef"
154 new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
155 request = client.generate_request(
156 :api_method => @prediction.training.insert,
157 :parameters => {'data' => '12345'}
161 :authorization => new_auth,
162 :connection => @connection
167 it 'should accept options in array style execute' do
168 client.authorization.access_token = "abcdef"
169 new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
171 @prediction.training.insert, {'data' => '12345'}, '', {},
172 { :authorization => new_auth, :connection => @connection }
177 describe 'when retries enabled' do
186 it 'should follow redirects' do
187 client.authorization = nil
188 @connection = stub_connection do |stub|
189 stub.get('/foo') do |env|
190 [302, {'location' => 'https://www.google.com/bar'}, '{}']
192 stub.get('/bar') do |env|
198 :uri => 'https://www.google.com/foo',
199 :connection => @connection
203 it 'should refresh tokens on 401 errors' do
204 client.authorization.access_token = '12345'
205 expect(client.authorization).to receive(:fetch_access_token!)
207 @connection = stub_connection do |stub|
208 stub.get('/foo') do |env|
211 stub.get('/foo') do |env|
217 :uri => 'https://www.google.com/foo',
218 :connection => @connection
223 it 'should not attempt multiple token refreshes' do
224 client.authorization.access_token = '12345'
225 expect(client.authorization).to receive(:fetch_access_token!).once
227 @connection = stub_connection do |stub|
228 stub.get('/foo') do |env|
234 :uri => 'https://www.google.com/foo',
235 :connection => @connection
239 it 'should not retry on client errors' do
241 @connection = stub_connection do |stub|
242 stub.get('/foo') do |env|
243 expect(count).to eq(0)
250 :uri => 'https://www.google.com/foo',
251 :connection => @connection,
252 :authenticated => false
256 it 'should retry on 500 errors' do
257 client.authorization = nil
259 @connection = stub_connection do |stub|
260 stub.get('/foo') do |env|
263 stub.get('/foo') do |env|
268 expect(client.execute(
269 :uri => 'https://www.google.com/foo',
270 :connection => @connection
275 it 'should fail after max retries' do
276 client.authorization = nil
278 @connection = stub_connection do |stub|
279 stub.get('/foo') do |env|
285 expect(client.execute(
286 :uri => 'https://www.google.com/foo',
287 :connection => @connection
289 expect(count).to eq(3)
294 describe 'when retries disabled and expired_auth_retry on (default)' do
303 it 'should refresh tokens on 401 errors' do
304 client.authorization.access_token = '12345'
305 expect(client.authorization).to receive(:fetch_access_token!)
307 @connection = stub_connection do |stub|
308 stub.get('/foo') do |env|
311 stub.get('/foo') do |env|
317 :uri => 'https://www.gogole.com/foo',
318 :connection => @connection
324 describe 'when retries disabled and expired_auth_retry off' do
327 client.expired_auth_retry = false
330 it 'should not refresh tokens on 401 errors' do
331 client.authorization.access_token = '12345'
332 expect(client.authorization).not_to receive(:fetch_access_token!)
334 @connection = stub_connection do |stub|
335 stub.get('/foo') do |env|
338 stub.get('/foo') do |env|
343 resp = client.execute(
344 :uri => 'https://www.gogole.com/foo',
345 :connection => @connection
348 expect(resp.response.status).to be == 401