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)
393 describe 'with the latitude API' do
395 CLIENT.authorization = nil
396 @latitude = CLIENT.discovered_api('latitude')
399 it 'should correctly determine the discovery URI' do
400 CLIENT.discovery_uri('latitude').should ===
401 'https://www.googleapis.com/discovery/v1/apis/latitude/v1/rest'
404 it 'should find APIs that are in the discovery document' do
405 CLIENT.discovered_api('latitude').name.should == 'latitude'
406 CLIENT.discovered_api('latitude').version.should == 'v1'
409 it 'should return a batch path' do
410 CLIENT.discovered_api('latitude').batch_path.should_not be_nil
413 it 'should find methods that are in the discovery document' do
414 CLIENT.discovered_method(
415 'latitude.currentLocation.get', 'latitude'
416 ).name.should == 'get'
419 it 'should define the origin API in discovered methods' do
420 CLIENT.discovered_method(
421 'latitude.currentLocation.get', 'latitude'
422 ).api.name.should == 'latitude'
425 it 'should not find methods that are not in the discovery document' do
426 CLIENT.discovered_method('latitude.bogus', 'latitude').should == nil
429 it 'should generate requests against the correct URIs' do
430 request = CLIENT.generate_request(
431 :api_method => 'latitude.currentLocation.get',
432 :authenticated => false
434 request.to_env(Faraday.default_connection)[:url].to_s.should ===
435 'https://www.googleapis.com/latitude/v1/currentLocation'
438 it 'should generate requests against the correct URIs' do
439 request = CLIENT.generate_request(
440 :api_method => @latitude.current_location.get,
441 :authenticated => false
443 request.to_env(Faraday.default_connection)[:url].to_s.should ===
444 'https://www.googleapis.com/latitude/v1/currentLocation'
447 it 'should not be able to execute requests without authorization' do
448 result = CLIENT.execute(
449 :api_method => 'latitude.currentLocation.get',
450 :authenticated => false
452 result.response.status.should == 401
456 describe 'with the moderator API' do
458 CLIENT.authorization = nil
459 @moderator = CLIENT.discovered_api('moderator')
462 it 'should correctly determine the discovery URI' do
463 CLIENT.discovery_uri('moderator').should ===
464 'https://www.googleapis.com/discovery/v1/apis/moderator/v1/rest'
467 it 'should find APIs that are in the discovery document' do
468 CLIENT.discovered_api('moderator').name.should == 'moderator'
469 CLIENT.discovered_api('moderator').version.should == 'v1'
472 it 'should find methods that are in the discovery document' do
473 CLIENT.discovered_method(
474 'moderator.profiles.get', 'moderator'
475 ).name.should == 'get'
478 it 'should define the origin API in discovered methods' do
479 CLIENT.discovered_method(
480 'moderator.profiles.get', 'moderator'
481 ).api.name.should == 'moderator'
484 it 'should not find methods that are not in the discovery document' do
485 CLIENT.discovered_method('moderator.bogus', 'moderator').should == nil
488 it 'should return a batch path' do
489 CLIENT.discovered_api('moderator').batch_path.should_not be_nil
492 it 'should generate requests against the correct URIs' do
493 request = CLIENT.generate_request(
494 :api_method => 'moderator.profiles.get',
495 :authenticated => false
497 request.to_env(Faraday.default_connection)[:url].to_s.should ===
498 'https://www.googleapis.com/moderator/v1/profiles/@me'
501 it 'should generate requests against the correct URIs' do
502 request = CLIENT.generate_request(
503 :api_method => @moderator.profiles.get,
504 :authenticated => false
506 request.to_env(Faraday.default_connection)[:url].to_s.should ===
507 'https://www.googleapis.com/moderator/v1/profiles/@me'
510 it 'should not be able to execute requests without authorization' do
511 result = CLIENT.execute(
512 'moderator.profiles.get',
516 {:authenticated => false}
518 result.response.status.should == 401
522 describe 'with the adsense API' do
524 CLIENT.authorization = nil
525 @adsense = CLIENT.discovered_api('adsense', 'v1')
528 it 'should correctly determine the discovery URI' do
529 CLIENT.discovery_uri('adsense').should ===
530 'https://www.googleapis.com/discovery/v1/apis/adsense/v1/rest'
533 it 'should find APIs that are in the discovery document' do
534 CLIENT.discovered_api('adsense').name.should == 'adsense'
535 CLIENT.discovered_api('adsense').version.should == 'v1'
538 it 'should return a batch path' do
539 CLIENT.discovered_api('adsense').batch_path.should_not be_nil
542 it 'should find methods that are in the discovery document' do
543 CLIENT.discovered_method(
544 'adsense.reports.generate', 'adsense'
545 ).name.should == 'generate'
548 it 'should not find methods that are not in the discovery document' do
549 CLIENT.discovered_method('adsense.bogus', 'adsense').should == nil
552 it 'should generate requests against the correct URIs' do
553 request = CLIENT.generate_request(
554 :api_method => 'adsense.adclients.list',
555 :authenticated => false
557 request.to_env(Faraday.default_connection)[:url].to_s.should ===
558 'https://www.googleapis.com/adsense/v1/adclients'
561 it 'should generate requests against the correct URIs' do
562 request = CLIENT.generate_request(
563 :api_method => @adsense.adclients.list,
564 :authenticated => false
566 request.to_env(Faraday.default_connection)[:url].to_s.should ===
567 'https://www.googleapis.com/adsense/v1/adclients'
570 it 'should not be able to execute requests without authorization' do
571 result = CLIENT.execute(
572 :api_method => 'adsense.adclients.list',
573 :authenticated => false
575 result.response.status.should == 401
578 it 'should fail when validating missing required parameters' do
580 CLIENT.generate_request(
581 :api_method => @adsense.reports.generate,
582 :authenticated => false
584 end).should raise_error(ArgumentError)
587 it 'should succeed when validating parameters in a correct call' do
589 CLIENT.generate_request(
590 :api_method => @adsense.reports.generate,
592 'startDate' => '2000-01-01',
593 'endDate' => '2010-01-01',
594 'dimension' => 'DATE',
595 'metric' => 'PAGE_VIEWS'
597 :authenticated => false
599 end).should_not raise_error
602 it 'should fail when validating parameters with invalid values' do
604 CLIENT.generate_request(
605 :api_method => @adsense.reports.generate,
607 'startDate' => '2000-01-01',
608 'endDate' => '2010-01-01',
609 'dimension' => 'BAD_CHARACTERS=-&*(£&',
610 'metric' => 'PAGE_VIEWS'
612 :authenticated => false
614 end).should raise_error(ArgumentError)
617 it 'should succeed when validating repeated parameters in a correct call' do
619 CLIENT.generate_request(
620 :api_method => @adsense.reports.generate,
622 'startDate' => '2000-01-01',
623 'endDate' => '2010-01-01',
624 'dimension' => ['DATE', 'PRODUCT_CODE'],
625 'metric' => ['PAGE_VIEWS', 'CLICKS']
627 :authenticated => false
629 end).should_not raise_error
632 it 'should fail when validating incorrect repeated parameters' do
634 CLIENT.generate_request(
635 :api_method => @adsense.reports.generate,
637 'startDate' => '2000-01-01',
638 'endDate' => '2010-01-01',
639 'dimension' => ['DATE', 'BAD_CHARACTERS=-&*(£&'],
640 'metric' => ['PAGE_VIEWS', 'CLICKS']
642 :authenticated => false
644 end).should raise_error(ArgumentError)
648 describe 'with the Drive API' do
650 CLIENT.authorization = nil
651 @drive = CLIENT.discovered_api('drive', 'v1')
654 it 'should include media upload info methods' do
655 @drive.files.insert.media_upload.should_not == nil
658 it 'should include accepted media types' do
659 @drive.files.insert.media_upload.accepted_types.should_not be_empty
662 it 'should have an upload path' do
663 @drive.files.insert.media_upload.uri_template.should_not == nil
666 it 'should have a max file size' do
667 @drive.files.insert.media_upload.max_size.should_not == nil