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 describe Google::APIClient do
58 include ConnectionHelpers
60 let(:client) { Google::APIClient.new(:application_name => 'API Client Tests') }
62 it 'should make its version number available' do
63 expect(Google::APIClient::VERSION::STRING).to be_instance_of(String)
66 it 'should default to OAuth 2' do
67 expect(Signet::OAuth2::Client).to be === client.authorization
70 describe 'configure for no authentication' do
72 client.authorization = nil
74 it_should_behave_like 'configurable user agent'
77 describe 'configured for OAuth 1' do
79 client.authorization = :oauth_1
80 client.authorization.token_credential_key = 'abc'
81 client.authorization.token_credential_secret = '123'
84 it 'should use the default OAuth1 client configuration' do
85 expect(client.authorization.temporary_credential_uri.to_s).to eq(
86 'https://www.google.com/accounts/OAuthGetRequestToken'
88 expect(client.authorization.authorization_uri.to_s).to include(
89 'https://www.google.com/accounts/OAuthAuthorizeToken'
91 expect(client.authorization.token_credential_uri.to_s).to eq(
92 'https://www.google.com/accounts/OAuthGetAccessToken'
94 expect(client.authorization.client_credential_key).to eq('anonymous')
95 expect(client.authorization.client_credential_secret).to eq('anonymous')
98 it_should_behave_like 'configurable user agent'
101 describe 'configured for OAuth 2' do
103 client.authorization = :oauth_2
104 client.authorization.access_token = '12345'
108 it_should_behave_like 'configurable user agent'
111 describe 'when executing requests' do
113 @prediction = client.discovered_api('prediction', 'v1.2')
114 client.authorization = :oauth_2
115 @connection = stub_connection do |stub|
116 stub.post('/prediction/v1.2/training?data=12345') do |env|
117 expect(env[:request_headers]['Authorization']).to eq('Bearer 12345')
127 it 'should use default authorization' do
128 client.authorization.access_token = "12345"
130 :api_method => @prediction.training.insert,
131 :parameters => {'data' => '12345'},
132 :connection => @connection
136 it 'should use request scoped authorization when provided' do
137 client.authorization.access_token = "abcdef"
138 new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
140 :api_method => @prediction.training.insert,
141 :parameters => {'data' => '12345'},
142 :authorization => new_auth,
143 :connection => @connection
147 it 'should accept options with batch/request style execute' do
148 client.authorization.access_token = "abcdef"
149 new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
150 request = client.generate_request(
151 :api_method => @prediction.training.insert,
152 :parameters => {'data' => '12345'}
156 :authorization => new_auth,
157 :connection => @connection
162 it 'should accept options in array style execute' do
163 client.authorization.access_token = "abcdef"
164 new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
166 @prediction.training.insert, {'data' => '12345'}, '', {},
167 { :authorization => new_auth, :connection => @connection }
172 describe 'when retries enabled' do
181 it 'should follow redirects' do
182 client.authorization = nil
183 @connection = stub_connection do |stub|
184 stub.get('/foo') do |env|
185 [302, {'location' => 'https://www.google.com/bar'}, '{}']
187 stub.get('/bar') do |env|
193 :uri => 'https://www.gogole.com/foo',
194 :connection => @connection
198 it 'should refresh tokens on 401 tokens' do
199 client.authorization.access_token = '12345'
200 expect(client.authorization).to receive(:fetch_access_token!)
202 @connection = stub_connection do |stub|
203 stub.get('/foo') do |env|
206 stub.get('/foo') do |env|
212 :uri => 'https://www.gogole.com/foo',
213 :connection => @connection
218 it 'should not attempt multiple token refreshes' do
219 client.authorization.access_token = '12345'
220 expect(client.authorization).to receive(:fetch_access_token!).once
222 @connection = stub_connection do |stub|
223 stub.get('/foo') do |env|
229 :uri => 'https://www.gogole.com/foo',
230 :connection => @connection
234 it 'should not retry on client errors' do
236 @connection = stub_connection do |stub|
237 stub.get('/foo') do |env|
238 expect(count).to eq(0)
245 :uri => 'https://www.gogole.com/foo',
246 :connection => @connection,
247 :authenticated => false
251 it 'should retry on 500 errors' do
252 client.authorization = nil
254 @connection = stub_connection do |stub|
255 stub.get('/foo') do |env|
258 stub.get('/foo') do |env|
263 expect(client.execute(
264 :uri => 'https://www.gogole.com/foo',
265 :connection => @connection
270 it 'should fail after max retries' do
271 client.authorization = nil
273 @connection = stub_connection do |stub|
274 stub.get('/foo') do |env|
280 expect(client.execute(
281 :uri => 'https://www.gogole.com/foo',
282 :connection => @connection
284 expect(count).to eq(3)