3 # Copyright 2010 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
22 require 'compat/multi_json'
23 require 'signet/oauth_1/client'
24 require 'google/api_client'
26 fixtures_path = File.expand_path('../../../fixtures', __FILE__)
28 RSpec.describe Google::APIClient do
29 include ConnectionHelpers
30 CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
33 # Reset client to not-quite-pristine state
38 it 'should raise a type error for bogus authorization' do
40 Google::APIClient.new(:application_name => 'API Client Tests', :authorization => 42)
41 end).to raise_error(TypeError)
44 it 'should not be able to retrieve the discovery document for a bogus API' do
46 CLIENT.discovery_document('bogus')
47 end).to raise_error(Google::APIClient::TransmissionError)
49 CLIENT.discovered_api('bogus')
50 end).to raise_error(Google::APIClient::TransmissionError)
53 it 'should raise an error for bogus services' do
55 CLIENT.discovered_api(42)
56 end).to raise_error(TypeError)
59 it 'should raise an error for bogus services' do
61 CLIENT.preferred_version(42)
62 end).to raise_error(TypeError)
65 it 'should raise an error for bogus methods' do
68 end).to raise_error(TypeError)
71 it 'should not return a preferred version for bogus service names' do
72 expect(CLIENT.preferred_version('bogus')).to eq(nil)
75 describe 'with zoo API' do
76 it 'should return API instance registered from file' do
77 zoo_json = File.join(fixtures_path, 'files', 'zoo.json')
78 contents = File.open(zoo_json, 'rb') { |io| io.read }
79 api = CLIENT.register_discovery_document('zoo', 'v1', contents)
80 expect(api).to be_kind_of(Google::APIClient::API)
84 describe 'with the prediction API' do
86 CLIENT.authorization = nil
87 # The prediction API no longer exposes a v1, so we have to be
88 # careful about looking up the wrong API version.
89 @prediction = CLIENT.discovered_api('prediction', 'v1.2')
92 it 'should correctly determine the discovery URI' do
93 expect(CLIENT.discovery_uri('prediction')).to be ===
94 'https://www.googleapis.com/discovery/v1/apis/prediction/v1/rest'
97 it 'should correctly determine the discovery URI if :user_ip is set' do
98 CLIENT.user_ip = '127.0.0.1'
100 conn = stub_connection do |stub|
101 stub.get('/discovery/v1/apis/prediction/v1.2/rest?userIp=127.0.0.1') do |env|
106 :http_method => 'GET',
107 :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
108 :authenticated => false,
114 it 'should correctly determine the discovery URI if :key is set' do
115 CLIENT.key = 'qwerty'
116 conn = stub_connection do |stub|
117 stub.get('/discovery/v1/apis/prediction/v1.2/rest?key=qwerty') do |env|
121 request = CLIENT.execute(
122 :http_method => 'GET',
123 :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
124 :authenticated => false,
130 it 'should correctly determine the discovery URI if both are set' do
131 CLIENT.key = 'qwerty'
132 CLIENT.user_ip = '127.0.0.1'
133 conn = stub_connection do |stub|
134 stub.get('/discovery/v1/apis/prediction/v1.2/rest?key=qwerty&userIp=127.0.0.1') do |env|
138 request = CLIENT.execute(
139 :http_method => 'GET',
140 :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
141 :authenticated => false,
147 it 'should correctly generate API objects' do
148 expect(CLIENT.discovered_api('prediction', 'v1.2').name).to eq('prediction')
149 expect(CLIENT.discovered_api('prediction', 'v1.2').version).to eq('v1.2')
150 expect(CLIENT.discovered_api(:prediction, 'v1.2').name).to eq('prediction')
151 expect(CLIENT.discovered_api(:prediction, 'v1.2').version).to eq('v1.2')
154 it 'should discover methods' do
155 expect(CLIENT.discovered_method(
156 'prediction.training.insert', 'prediction', 'v1.2'
157 ).name).to eq('insert')
158 expect(CLIENT.discovered_method(
159 :'prediction.training.insert', :prediction, 'v1.2'
160 ).name).to eq('insert')
161 expect(CLIENT.discovered_method(
162 'prediction.training.delete', 'prediction', 'v1.2'
163 ).name).to eq('delete')
166 it 'should define the origin API in discovered methods' do
167 expect(CLIENT.discovered_method(
168 'prediction.training.insert', 'prediction', 'v1.2'
169 ).api.name).to eq('prediction')
172 it 'should not find methods that are not in the discovery document' do
173 expect(CLIENT.discovered_method(
174 'prediction.bogus', 'prediction', 'v1.2'
178 it 'should raise an error for bogus methods' do
180 CLIENT.discovered_method(42, 'prediction', 'v1.2')
181 end).to raise_error(TypeError)
184 it 'should raise an error for bogus methods' do
186 CLIENT.execute(:api_method => CLIENT.discovered_api('prediction', 'v1.2'))
187 end).to raise_error(TypeError)
190 it 'should correctly determine the preferred version' do
191 expect(CLIENT.preferred_version('prediction').version).not_to eq('v1')
192 expect(CLIENT.preferred_version(:prediction).version).not_to eq('v1')
195 it 'should return a batch path' do
196 expect(CLIENT.discovered_api('prediction', 'v1.2').batch_path).not_to be_nil
199 it 'should generate valid requests' do
200 conn = stub_connection do |stub|
201 stub.post('/prediction/v1.2/training?data=12345') do |env|
202 expect(env[:body]).to eq('')
206 request = CLIENT.execute(
207 :api_method => @prediction.training.insert,
208 :parameters => {'data' => '12345'},
214 it 'should generate valid requests when parameter value includes semicolon' do
215 conn = stub_connection do |stub|
216 # semicolon (;) in parameter value was being converted to
217 # bare ampersand (&) in 0.4.7. ensure that it gets converted
218 # to a CGI-escaped semicolon (%3B) instead.
219 stub.post('/prediction/v1.2/training?data=12345%3B67890') do |env|
220 expect(env[:body]).to eq('')
224 request = CLIENT.execute(
225 :api_method => @prediction.training.insert,
226 :parameters => {'data' => '12345;67890'},
232 it 'should generate valid requests when multivalued parameters are passed' do
233 conn = stub_connection do |stub|
234 stub.post('/prediction/v1.2/training?data=1&data=2') do |env|
235 expect(env.params['data']).to include('1', '2')
239 request = CLIENT.execute(
240 :api_method => @prediction.training.insert,
241 :parameters => {'data' => ['1', '2']},
247 it 'should generate requests against the correct URIs' do
248 conn = stub_connection do |stub|
249 stub.post('/prediction/v1.2/training?data=12345') do |env|
253 request = CLIENT.execute(
254 :api_method => @prediction.training.insert,
255 :parameters => {'data' => '12345'},
261 it 'should generate requests against the correct URIs' do
262 conn = stub_connection do |stub|
263 stub.post('/prediction/v1.2/training?data=12345') do |env|
267 request = CLIENT.execute(
268 :api_method => @prediction.training.insert,
269 :parameters => {'data' => '12345'},
275 it 'should allow modification to the base URIs for testing purposes' do
276 # Using a new client instance here to avoid caching rebased discovery doc
278 Google::APIClient.new(:application_name => 'API Client Tests').discovered_api('prediction', 'v1.2')
279 prediction_rebase.method_base =
280 'https://testing-domain.example.com/prediction/v1.2/'
282 conn = stub_connection do |stub|
283 stub.post('/prediction/v1.2/training') do |env|
284 expect(env[:url].host).to eq('testing-domain.example.com')
289 request = CLIENT.execute(
290 :api_method => prediction_rebase.training.insert,
291 :parameters => {'data' => '123'},
297 it 'should generate OAuth 1 requests' do
298 CLIENT.authorization = :oauth_1
299 CLIENT.authorization.token_credential_key = '12345'
300 CLIENT.authorization.token_credential_secret = '12345'
302 conn = stub_connection do |stub|
303 stub.post('/prediction/v1.2/training?data=12345') do |env|
304 expect(env[:request_headers]).to have_key('Authorization')
305 expect(env[:request_headers]['Authorization']).to match(/^OAuth/)
310 request = CLIENT.execute(
311 :api_method => @prediction.training.insert,
312 :parameters => {'data' => '12345'},
318 it 'should generate OAuth 2 requests' do
319 CLIENT.authorization = :oauth_2
320 CLIENT.authorization.access_token = '12345'
322 conn = stub_connection do |stub|
323 stub.post('/prediction/v1.2/training?data=12345') do |env|
324 expect(env[:request_headers]).to have_key('Authorization')
325 expect(env[:request_headers]['Authorization']).to match(/^Bearer/)
330 request = CLIENT.execute(
331 :api_method => @prediction.training.insert,
332 :parameters => {'data' => '12345'},
338 it 'should not be able to execute improperly authorized requests' do
339 CLIENT.authorization = :oauth_1
340 CLIENT.authorization.token_credential_key = '12345'
341 CLIENT.authorization.token_credential_secret = '12345'
342 result = CLIENT.execute(
343 @prediction.training.insert,
346 expect(result.response.status).to eq(401)
349 it 'should not be able to execute improperly authorized requests' do
350 CLIENT.authorization = :oauth_2
351 CLIENT.authorization.access_token = '12345'
352 result = CLIENT.execute(
353 @prediction.training.insert,
356 expect(result.response.status).to eq(401)
359 it 'should not be able to execute improperly authorized requests' do
361 CLIENT.authorization = :oauth_1
362 CLIENT.authorization.token_credential_key = '12345'
363 CLIENT.authorization.token_credential_secret = '12345'
364 result = CLIENT.execute!(
365 @prediction.training.insert,
368 end).to raise_error(Google::APIClient::ClientError)
371 it 'should not be able to execute improperly authorized requests' do
373 CLIENT.authorization = :oauth_2
374 CLIENT.authorization.access_token = '12345'
375 result = CLIENT.execute!(
376 @prediction.training.insert,
379 end).to raise_error(Google::APIClient::ClientError)
382 it 'should correctly handle unnamed parameters' do
383 conn = stub_connection do |stub|
384 stub.post('/prediction/v1.2/training') do |env|
385 expect(env[:request_headers]).to have_key('Content-Type')
386 expect(env[:request_headers]['Content-Type']).to eq('application/json')
390 CLIENT.authorization = :oauth_2
391 CLIENT.authorization.access_token = '12345'
393 :api_method => @prediction.training.insert,
394 :body => MultiJson.dump({"id" => "bucket/object"}),
395 :headers => {'Content-Type' => 'application/json'},
402 describe 'with the plus API' do
404 CLIENT.authorization = nil
405 @plus = CLIENT.discovered_api('plus')
408 it 'should correctly determine the discovery URI' do
409 expect(CLIENT.discovery_uri('plus')).to be ===
410 'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest'
413 it 'should find APIs that are in the discovery document' do
414 expect(CLIENT.discovered_api('plus').name).to eq('plus')
415 expect(CLIENT.discovered_api('plus').version).to eq('v1')
416 expect(CLIENT.discovered_api(:plus).name).to eq('plus')
417 expect(CLIENT.discovered_api(:plus).version).to eq('v1')
420 it 'should find methods that are in the discovery document' do
421 # TODO(bobaman) Fix this when the RPC names are correct
422 expect(CLIENT.discovered_method(
423 'plus.activities.list', 'plus'
424 ).name).to eq('list')
427 it 'should define the origin API in discovered methods' do
428 expect(CLIENT.discovered_method(
429 'plus.activities.list', 'plus'
430 ).api.name).to eq('plus')
433 it 'should not find methods that are not in the discovery document' do
434 expect(CLIENT.discovered_method('plus.bogus', 'plus')).to eq(nil)
437 it 'should generate requests against the correct URIs' do
438 conn = stub_connection do |stub|
439 stub.get('/plus/v1/people/107807692475771887386/activities/public') do |env|
444 request = CLIENT.execute(
445 :api_method => @plus.activities.list,
447 'userId' => '107807692475771887386', 'collection' => 'public'
449 :authenticated => false,
455 it 'should correctly validate parameters' do
458 :api_method => @plus.activities.list,
459 :parameters => {'alt' => 'json'},
460 :authenticated => false
462 end).to raise_error(ArgumentError)
465 it 'should correctly validate parameters' do
468 :api_method => @plus.activities.list,
470 'userId' => '107807692475771887386', 'collection' => 'bogus'
472 :authenticated => false
473 ).to_env(CLIENT.connection)
474 end).to raise_error(ArgumentError)
478 describe 'with the adsense API' do
480 CLIENT.authorization = nil
481 @adsense = CLIENT.discovered_api('adsense', 'v1.3')
484 it 'should correctly determine the discovery URI' do
485 expect(CLIENT.discovery_uri('adsense', 'v1.3').to_s).to be ===
486 'https://www.googleapis.com/discovery/v1/apis/adsense/v1.3/rest'
489 it 'should find APIs that are in the discovery document' do
490 expect(CLIENT.discovered_api('adsense', 'v1.3').name).to eq('adsense')
491 expect(CLIENT.discovered_api('adsense', 'v1.3').version).to eq('v1.3')
494 it 'should return a batch path' do
495 expect(CLIENT.discovered_api('adsense', 'v1.3').batch_path).not_to be_nil
498 it 'should find methods that are in the discovery document' do
499 expect(CLIENT.discovered_method(
500 'adsense.reports.generate', 'adsense', 'v1.3'
501 ).name).to eq('generate')
504 it 'should not find methods that are not in the discovery document' do
505 expect(CLIENT.discovered_method('adsense.bogus', 'adsense', 'v1.3')).to eq(nil)
508 it 'should generate requests against the correct URIs' do
509 conn = stub_connection do |stub|
510 stub.get('/adsense/v1.3/adclients') do |env|
514 request = CLIENT.execute(
515 :api_method => @adsense.adclients.list,
516 :authenticated => false,
522 it 'should not be able to execute requests without authorization' do
523 result = CLIENT.execute(
524 :api_method => @adsense.adclients.list,
525 :authenticated => false
527 expect(result.response.status).to eq(401)
530 it 'should fail when validating missing required parameters' do
533 :api_method => @adsense.reports.generate,
534 :authenticated => false
536 end).to raise_error(ArgumentError)
539 it 'should succeed when validating parameters in a correct call' do
540 conn = stub_connection do |stub|
541 stub.get('/adsense/v1.3/reports?dimension=DATE&endDate=2010-01-01&metric=PAGE_VIEWS&startDate=2000-01-01') do |env|
547 :api_method => @adsense.reports.generate,
549 'startDate' => '2000-01-01',
550 'endDate' => '2010-01-01',
551 'dimension' => 'DATE',
552 'metric' => 'PAGE_VIEWS'
554 :authenticated => false,
557 end).not_to raise_error
561 it 'should fail when validating parameters with invalid values' do
564 :api_method => @adsense.reports.generate,
566 'startDate' => '2000-01-01',
567 'endDate' => '2010-01-01',
568 'dimension' => 'BAD_CHARACTERS=-&*(£&',
569 'metric' => 'PAGE_VIEWS'
571 :authenticated => false
573 end).to raise_error(ArgumentError)
576 it 'should succeed when validating repeated parameters in a correct call' do
577 conn = stub_connection do |stub|
578 stub.get('/adsense/v1.3/reports?dimension=DATE&dimension=PRODUCT_CODE'+
579 '&endDate=2010-01-01&metric=CLICKS&metric=PAGE_VIEWS&'+
580 'startDate=2000-01-01') do |env|
586 :api_method => @adsense.reports.generate,
588 'startDate' => '2000-01-01',
589 'endDate' => '2010-01-01',
590 'dimension' => ['DATE', 'PRODUCT_CODE'],
591 'metric' => ['PAGE_VIEWS', 'CLICKS']
593 :authenticated => false,
596 end).not_to raise_error
600 it 'should fail when validating incorrect repeated parameters' do
603 :api_method => @adsense.reports.generate,
605 'startDate' => '2000-01-01',
606 'endDate' => '2010-01-01',
607 'dimension' => ['DATE', 'BAD_CHARACTERS=-&*(£&'],
608 'metric' => ['PAGE_VIEWS', 'CLICKS']
610 :authenticated => false
612 end).to raise_error(ArgumentError)
615 it 'should generate valid requests when multivalued parameters are passed' do
616 conn = stub_connection do |stub|
617 stub.get('/adsense/v1.3/reports?dimension=DATE&dimension=PRODUCT_CODE'+
618 '&endDate=2010-01-01&metric=CLICKS&metric=PAGE_VIEWS&'+
619 'startDate=2000-01-01') do |env|
620 expect(env.params['dimension']).to include('DATE', 'PRODUCT_CODE')
621 expect(env.params['metric']).to include('CLICKS', 'PAGE_VIEWS')
625 request = CLIENT.execute(
626 :api_method => @adsense.reports.generate,
628 'startDate' => '2000-01-01',
629 'endDate' => '2010-01-01',
630 'dimension' => ['DATE', 'PRODUCT_CODE'],
631 'metric' => ['PAGE_VIEWS', 'CLICKS']
633 :authenticated => false,
640 describe 'with the Drive API' do
642 CLIENT.authorization = nil
643 @drive = CLIENT.discovered_api('drive', 'v1')
646 it 'should include media upload info methods' do
647 expect(@drive.files.insert.media_upload).not_to eq(nil)
650 it 'should include accepted media types' do
651 expect(@drive.files.insert.media_upload.accepted_types).not_to be_empty
654 it 'should have an upload path' do
655 expect(@drive.files.insert.media_upload.uri_template).not_to eq(nil)
658 it 'should have a max file size' do
659 expect(@drive.files.insert.media_upload.max_size).not_to eq(nil)