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'
25 require 'google/api_client/version'
27 describe Google::APIClient do
28 include ConnectionHelpers
29 CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
32 # Reset client to not-quite-pristine state
37 it 'should raise a type error for bogus authorization' do
39 Google::APIClient.new(:application_name => 'API Client Tests', :authorization => 42)
40 end).should raise_error(TypeError)
43 it 'should not be able to retrieve the discovery document for a bogus API' do
45 CLIENT.discovery_document('bogus')
46 end).should raise_error(Google::APIClient::TransmissionError)
48 CLIENT.discovered_api('bogus')
49 end).should raise_error(Google::APIClient::TransmissionError)
52 it 'should raise an error for bogus services' do
54 CLIENT.discovered_api(42)
55 end).should raise_error(TypeError)
58 it 'should raise an error for bogus services' do
60 CLIENT.preferred_version(42)
61 end).should raise_error(TypeError)
64 it 'should raise an error for bogus methods' do
67 end).should raise_error(TypeError)
70 it 'should not return a preferred version for bogus service names' do
71 CLIENT.preferred_version('bogus').should == nil
74 describe 'with the prediction API' do
76 CLIENT.authorization = nil
77 # The prediction API no longer exposes a v1, so we have to be
78 # careful about looking up the wrong API version.
79 @prediction = CLIENT.discovered_api('prediction', 'v1.2')
82 it 'should correctly determine the discovery URI' do
83 CLIENT.discovery_uri('prediction').should ===
84 'https://www.googleapis.com/discovery/v1/apis/prediction/v1/rest'
87 it 'should correctly determine the discovery URI if :user_ip is set' do
88 CLIENT.user_ip = '127.0.0.1'
90 conn = stub_connection do |stub|
91 stub.get('/discovery/v1/apis/prediction/v1.2/rest?userIp=127.0.0.1') do |env|
96 :http_method => 'GET',
97 :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
98 :authenticated => false,
104 it 'should correctly determine the discovery URI if :key is set' do
105 CLIENT.key = 'qwerty'
106 conn = stub_connection do |stub|
107 stub.get('/discovery/v1/apis/prediction/v1.2/rest?key=qwerty') do |env|
111 request = CLIENT.execute(
112 :http_method => 'GET',
113 :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
114 :authenticated => false,
120 it 'should correctly determine the discovery URI if both are set' do
121 CLIENT.key = 'qwerty'
122 CLIENT.user_ip = '127.0.0.1'
123 conn = stub_connection do |stub|
124 stub.get('/discovery/v1/apis/prediction/v1.2/rest?key=qwerty&userIp=127.0.0.1') do |env|
128 request = CLIENT.execute(
129 :http_method => 'GET',
130 :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
131 :authenticated => false,
137 it 'should correctly generate API objects' do
138 CLIENT.discovered_api('prediction', 'v1.2').name.should == 'prediction'
139 CLIENT.discovered_api('prediction', 'v1.2').version.should == 'v1.2'
140 CLIENT.discovered_api(:prediction, 'v1.2').name.should == 'prediction'
141 CLIENT.discovered_api(:prediction, 'v1.2').version.should == 'v1.2'
144 it 'should discover methods' do
145 CLIENT.discovered_method(
146 'prediction.training.insert', 'prediction', 'v1.2'
147 ).name.should == 'insert'
148 CLIENT.discovered_method(
149 :'prediction.training.insert', :prediction, 'v1.2'
150 ).name.should == 'insert'
151 CLIENT.discovered_method(
152 'prediction.training.delete', 'prediction', 'v1.2'
153 ).name.should == 'delete'
156 it 'should define the origin API in discovered methods' do
157 CLIENT.discovered_method(
158 'prediction.training.insert', 'prediction', 'v1.2'
159 ).api.name.should == 'prediction'
162 it 'should not find methods that are not in the discovery document' do
163 CLIENT.discovered_method(
164 'prediction.bogus', 'prediction', 'v1.2'
168 it 'should raise an error for bogus methods' do
170 CLIENT.discovered_method(42, 'prediction', 'v1.2')
171 end).should raise_error(TypeError)
174 it 'should raise an error for bogus methods' do
176 CLIENT.execute(:api_method => CLIENT.discovered_api('prediction', 'v1.2'))
177 end).should raise_error(TypeError)
180 it 'should correctly determine the preferred version' do
181 CLIENT.preferred_version('prediction').version.should_not == 'v1'
182 CLIENT.preferred_version(:prediction).version.should_not == 'v1'
185 it 'should return a batch path' do
186 CLIENT.discovered_api('prediction', 'v1.2').batch_path.should_not be_nil
189 it 'should generate valid requests' do
190 conn = stub_connection do |stub|
191 stub.post('/prediction/v1.2/training?data=12345') do |env|
192 env[:body].should == ''
196 request = CLIENT.execute(
197 :api_method => @prediction.training.insert,
198 :parameters => {'data' => '12345'},
204 it 'should generate valid requests when parameter value includes semicolon' do
205 conn = stub_connection do |stub|
206 # semicolon (;) in parameter value was being converted to
207 # bare ampersand (&) in 0.4.7. ensure that it gets converted
208 # to a CGI-escaped semicolon (%3B) instead.
209 stub.post('/prediction/v1.2/training?data=12345%3B67890') do |env|
210 env[:body].should == ''
214 request = CLIENT.execute(
215 :api_method => @prediction.training.insert,
216 :parameters => {'data' => '12345;67890'},
222 it 'should generate valid requests when multivalued parameters are passed' do
223 conn = stub_connection do |stub|
224 stub.post('/prediction/v1.2/training?data=1&data=2') do |env|
225 env.params['data'].should include('1', '2')
229 request = CLIENT.execute(
230 :api_method => @prediction.training.insert,
231 :parameters => {'data' => ['1', '2']},
237 it 'should generate requests against the correct URIs' do
238 conn = stub_connection do |stub|
239 stub.post('/prediction/v1.2/training?data=12345') do |env|
243 request = CLIENT.execute(
244 :api_method => @prediction.training.insert,
245 :parameters => {'data' => '12345'},
251 it 'should generate requests against the correct URIs' do
252 conn = stub_connection do |stub|
253 stub.post('/prediction/v1.2/training?data=12345') do |env|
257 request = CLIENT.execute(
258 :api_method => @prediction.training.insert,
259 :parameters => {'data' => '12345'},
265 it 'should allow modification to the base URIs for testing purposes' do
266 # Using a new client instance here to avoid caching rebased discovery doc
268 Google::APIClient.new(:application_name => 'API Client Tests').discovered_api('prediction', 'v1.2')
269 prediction_rebase.method_base =
270 'https://testing-domain.example.com/prediction/v1.2/'
272 conn = stub_connection do |stub|
273 stub.post('/prediction/v1.2/training') do |env|
274 env[:url].host.should == 'testing-domain.example.com'
279 request = CLIENT.execute(
280 :api_method => prediction_rebase.training.insert,
281 :parameters => {'data' => '123'},
287 it 'should generate OAuth 1 requests' do
288 CLIENT.authorization = :oauth_1
289 CLIENT.authorization.token_credential_key = '12345'
290 CLIENT.authorization.token_credential_secret = '12345'
292 conn = stub_connection do |stub|
293 stub.post('/prediction/v1.2/training?data=12345') do |env|
294 env[:request_headers].should have_key('Authorization')
295 env[:request_headers]['Authorization'].should =~ /^OAuth/
300 request = CLIENT.execute(
301 :api_method => @prediction.training.insert,
302 :parameters => {'data' => '12345'},
308 it 'should generate OAuth 2 requests' do
309 CLIENT.authorization = :oauth_2
310 CLIENT.authorization.access_token = '12345'
312 conn = stub_connection do |stub|
313 stub.post('/prediction/v1.2/training?data=12345') do |env|
314 env[:request_headers].should have_key('Authorization')
315 env[:request_headers]['Authorization'].should =~ /^Bearer/
320 request = CLIENT.execute(
321 :api_method => @prediction.training.insert,
322 :parameters => {'data' => '12345'},
328 it 'should not be able to execute improperly authorized requests' do
329 CLIENT.authorization = :oauth_1
330 CLIENT.authorization.token_credential_key = '12345'
331 CLIENT.authorization.token_credential_secret = '12345'
332 result = CLIENT.execute(
333 @prediction.training.insert,
336 result.response.status.should == 401
339 it 'should not be able to execute improperly authorized requests' do
340 CLIENT.authorization = :oauth_2
341 CLIENT.authorization.access_token = '12345'
342 result = CLIENT.execute(
343 @prediction.training.insert,
346 result.response.status.should == 401
349 it 'should not be able to execute improperly authorized requests' do
351 CLIENT.authorization = :oauth_1
352 CLIENT.authorization.token_credential_key = '12345'
353 CLIENT.authorization.token_credential_secret = '12345'
354 result = CLIENT.execute!(
355 @prediction.training.insert,
358 end).should raise_error(Google::APIClient::ClientError)
361 it 'should not be able to execute improperly authorized requests' do
363 CLIENT.authorization = :oauth_2
364 CLIENT.authorization.access_token = '12345'
365 result = CLIENT.execute!(
366 @prediction.training.insert,
369 end).should raise_error(Google::APIClient::ClientError)
372 it 'should correctly handle unnamed parameters' do
373 conn = stub_connection do |stub|
374 stub.post('/prediction/v1.2/training') do |env|
375 env[:request_headers].should have_key('Content-Type')
376 env[:request_headers]['Content-Type'].should == 'application/json'
380 CLIENT.authorization = :oauth_2
381 CLIENT.authorization.access_token = '12345'
383 :api_method => @prediction.training.insert,
384 :body => MultiJson.dump({"id" => "bucket/object"}),
385 :headers => {'Content-Type' => 'application/json'},
392 describe 'with the plus API' do
394 CLIENT.authorization = nil
395 @plus = CLIENT.discovered_api('plus')
398 it 'should correctly determine the discovery URI' do
399 CLIENT.discovery_uri('plus').should ===
400 'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest'
403 it 'should find APIs that are in the discovery document' do
404 CLIENT.discovered_api('plus').name.should == 'plus'
405 CLIENT.discovered_api('plus').version.should == 'v1'
406 CLIENT.discovered_api(:plus).name.should == 'plus'
407 CLIENT.discovered_api(:plus).version.should == 'v1'
410 it 'should find methods that are in the discovery document' do
411 # TODO(bobaman) Fix this when the RPC names are correct
412 CLIENT.discovered_method(
413 'plus.activities.list', 'plus'
414 ).name.should == 'list'
417 it 'should define the origin API in discovered methods' do
418 CLIENT.discovered_method(
419 'plus.activities.list', 'plus'
420 ).api.name.should == 'plus'
423 it 'should not find methods that are not in the discovery document' do
424 CLIENT.discovered_method('plus.bogus', 'plus').should == nil
427 it 'should generate requests against the correct URIs' do
428 conn = stub_connection do |stub|
429 stub.get('/plus/v1/people/107807692475771887386/activities/public') do |env|
434 request = CLIENT.execute(
435 :api_method => @plus.activities.list,
437 'userId' => '107807692475771887386', 'collection' => 'public'
439 :authenticated => false,
445 it 'should correctly validate parameters' do
448 :api_method => @plus.activities.list,
449 :parameters => {'alt' => 'json'},
450 :authenticated => false
452 end).should raise_error(ArgumentError)
455 it 'should correctly validate parameters' do
458 :api_method => @plus.activities.list,
460 'userId' => '107807692475771887386', 'collection' => 'bogus'
462 :authenticated => false
463 ).to_env(CLIENT.connection)
464 end).should raise_error(ArgumentError)
468 describe 'with the adsense API' do
470 CLIENT.authorization = nil
471 @adsense = CLIENT.discovered_api('adsense', 'v1.3')
474 it 'should correctly determine the discovery URI' do
475 CLIENT.discovery_uri('adsense', 'v1.3').to_s.should ===
476 'https://www.googleapis.com/discovery/v1/apis/adsense/v1.3/rest'
479 it 'should find APIs that are in the discovery document' do
480 CLIENT.discovered_api('adsense', 'v1.3').name.should == 'adsense'
481 CLIENT.discovered_api('adsense', 'v1.3').version.should == 'v1.3'
484 it 'should return a batch path' do
485 CLIENT.discovered_api('adsense', 'v1.3').batch_path.should_not be_nil
488 it 'should find methods that are in the discovery document' do
489 CLIENT.discovered_method(
490 'adsense.reports.generate', 'adsense', 'v1.3'
491 ).name.should == 'generate'
494 it 'should not find methods that are not in the discovery document' do
495 CLIENT.discovered_method('adsense.bogus', 'adsense', 'v1.3').should == nil
498 it 'should generate requests against the correct URIs' do
499 conn = stub_connection do |stub|
500 stub.get('/adsense/v1.3/adclients') do |env|
504 request = CLIENT.execute(
505 :api_method => @adsense.adclients.list,
506 :authenticated => false,
512 it 'should not be able to execute requests without authorization' do
513 result = CLIENT.execute(
514 :api_method => @adsense.adclients.list,
515 :authenticated => false
517 result.response.status.should == 401
520 it 'should fail when validating missing required parameters' do
523 :api_method => @adsense.reports.generate,
524 :authenticated => false
526 end).should raise_error(ArgumentError)
529 it 'should succeed when validating parameters in a correct call' do
530 conn = stub_connection do |stub|
531 stub.get('/adsense/v1.3/reports?dimension=DATE&endDate=2010-01-01&metric=PAGE_VIEWS&startDate=2000-01-01') do |env|
537 :api_method => @adsense.reports.generate,
539 'startDate' => '2000-01-01',
540 'endDate' => '2010-01-01',
541 'dimension' => 'DATE',
542 'metric' => 'PAGE_VIEWS'
544 :authenticated => false,
547 end).should_not raise_error
551 it 'should fail when validating parameters with invalid values' do
554 :api_method => @adsense.reports.generate,
556 'startDate' => '2000-01-01',
557 'endDate' => '2010-01-01',
558 'dimension' => 'BAD_CHARACTERS=-&*(£&',
559 'metric' => 'PAGE_VIEWS'
561 :authenticated => false
563 end).should raise_error(ArgumentError)
566 it 'should succeed when validating repeated parameters in a correct call' do
567 conn = stub_connection do |stub|
568 stub.get('/adsense/v1.3/reports?dimension=DATE&dimension=PRODUCT_CODE'+
569 '&endDate=2010-01-01&metric=CLICKS&metric=PAGE_VIEWS&'+
570 'startDate=2000-01-01') do |env|
576 :api_method => @adsense.reports.generate,
578 'startDate' => '2000-01-01',
579 'endDate' => '2010-01-01',
580 'dimension' => ['DATE', 'PRODUCT_CODE'],
581 'metric' => ['PAGE_VIEWS', 'CLICKS']
583 :authenticated => false,
586 end).should_not raise_error
590 it 'should fail when validating incorrect repeated parameters' do
593 :api_method => @adsense.reports.generate,
595 'startDate' => '2000-01-01',
596 'endDate' => '2010-01-01',
597 'dimension' => ['DATE', 'BAD_CHARACTERS=-&*(£&'],
598 'metric' => ['PAGE_VIEWS', 'CLICKS']
600 :authenticated => false
602 end).should raise_error(ArgumentError)
605 it 'should generate valid requests when multivalued parameters are passed' do
606 conn = stub_connection do |stub|
607 stub.get('/adsense/v1.3/reports?dimension=DATE&dimension=PRODUCT_CODE'+
608 '&endDate=2010-01-01&metric=CLICKS&metric=PAGE_VIEWS&'+
609 'startDate=2000-01-01') do |env|
610 env.params['dimension'].should include('DATE', 'PRODUCT_CODE')
611 env.params['metric'].should include('CLICKS', 'PAGE_VIEWS')
615 request = CLIENT.execute(
616 :api_method => @adsense.reports.generate,
618 'startDate' => '2000-01-01',
619 'endDate' => '2010-01-01',
620 'dimension' => ['DATE', 'PRODUCT_CODE'],
621 'metric' => ['PAGE_VIEWS', 'CLICKS']
623 :authenticated => false,
630 describe 'with the Drive API' do
632 CLIENT.authorization = nil
633 @drive = CLIENT.discovered_api('drive', 'v1')
636 it 'should include media upload info methods' do
637 @drive.files.insert.media_upload.should_not == nil
640 it 'should include accepted media types' do
641 @drive.files.insert.media_upload.accepted_types.should_not be_empty
644 it 'should have an upload path' do
645 @drive.files.insert.media_upload.uri_template.should_not == nil
648 it 'should have a max file size' do
649 @drive.files.insert.media_upload.max_size.should_not == nil