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.
21 require 'faraday/utils'
23 require 'compat/multi_json'
24 require 'signet/oauth_1/client'
25 require 'google/api_client'
26 require 'google/api_client/version'
29 def initialize(&block)
38 def mock_connection(&block)
39 connection = Faraday.new do |builder|
44 describe Google::APIClient do
45 include ConnectionHelpers
46 CLIENT ||= Google::APIClient.new
49 # Reset client to not-quite-pristine state
54 it 'should raise a type error for bogus authorization' do
56 Google::APIClient.new(:authorization => 42)
57 end).should raise_error(TypeError)
60 it 'should not be able to retrieve the discovery document for a bogus API' do
62 CLIENT.discovery_document('bogus')
63 end).should raise_error(Google::APIClient::TransmissionError)
65 CLIENT.discovered_api('bogus')
66 end).should raise_error(Google::APIClient::TransmissionError)
69 it 'should raise an error for bogus services' do
71 CLIENT.discovered_api(42)
72 end).should raise_error(TypeError)
75 it 'should raise an error for bogus services' do
77 CLIENT.preferred_version(42)
78 end).should raise_error(TypeError)
81 it 'should raise an error for bogus methods' do
84 end).should raise_error(TypeError)
87 it 'should not return a preferred version for bogus service names' do
88 CLIENT.preferred_version('bogus').should == nil
91 describe 'with the prediction API' do
93 CLIENT.authorization = nil
94 # The prediction API no longer exposes a v1, so we have to be
95 # careful about looking up the wrong API version.
96 @prediction = CLIENT.discovered_api('prediction', 'v1.2')
99 it 'should correctly determine the discovery URI' do
100 CLIENT.discovery_uri('prediction').should ===
101 'https://www.googleapis.com/discovery/v1/apis/prediction/v1/rest'
104 it 'should correctly determine the discovery URI if :user_ip is set' do
105 CLIENT.user_ip = '127.0.0.1'
107 conn = stub_connection do |stub|
108 stub.get('/discovery/v1/apis/prediction/v1.2/rest?userIp=127.0.0.1') do |env|
112 :http_method => 'GET',
113 :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
114 :authenticated => false,
120 it 'should correctly determine the discovery URI if :key is set' do
121 CLIENT.key = 'qwerty'
122 conn = stub_connection do |stub|
123 stub.get('/discovery/v1/apis/prediction/v1.2/rest?key=qwerty') do |env|
126 request = CLIENT.execute(
127 :http_method => 'GET',
128 :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
129 :authenticated => false,
135 it 'should correctly determine the discovery URI if both are set' do
136 CLIENT.key = 'qwerty'
137 CLIENT.user_ip = '127.0.0.1'
138 conn = stub_connection do |stub|
139 stub.get('/discovery/v1/apis/prediction/v1.2/rest?key=qwerty&userIp=127.0.0.1') do |env|
142 request = CLIENT.execute(
143 :http_method => 'GET',
144 :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
145 :authenticated => false,
151 it 'should correctly generate API objects' do
152 CLIENT.discovered_api('prediction', 'v1.2').name.should == 'prediction'
153 CLIENT.discovered_api('prediction', 'v1.2').version.should == 'v1.2'
154 CLIENT.discovered_api(:prediction, 'v1.2').name.should == 'prediction'
155 CLIENT.discovered_api(:prediction, 'v1.2').version.should == 'v1.2'
158 it 'should discover methods' do
159 CLIENT.discovered_method(
160 'prediction.training.insert', 'prediction', 'v1.2'
161 ).name.should == 'insert'
162 CLIENT.discovered_method(
163 :'prediction.training.insert', :prediction, 'v1.2'
164 ).name.should == 'insert'
165 CLIENT.discovered_method(
166 'prediction.training.delete', 'prediction', 'v1.2'
167 ).name.should == 'delete'
170 it 'should define the origin API in discovered methods' do
171 CLIENT.discovered_method(
172 'prediction.training.insert', 'prediction', 'v1.2'
173 ).api.name.should == 'prediction'
176 it 'should not find methods that are not in the discovery document' do
177 CLIENT.discovered_method(
178 'prediction.bogus', 'prediction', 'v1.2'
182 it 'should raise an error for bogus methods' do
184 CLIENT.discovered_method(42, 'prediction', 'v1.2')
185 end).should raise_error(TypeError)
188 it 'should raise an error for bogus methods' do
190 CLIENT.execute(:api_method => CLIENT.discovered_api('prediction', 'v1.2'))
191 end).should raise_error(TypeError)
194 it 'should correctly determine the preferred version' do
195 CLIENT.preferred_version('prediction').version.should_not == 'v1'
196 CLIENT.preferred_version(:prediction).version.should_not == 'v1'
199 it 'should return a batch path' do
200 CLIENT.discovered_api('prediction', 'v1.2').batch_path.should_not be_nil
203 it 'should generate valid requests' do
204 conn = stub_connection do |stub|
205 stub.post('/prediction/v1.2/training?data=12345') do |env|
206 env[:body].should == ''
209 request = CLIENT.execute(
210 :api_method => @prediction.training.insert,
211 :parameters => {'data' => '12345'},
217 it 'should generate valid requests when repeated parameters are passed' do
218 conn = stub_connection do |stub|
219 stub.post('/prediction/v1.2/training?data=1&data=2') do |env|
220 env[:params]['data'].should include('1', '2')
223 request = CLIENT.execute(
224 :api_method => @prediction.training.insert,
225 :parameters => [['data', '1'], ['data','2']],
231 it 'should generate requests against the correct URIs' do
232 conn = stub_connection do |stub|
233 stub.post('/prediction/v1.2/training?data=12345') do |env|
236 request = CLIENT.execute(
237 :api_method => @prediction.training.insert,
238 :parameters => {'data' => '12345'},
244 it 'should generate requests against the correct URIs' do
245 conn = stub_connection do |stub|
246 stub.post('/prediction/v1.2/training?data=12345') do |env|
249 request = CLIENT.execute(
250 :api_method => @prediction.training.insert,
251 :parameters => {'data' => '12345'},
257 it 'should allow modification to the base URIs for testing purposes' do
258 # Using a new client instance here to avoid caching rebased discovery doc
260 Google::APIClient.new.discovered_api('prediction', 'v1.2')
261 prediction_rebase.method_base =
262 'https://testing-domain.example.com/prediction/v1.2/'
264 conn = stub_connection do |stub|
265 stub.post('/prediction/v1.2/training') do |env|
266 env[:url].host.should == 'testing-domain.example.com'
270 request = CLIENT.execute(
271 :api_method => prediction_rebase.training.insert,
272 :parameters => {'data' => '123'},
278 it 'should generate OAuth 1 requests' do
279 CLIENT.authorization = :oauth_1
280 CLIENT.authorization.token_credential_key = '12345'
281 CLIENT.authorization.token_credential_secret = '12345'
283 conn = stub_connection do |stub|
284 stub.post('/prediction/v1.2/training?data=12345') do |env|
285 env[:request_headers].should have_key('Authorization')
286 env[:request_headers]['Authorization'].should =~ /^OAuth/
290 request = CLIENT.execute(
291 :api_method => @prediction.training.insert,
292 :parameters => {'data' => '12345'},
298 it 'should generate OAuth 2 requests' do
299 CLIENT.authorization = :oauth_2
300 CLIENT.authorization.access_token = '12345'
302 conn = stub_connection do |stub|
303 stub.post('/prediction/v1.2/training?data=12345') do |env|
304 env[:request_headers].should have_key('Authorization')
305 env[:request_headers]['Authorization'].should =~ /^Bearer/
309 request = CLIENT.execute(
310 :api_method => @prediction.training.insert,
311 :parameters => {'data' => '12345'},
317 it 'should not be able to execute improperly authorized requests' do
318 CLIENT.authorization = :oauth_1
319 CLIENT.authorization.token_credential_key = '12345'
320 CLIENT.authorization.token_credential_secret = '12345'
321 result = CLIENT.execute(
322 @prediction.training.insert,
325 result.response.status.should == 401
328 it 'should not be able to execute improperly authorized requests' do
329 CLIENT.authorization = :oauth_2
330 CLIENT.authorization.access_token = '12345'
331 result = CLIENT.execute(
332 @prediction.training.insert,
335 result.response.status.should == 401
338 it 'should not be able to execute improperly authorized requests' do
340 CLIENT.authorization = :oauth_1
341 CLIENT.authorization.token_credential_key = '12345'
342 CLIENT.authorization.token_credential_secret = '12345'
343 result = CLIENT.execute!(
344 @prediction.training.insert,
347 end).should raise_error(Google::APIClient::ClientError)
350 it 'should not be able to execute improperly authorized requests' do
352 CLIENT.authorization = :oauth_2
353 CLIENT.authorization.access_token = '12345'
354 result = CLIENT.execute!(
355 @prediction.training.insert,
358 end).should raise_error(Google::APIClient::ClientError)
361 it 'should correctly handle unnamed parameters' do
362 conn = stub_connection do |stub|
363 stub.post('/prediction/v1.2/training') do |env|
364 env[:request_headers].should have_key('Content-Type')
365 env[:request_headers]['Content-Type'].should == 'application/json'
368 CLIENT.authorization = :oauth_2
369 CLIENT.authorization.access_token = '12345'
371 :api_method => @prediction.training.insert,
372 :body => MultiJson.dump({"id" => "bucket/object"}),
373 :headers => {'Content-Type' => 'application/json'},
380 describe 'with the plus API' do
382 CLIENT.authorization = nil
383 @plus = CLIENT.discovered_api('plus')
386 it 'should correctly determine the discovery URI' do
387 CLIENT.discovery_uri('plus').should ===
388 'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest'
391 it 'should find APIs that are in the discovery document' do
392 CLIENT.discovered_api('plus').name.should == 'plus'
393 CLIENT.discovered_api('plus').version.should == 'v1'
394 CLIENT.discovered_api(:plus).name.should == 'plus'
395 CLIENT.discovered_api(:plus).version.should == 'v1'
398 it 'should find methods that are in the discovery document' do
399 # TODO(bobaman) Fix this when the RPC names are correct
400 CLIENT.discovered_method(
401 'plus.activities.list', 'plus'
402 ).name.should == 'list'
405 it 'should define the origin API in discovered methods' do
406 CLIENT.discovered_method(
407 'plus.activities.list', 'plus'
408 ).api.name.should == 'plus'
411 it 'should not find methods that are not in the discovery document' do
412 CLIENT.discovered_method('plus.bogus', 'plus').should == nil
415 it 'should generate requests against the correct URIs' do
416 conn = stub_connection do |stub|
417 stub.get('/plus/v1/people/107807692475771887386/activities/public' +
418 '?collection=public&userId=107807692475771887386') do |env|
422 request = CLIENT.execute(
423 :api_method => @plus.activities.list,
425 'userId' => '107807692475771887386', 'collection' => 'public'
427 :authenticated => false,
433 it 'should correctly validate parameters' do
436 :api_method => @plus.activities.list,
437 :parameters => {'alt' => 'json'},
438 :authenticated => false
440 end).should raise_error(ArgumentError)
443 it 'should correctly validate parameters' do
446 :api_method => @plus.activities.list,
448 'userId' => '107807692475771887386', 'collection' => 'bogus'
450 :authenticated => false
451 ).to_env(Faraday.default_connection)
452 end).should raise_error(ArgumentError)
457 describe 'with the latitude API' do
459 CLIENT.authorization = nil
460 @latitude = CLIENT.discovered_api('latitude')
463 it 'should correctly determine the discovery URI' do
464 CLIENT.discovery_uri('latitude').should ===
465 'https://www.googleapis.com/discovery/v1/apis/latitude/v1/rest'
468 it 'should find APIs that are in the discovery document' do
469 CLIENT.discovered_api('latitude').name.should == 'latitude'
470 CLIENT.discovered_api('latitude').version.should == 'v1'
473 it 'should return a batch path' do
474 CLIENT.discovered_api('latitude').batch_path.should_not be_nil
477 it 'should find methods that are in the discovery document' do
478 CLIENT.discovered_method(
479 'latitude.currentLocation.get', 'latitude'
480 ).name.should == 'get'
483 it 'should define the origin API in discovered methods' do
484 CLIENT.discovered_method(
485 'latitude.currentLocation.get', 'latitude'
486 ).api.name.should == 'latitude'
489 it 'should not find methods that are not in the discovery document' do
490 CLIENT.discovered_method('latitude.bogus', 'latitude').should == nil
493 it 'should generate requests against the correct URIs' do
494 request = CLIENT.generate_request(
495 :api_method => 'latitude.currentLocation.get',
496 :authenticated => false
498 request.to_env(Faraday.default_connection)[:url].to_s.should ===
499 'https://www.googleapis.com/latitude/v1/currentLocation'
502 it 'should generate requests against the correct URIs' do
503 request = CLIENT.generate_request(
504 :api_method => @latitude.current_location.get,
505 :authenticated => false
507 request.to_env(Faraday.default_connection)[:url].to_s.should ===
508 'https://www.googleapis.com/latitude/v1/currentLocation'
511 it 'should not be able to execute requests without authorization' do
512 result = CLIENT.execute(
513 :api_method => 'latitude.currentLocation.get',
514 :authenticated => false
516 result.response.status.should == 401
521 describe 'with the moderator API' do
523 CLIENT.authorization = nil
524 @moderator = CLIENT.discovered_api('moderator')
527 it 'should correctly determine the discovery URI' do
528 CLIENT.discovery_uri('moderator').should ===
529 'https://www.googleapis.com/discovery/v1/apis/moderator/v1/rest'
532 it 'should find APIs that are in the discovery document' do
533 CLIENT.discovered_api('moderator').name.should == 'moderator'
534 CLIENT.discovered_api('moderator').version.should == 'v1'
537 it 'should find methods that are in the discovery document' do
538 CLIENT.discovered_method(
539 'moderator.profiles.get', 'moderator'
540 ).name.should == 'get'
543 it 'should define the origin API in discovered methods' do
544 CLIENT.discovered_method(
545 'moderator.profiles.get', 'moderator'
546 ).api.name.should == 'moderator'
549 it 'should not find methods that are not in the discovery document' do
550 CLIENT.discovered_method('moderator.bogus', 'moderator').should == nil
553 it 'should return a batch path' do
554 CLIENT.discovered_api('moderator').batch_path.should_not be_nil
557 it 'should generate requests against the correct URIs' do
558 conn = stub_connection do |stub|
559 stub.get('/moderator/v1/profiles/@me') do |env|
562 request = CLIENT.execute(
563 :api_method => 'moderator.profiles.get',
564 :authenticated => false,
570 it 'should generate requests against the correct URIs' do
571 conn = stub_connection do |stub|
572 stub.get('/moderator/v1/profiles/@me') do |env|
575 request = CLIENT.execute(
576 :api_method => @moderator.profiles.get,
577 :authenticated => false,
583 it 'should not be able to execute requests without authorization' do
584 result = CLIENT.execute(
585 'moderator.profiles.get',
589 {:authenticated => false}
591 result.response.status.should == 401
595 describe 'with the adsense API' do
597 CLIENT.authorization = nil
598 @adsense = CLIENT.discovered_api('adsense', 'v1')
601 it 'should correctly determine the discovery URI' do
602 CLIENT.discovery_uri('adsense').should ===
603 'https://www.googleapis.com/discovery/v1/apis/adsense/v1/rest'
606 it 'should find APIs that are in the discovery document' do
607 CLIENT.discovered_api('adsense').name.should == 'adsense'
608 CLIENT.discovered_api('adsense').version.should == 'v1'
611 it 'should return a batch path' do
612 CLIENT.discovered_api('adsense').batch_path.should_not be_nil
615 it 'should find methods that are in the discovery document' do
616 CLIENT.discovered_method(
617 'adsense.reports.generate', 'adsense'
618 ).name.should == 'generate'
621 it 'should not find methods that are not in the discovery document' do
622 CLIENT.discovered_method('adsense.bogus', 'adsense').should == nil
625 it 'should generate requests against the correct URIs' do
626 conn = stub_connection do |stub|
627 stub.get('/adsense/v1/adclients') do |env|
630 request = CLIENT.execute(
631 :api_method => 'adsense.adclients.list',
632 :authenticated => false,
638 it 'should generate requests against the correct URIs' do
639 conn = stub_connection do |stub|
640 stub.get('/adsense/v1/adclients') do |env|
643 request = CLIENT.execute(
644 :api_method => @adsense.adclients.list,
645 :authenticated => false,
651 it 'should not be able to execute requests without authorization' do
652 result = CLIENT.execute(
653 :api_method => 'adsense.adclients.list',
654 :authenticated => false
656 result.response.status.should == 401
659 it 'should fail when validating missing required parameters' do
662 :api_method => @adsense.reports.generate,
663 :authenticated => false
665 end).should raise_error(ArgumentError)
668 it 'should succeed when validating parameters in a correct call' do
669 conn = stub_connection do |stub|
670 stub.get('/adsense/v1/reports?dimension=DATE&endDate=2010-01-01&metric=PAGE_VIEWS&startDate=2000-01-01') do |env|
675 :api_method => @adsense.reports.generate,
677 'startDate' => '2000-01-01',
678 'endDate' => '2010-01-01',
679 'dimension' => 'DATE',
680 'metric' => 'PAGE_VIEWS'
682 :authenticated => false,
685 end).should_not raise_error
689 it 'should fail when validating parameters with invalid values' do
692 :api_method => @adsense.reports.generate,
694 'startDate' => '2000-01-01',
695 'endDate' => '2010-01-01',
696 'dimension' => 'BAD_CHARACTERS=-&*(£&',
697 'metric' => 'PAGE_VIEWS'
699 :authenticated => false
701 end).should raise_error(ArgumentError)
704 it 'should succeed when validating repeated parameters in a correct call' do
705 conn = stub_connection do |stub|
706 stub.get('/adsense/v1/reports?dimension%5B%5D=DATE&dimension%5B%5D=PRODUCT_CODE'+
707 '&endDate=2010-01-01&metric%5B%5D=CLICKS&metric%5B%5D=PAGE_VIEWS&'+
708 'startDate=2000-01-01') do |env|
713 :api_method => @adsense.reports.generate,
715 'startDate' => '2000-01-01',
716 'endDate' => '2010-01-01',
717 'dimension' => ['DATE', 'PRODUCT_CODE'],
718 'metric' => ['PAGE_VIEWS', 'CLICKS']
720 :authenticated => false,
723 end).should_not raise_error
727 it 'should fail when validating incorrect repeated parameters' do
730 :api_method => @adsense.reports.generate,
732 'startDate' => '2000-01-01',
733 'endDate' => '2010-01-01',
734 'dimension' => ['DATE', 'BAD_CHARACTERS=-&*(£&'],
735 'metric' => ['PAGE_VIEWS', 'CLICKS']
737 :authenticated => false
739 end).should raise_error(ArgumentError)
743 describe 'with the Drive API' do
745 CLIENT.authorization = nil
746 @drive = CLIENT.discovered_api('drive', 'v1')
749 it 'should include media upload info methods' do
750 @drive.files.insert.media_upload.should_not == nil
753 it 'should include accepted media types' do
754 @drive.files.insert.media_upload.accepted_types.should_not be_empty
757 it 'should have an upload path' do
758 @drive.files.insert.media_upload.uri_template.should_not == nil
761 it 'should have a max file size' do
762 @drive.files.insert.media_upload.max_size.should_not == nil