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.
20 gem 'faraday', '~> 0.8.1'
22 require 'faraday/utils'
24 require 'compat/multi_json'
26 gem 'signet', '~> 0.4.0'
27 require 'signet/oauth_1/client'
29 require 'google/api_client'
30 require 'google/api_client/version'
32 describe Google::APIClient do
33 CLIENT ||= Google::APIClient.new
36 # Reset client to not-quite-pristine state
41 it 'should raise a type error for bogus authorization' do
43 Google::APIClient.new(:authorization => 42)
44 end).should raise_error(TypeError)
47 it 'should not be able to retrieve the discovery document for a bogus API' do
49 CLIENT.discovery_document('bogus')
50 end).should raise_error(Google::APIClient::TransmissionError)
52 CLIENT.discovered_api('bogus')
53 end).should raise_error(Google::APIClient::TransmissionError)
56 it 'should raise an error for bogus services' do
58 CLIENT.discovered_api(42)
59 end).should raise_error(TypeError)
62 it 'should raise an error for bogus services' do
64 CLIENT.preferred_version(42)
65 end).should raise_error(TypeError)
68 it 'should raise an error for bogus methods' do
70 CLIENT.generate_request(42)
71 end).should raise_error(TypeError)
74 it 'should not return a preferred version for bogus service names' do
75 CLIENT.preferred_version('bogus').should == nil
78 describe 'with the prediction API' do
80 CLIENT.authorization = nil
81 # The prediction API no longer exposes a v1, so we have to be
82 # careful about looking up the wrong API version.
83 @prediction = CLIENT.discovered_api('prediction', 'v1.2')
86 it 'should correctly determine the discovery URI' do
87 CLIENT.discovery_uri('prediction').should ===
88 'https://www.googleapis.com/discovery/v1/apis/prediction/v1/rest'
91 it 'should correctly determine the discovery URI if :user_ip is set' do
92 CLIENT.user_ip = '127.0.0.1'
93 request = CLIENT.generate_request(
94 :http_method => 'GET',
95 :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
96 :authenticated => false
98 request.to_env(Faraday.default_connection)[:url].to_s.should === (
99 'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
104 it 'should correctly determine the discovery URI if :key is set' do
105 CLIENT.key = 'qwerty'
106 request = CLIENT.generate_request(
107 :http_method => 'GET',
108 :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
109 :authenticated => false
111 request.to_env(Faraday.default_connection)[:url].to_s.should === (
112 'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
117 it 'should correctly determine the discovery URI if both are set' do
118 CLIENT.key = 'qwerty'
119 CLIENT.user_ip = '127.0.0.1'
120 request = CLIENT.generate_request(
121 :http_method => 'GET',
122 :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
123 :authenticated => false
125 Addressable::URI.parse(
126 request.to_env(Faraday.default_connection)[:url]
127 ).query_values.should == {
129 'userIp' => '127.0.0.1'
133 it 'should correctly generate API objects' do
134 CLIENT.discovered_api('prediction', 'v1.2').name.should == 'prediction'
135 CLIENT.discovered_api('prediction', 'v1.2').version.should == 'v1.2'
136 CLIENT.discovered_api(:prediction, 'v1.2').name.should == 'prediction'
137 CLIENT.discovered_api(:prediction, 'v1.2').version.should == 'v1.2'
140 it 'should discover methods' do
141 CLIENT.discovered_method(
142 'prediction.training.insert', 'prediction', 'v1.2'
143 ).name.should == 'insert'
144 CLIENT.discovered_method(
145 :'prediction.training.insert', :prediction, 'v1.2'
146 ).name.should == 'insert'
147 CLIENT.discovered_method(
148 'prediction.training.delete', 'prediction', 'v1.2'
149 ).name.should == 'delete'
152 it 'should define the origin API in discovered methods' do
153 CLIENT.discovered_method(
154 'prediction.training.insert', 'prediction', 'v1.2'
155 ).api.name.should == 'prediction'
158 it 'should not find methods that are not in the discovery document' do
159 CLIENT.discovered_method(
160 'prediction.bogus', 'prediction', 'v1.2'
164 it 'should raise an error for bogus methods' do
166 CLIENT.discovered_method(42, 'prediction', 'v1.2')
167 end).should raise_error(TypeError)
170 it 'should raise an error for bogus methods' do
172 CLIENT.generate_request(CLIENT.discovered_api('prediction', 'v1.2'))
173 end).should raise_error(TypeError)
176 it 'should correctly determine the preferred version' do
177 CLIENT.preferred_version('prediction').version.should_not == 'v1'
178 CLIENT.preferred_version(:prediction).version.should_not == 'v1'
181 it 'should return a batch path' do
182 CLIENT.discovered_api('prediction', 'v1.2').batch_path.should_not be_nil
185 it 'should generate valid requests' do
186 request = CLIENT.generate_request(
187 :api_method => @prediction.training.insert,
188 :parameters => {'data' => '12345'}
190 request.method.should == :post
191 request.to_env(Faraday.default_connection)[:url].to_s.should ===
192 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
193 request.headers.should be_empty
194 request.body.should == ''
197 it 'should generate valid requests when repeated parameters are passed' do
198 request = CLIENT.generate_request(
199 :api_method => @prediction.training.insert,
200 :parameters => [['data', '1'], ['data','2']]
202 request.method.should == :post
203 request.to_env(Faraday.default_connection)[:url].to_s.should ===
204 'https://www.googleapis.com/prediction/v1.2/training?data=1&data=2'
207 it 'should generate requests against the correct URIs' do
208 request = CLIENT.generate_request(
209 :api_method => @prediction.training.insert,
210 :parameters => {'data' => '12345'}
212 request.to_env(Faraday.default_connection)[:url].to_s.should ===
213 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
216 it 'should generate requests against the correct URIs' do
217 request = CLIENT.generate_request(
218 :api_method => @prediction.training.insert,
219 :parameters => {'data' => '12345'}
221 request.to_env(Faraday.default_connection)[:url].to_s.should ===
222 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
225 it 'should allow modification to the base URIs for testing purposes' do
226 # Using a new client instance here to avoid caching rebased discovery doc
228 Google::APIClient.new.discovered_api('prediction', 'v1.2')
229 prediction_rebase.method_base =
230 'https://testing-domain.example.com/prediction/v1.2/'
231 request = CLIENT.generate_request(
232 :api_method => prediction_rebase.training.insert,
233 :parameters => {'data' => '123'}
235 request.to_env(Faraday.default_connection)[:url].to_s.should === (
236 'https://testing-domain.example.com/' +
237 'prediction/v1.2/training?data=123'
241 it 'should generate OAuth 1 requests' do
242 CLIENT.authorization = :oauth_1
243 CLIENT.authorization.token_credential_key = '12345'
244 CLIENT.authorization.token_credential_secret = '12345'
245 request = CLIENT.generate_request(
246 :api_method => @prediction.training.insert,
247 :parameters => {'data' => '12345'}
249 request.headers.should have_key('Authorization')
250 request.headers['Authorization'].should =~ /^OAuth/
253 it 'should generate OAuth 2 requests' do
254 CLIENT.authorization = :oauth_2
255 CLIENT.authorization.access_token = '12345'
256 request = CLIENT.generate_request(
257 :api_method => @prediction.training.insert,
258 :parameters => {'data' => '12345'}
260 request.headers.should have_key('Authorization')
261 request.headers['Authorization'].should =~ /^Bearer/
264 it 'should not be able to execute improperly authorized requests' do
265 CLIENT.authorization = :oauth_1
266 CLIENT.authorization.token_credential_key = '12345'
267 CLIENT.authorization.token_credential_secret = '12345'
268 result = CLIENT.execute(
269 @prediction.training.insert,
272 result.response.status.should == 401
275 it 'should not be able to execute improperly authorized requests' do
276 CLIENT.authorization = :oauth_2
277 CLIENT.authorization.access_token = '12345'
278 result = CLIENT.execute(
279 @prediction.training.insert,
282 result.response.status.should == 401
285 it 'should not be able to execute improperly authorized requests' do
287 CLIENT.authorization = :oauth_1
288 CLIENT.authorization.token_credential_key = '12345'
289 CLIENT.authorization.token_credential_secret = '12345'
290 result = CLIENT.execute!(
291 @prediction.training.insert,
294 end).should raise_error(Google::APIClient::ClientError)
297 it 'should not be able to execute improperly authorized requests' do
299 CLIENT.authorization = :oauth_2
300 CLIENT.authorization.access_token = '12345'
301 result = CLIENT.execute!(
302 @prediction.training.insert,
305 end).should raise_error(Google::APIClient::ClientError)
308 it 'should correctly handle unnamed parameters' do
309 CLIENT.authorization = :oauth_2
310 CLIENT.authorization.access_token = '12345'
311 result = CLIENT.execute(
312 @prediction.training.insert,
314 MultiJson.dump({"id" => "bucket/object"}),
315 {'Content-Type' => 'application/json'}
317 result.request.headers['Content-Type'].should == 'application/json'
321 describe 'with the plus API' do
323 CLIENT.authorization = nil
324 @plus = CLIENT.discovered_api('plus')
327 it 'should correctly determine the discovery URI' do
328 CLIENT.discovery_uri('plus').should ===
329 'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest'
332 it 'should find APIs that are in the discovery document' do
333 CLIENT.discovered_api('plus').name.should == 'plus'
334 CLIENT.discovered_api('plus').version.should == 'v1'
335 CLIENT.discovered_api(:plus).name.should == 'plus'
336 CLIENT.discovered_api(:plus).version.should == 'v1'
339 it 'should find methods that are in the discovery document' do
340 # TODO(bobaman) Fix this when the RPC names are correct
341 CLIENT.discovered_method(
342 'plus.activities.list', 'plus'
343 ).name.should == 'list'
346 it 'should define the origin API in discovered methods' do
347 CLIENT.discovered_method(
348 'plus.activities.list', 'plus'
349 ).api.name.should == 'plus'
352 it 'should not find methods that are not in the discovery document' do
353 CLIENT.discovered_method('plus.bogus', 'plus').should == nil
356 it 'should generate requests against the correct URIs' do
357 request = CLIENT.generate_request(
358 :api_method => @plus.activities.list,
360 'userId' => '107807692475771887386', 'collection' => 'public'
362 :authenticated => false
364 request.to_env(Faraday.default_connection)[:url].to_s.should === (
365 'https://www.googleapis.com/plus/v1/' +
366 'people/107807692475771887386/activities/public'
370 it 'should correctly validate parameters' do
372 CLIENT.generate_request(
373 :api_method => @plus.activities.list,
374 :parameters => {'alt' => 'json'},
375 :authenticated => false
377 end).should raise_error(ArgumentError)
380 it 'should correctly validate parameters' do
382 CLIENT.generate_request(
383 :api_method => @plus.activities.list,
385 'userId' => '107807692475771887386', 'collection' => 'bogus'
387 :authenticated => false
389 end).should raise_error(ArgumentError)
394 describe 'with the latitude API' do
396 CLIENT.authorization = nil
397 @latitude = CLIENT.discovered_api('latitude')
400 it 'should correctly determine the discovery URI' do
401 CLIENT.discovery_uri('latitude').should ===
402 'https://www.googleapis.com/discovery/v1/apis/latitude/v1/rest'
405 it 'should find APIs that are in the discovery document' do
406 CLIENT.discovered_api('latitude').name.should == 'latitude'
407 CLIENT.discovered_api('latitude').version.should == 'v1'
410 it 'should return a batch path' do
411 CLIENT.discovered_api('latitude').batch_path.should_not be_nil
414 it 'should find methods that are in the discovery document' do
415 CLIENT.discovered_method(
416 'latitude.currentLocation.get', 'latitude'
417 ).name.should == 'get'
420 it 'should define the origin API in discovered methods' do
421 CLIENT.discovered_method(
422 'latitude.currentLocation.get', 'latitude'
423 ).api.name.should == 'latitude'
426 it 'should not find methods that are not in the discovery document' do
427 CLIENT.discovered_method('latitude.bogus', 'latitude').should == nil
430 it 'should generate requests against the correct URIs' do
431 request = CLIENT.generate_request(
432 :api_method => 'latitude.currentLocation.get',
433 :authenticated => false
435 request.to_env(Faraday.default_connection)[:url].to_s.should ===
436 'https://www.googleapis.com/latitude/v1/currentLocation'
439 it 'should generate requests against the correct URIs' do
440 request = CLIENT.generate_request(
441 :api_method => @latitude.current_location.get,
442 :authenticated => false
444 request.to_env(Faraday.default_connection)[:url].to_s.should ===
445 'https://www.googleapis.com/latitude/v1/currentLocation'
448 it 'should not be able to execute requests without authorization' do
449 result = CLIENT.execute(
450 :api_method => 'latitude.currentLocation.get',
451 :authenticated => false
453 result.response.status.should == 401
458 describe 'with the moderator API' do
460 CLIENT.authorization = nil
461 @moderator = CLIENT.discovered_api('moderator')
464 it 'should correctly determine the discovery URI' do
465 CLIENT.discovery_uri('moderator').should ===
466 'https://www.googleapis.com/discovery/v1/apis/moderator/v1/rest'
469 it 'should find APIs that are in the discovery document' do
470 CLIENT.discovered_api('moderator').name.should == 'moderator'
471 CLIENT.discovered_api('moderator').version.should == 'v1'
474 it 'should find methods that are in the discovery document' do
475 CLIENT.discovered_method(
476 'moderator.profiles.get', 'moderator'
477 ).name.should == 'get'
480 it 'should define the origin API in discovered methods' do
481 CLIENT.discovered_method(
482 'moderator.profiles.get', 'moderator'
483 ).api.name.should == 'moderator'
486 it 'should not find methods that are not in the discovery document' do
487 CLIENT.discovered_method('moderator.bogus', 'moderator').should == nil
490 it 'should return a batch path' do
491 CLIENT.discovered_api('moderator').batch_path.should_not be_nil
494 it 'should generate requests against the correct URIs' do
495 request = CLIENT.generate_request(
496 :api_method => 'moderator.profiles.get',
497 :authenticated => false
499 request.to_env(Faraday.default_connection)[:url].to_s.should ===
500 'https://www.googleapis.com/moderator/v1/profiles/@me'
503 it 'should generate requests against the correct URIs' do
504 request = CLIENT.generate_request(
505 :api_method => @moderator.profiles.get,
506 :authenticated => false
508 request.to_env(Faraday.default_connection)[:url].to_s.should ===
509 'https://www.googleapis.com/moderator/v1/profiles/@me'
512 it 'should not be able to execute requests without authorization' do
513 result = CLIENT.execute(
514 'moderator.profiles.get',
518 {:authenticated => false}
520 result.response.status.should == 401
524 describe 'with the adsense API' do
526 CLIENT.authorization = nil
527 @adsense = CLIENT.discovered_api('adsense', 'v1')
530 it 'should correctly determine the discovery URI' do
531 CLIENT.discovery_uri('adsense').should ===
532 'https://www.googleapis.com/discovery/v1/apis/adsense/v1/rest'
535 it 'should find APIs that are in the discovery document' do
536 CLIENT.discovered_api('adsense').name.should == 'adsense'
537 CLIENT.discovered_api('adsense').version.should == 'v1'
540 it 'should return a batch path' do
541 CLIENT.discovered_api('adsense').batch_path.should_not be_nil
544 it 'should find methods that are in the discovery document' do
545 CLIENT.discovered_method(
546 'adsense.reports.generate', 'adsense'
547 ).name.should == 'generate'
550 it 'should not find methods that are not in the discovery document' do
551 CLIENT.discovered_method('adsense.bogus', 'adsense').should == nil
554 it 'should generate requests against the correct URIs' do
555 request = CLIENT.generate_request(
556 :api_method => 'adsense.adclients.list',
557 :authenticated => false
559 request.to_env(Faraday.default_connection)[:url].to_s.should ===
560 'https://www.googleapis.com/adsense/v1/adclients'
563 it 'should generate requests against the correct URIs' do
564 request = CLIENT.generate_request(
565 :api_method => @adsense.adclients.list,
566 :authenticated => false
568 request.to_env(Faraday.default_connection)[:url].to_s.should ===
569 'https://www.googleapis.com/adsense/v1/adclients'
572 it 'should not be able to execute requests without authorization' do
573 result = CLIENT.execute(
574 :api_method => 'adsense.adclients.list',
575 :authenticated => false
577 result.response.status.should == 401
580 it 'should fail when validating missing required parameters' do
582 CLIENT.generate_request(
583 :api_method => @adsense.reports.generate,
584 :authenticated => false
586 end).should raise_error(ArgumentError)
589 it 'should succeed when validating parameters in a correct call' do
591 CLIENT.generate_request(
592 :api_method => @adsense.reports.generate,
594 'startDate' => '2000-01-01',
595 'endDate' => '2010-01-01',
596 'dimension' => 'DATE',
597 'metric' => 'PAGE_VIEWS'
599 :authenticated => false
601 end).should_not raise_error
604 it 'should fail when validating parameters with invalid values' do
606 CLIENT.generate_request(
607 :api_method => @adsense.reports.generate,
609 'startDate' => '2000-01-01',
610 'endDate' => '2010-01-01',
611 'dimension' => 'BAD_CHARACTERS=-&*(£&',
612 'metric' => 'PAGE_VIEWS'
614 :authenticated => false
616 end).should raise_error(ArgumentError)
619 it 'should succeed when validating repeated parameters in a correct call' do
621 CLIENT.generate_request(
622 :api_method => @adsense.reports.generate,
624 'startDate' => '2000-01-01',
625 'endDate' => '2010-01-01',
626 'dimension' => ['DATE', 'PRODUCT_CODE'],
627 'metric' => ['PAGE_VIEWS', 'CLICKS']
629 :authenticated => false
631 end).should_not raise_error
634 it 'should fail when validating incorrect repeated parameters' do
636 CLIENT.generate_request(
637 :api_method => @adsense.reports.generate,
639 'startDate' => '2000-01-01',
640 'endDate' => '2010-01-01',
641 'dimension' => ['DATE', 'BAD_CHARACTERS=-&*(£&'],
642 'metric' => ['PAGE_VIEWS', 'CLICKS']
644 :authenticated => false
646 end).should raise_error(ArgumentError)
650 describe 'with the Drive API' do
652 CLIENT.authorization = nil
653 @drive = CLIENT.discovered_api('drive', 'v1')
656 it 'should include media upload info methods' do
657 @drive.files.insert.media_upload.should_not == nil
660 it 'should include accepted media types' do
661 @drive.files.insert.media_upload.accepted_types.should_not be_empty
664 it 'should have an upload path' do
665 @drive.files.insert.media_upload.uri_template.should_not == nil
668 it 'should have a max file size' do
669 @drive.files.insert.media_upload.max_size.should_not == nil