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.7.0'
22 require 'faraday/utils'
25 gem 'signet', '~> 0.3.0'
26 require 'signet/oauth_1/client'
28 require 'google/api_client'
29 require 'google/api_client/version'
31 describe Google::APIClient do
33 @client = Google::APIClient.new
36 it 'should raise a type error for bogus authorization' do
38 Google::APIClient.new(:authorization => 42)
39 end).should raise_error(TypeError)
42 it 'should not be able to retrieve the discovery document for a bogus API' do
44 @client.discovery_document('bogus')
45 end).should raise_error(Google::APIClient::TransmissionError)
47 @client.discovered_api('bogus')
48 end).should raise_error(Google::APIClient::TransmissionError)
51 it 'should raise an error for bogus services' do
53 @client.discovered_api(42)
54 end).should raise_error(TypeError)
57 it 'should raise an error for bogus services' do
59 @client.preferred_version(42)
60 end).should raise_error(TypeError)
63 it 'should raise an error for bogus methods' do
65 @client.generate_request(42)
66 end).should raise_error(TypeError)
69 it 'should not return a preferred version for bogus service names' do
70 @client.preferred_version('bogus').should == nil
73 describe 'with the prediction API' do
75 @client.authorization = nil
76 # The prediction API no longer exposes a v1, so we have to be
77 # careful about looking up the wrong API version.
78 @prediction = @client.discovered_api('prediction', 'v1.2')
81 it 'should correctly determine the discovery URI' do
82 @client.discovery_uri('prediction').should ===
83 'https://www.googleapis.com/discovery/v1/apis/prediction/v1/rest'
86 it 'should correctly determine the discovery URI if :user_ip is set' do
87 @client.user_ip = '127.0.0.1'
88 request = @client.generate_request(
89 :http_method => 'GET',
90 :uri => @client.discovery_uri('prediction', 'v1.2'),
91 :authenticated => false
93 request.to_env(Faraday.default_connection)[:url].should === (
94 'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
99 it 'should correctly determine the discovery URI if :key is set' do
100 @client.key = 'qwerty'
101 request = @client.generate_request(
102 :http_method => 'GET',
103 :uri => @client.discovery_uri('prediction', 'v1.2'),
104 :authenticated => false
106 request.to_env(Faraday.default_connection)[:url].should === (
107 'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
112 it 'should correctly determine the discovery URI if both are set' do
113 @client.key = 'qwerty'
114 @client.user_ip = '127.0.0.1'
115 request = @client.generate_request(
116 :http_method => 'GET',
117 :uri => @client.discovery_uri('prediction', 'v1.2'),
118 :authenticated => false
120 request.to_env(Faraday.default_connection)[:url].query_values.should == {
122 'userIp' => '127.0.0.1'
126 it 'should correctly generate API objects' do
127 @client.discovered_api('prediction', 'v1.2').name.should == 'prediction'
128 @client.discovered_api('prediction', 'v1.2').version.should == 'v1.2'
129 @client.discovered_api(:prediction, 'v1.2').name.should == 'prediction'
130 @client.discovered_api(:prediction, 'v1.2').version.should == 'v1.2'
133 it 'should discover methods' do
134 @client.discovered_method(
135 'prediction.training.insert', 'prediction', 'v1.2'
136 ).name.should == 'insert'
137 @client.discovered_method(
138 :'prediction.training.insert', :prediction, 'v1.2'
139 ).name.should == 'insert'
140 @client.discovered_method(
141 'prediction.training.delete', 'prediction', 'v1.2'
142 ).name.should == 'delete'
145 it 'should not find methods that are not in the discovery document' do
146 @client.discovered_method(
147 'prediction.bogus', 'prediction', 'v1.2'
151 it 'should raise an error for bogus methods' do
153 @client.discovered_method(42, 'prediction', 'v1.2')
154 end).should raise_error(TypeError)
157 it 'should raise an error for bogus methods' do
159 @client.generate_request(@client.discovered_api('prediction', 'v1.2'))
160 end).should raise_error(TypeError)
163 it 'should correctly determine the preferred version' do
164 @client.preferred_version('prediction').version.should_not == 'v1'
165 @client.preferred_version(:prediction).version.should_not == 'v1'
168 it 'should generate valid requests' do
169 request = @client.generate_request(
170 :api_method => @prediction.training.insert,
171 :parameters => {'data' => '12345', }
173 request.method.should == :post
174 request.to_env(Faraday.default_connection)[:url].should ===
175 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
176 request.headers.should be_empty
177 request.body.should == ''
180 it 'should generate valid requests when repeated parameters are passed' do
181 request = @client.generate_request(
182 :api_method => @prediction.training.insert,
183 :parameters => [['data', '1'],['data','2']]
185 request.method.should == :post
186 request.to_env(Faraday.default_connection)[:url].should ===
187 'https://www.googleapis.com/prediction/v1.2/training?data=1&data=2'
190 it 'should generate requests against the correct URIs' do
191 request = @client.generate_request(
192 :api_method => @prediction.training.insert,
193 :parameters => {'data' => '12345'}
195 request.to_env(Faraday.default_connection)[:url].should ===
196 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
199 it 'should generate requests against the correct URIs' do
200 request = @client.generate_request(
201 :api_method => @prediction.training.insert,
202 :parameters => {'data' => '12345'}
204 request.to_env(Faraday.default_connection)[:url].should ===
205 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
208 it 'should allow modification to the base URIs for testing purposes' do
209 prediction = @client.discovered_api('prediction', 'v1.2')
210 prediction.method_base =
211 'https://testing-domain.googleapis.com/prediction/v1.2/'
212 request = @client.generate_request(
213 :api_method => prediction.training.insert,
214 :parameters => {'data' => '123'}
216 request.to_env(Faraday.default_connection)[:url].should === (
217 'https://testing-domain.googleapis.com/' +
218 'prediction/v1.2/training?data=123'
222 it 'should generate OAuth 1 requests' do
223 @client.authorization = :oauth_1
224 @client.authorization.token_credential_key = '12345'
225 @client.authorization.token_credential_secret = '12345'
226 request = @client.generate_request(
227 :api_method => @prediction.training.insert,
228 :parameters => {'data' => '12345'}
230 request.headers.should have_key('Authorization')
231 request.headers['Authorization'].should =~ /^OAuth/
234 it 'should generate OAuth 2 requests' do
235 @client.authorization = :oauth_2
236 @client.authorization.access_token = '12345'
237 request = @client.generate_request(
238 :api_method => @prediction.training.insert,
239 :parameters => {'data' => '12345'}
241 request.headers.should have_key('Authorization')
242 request.headers['Authorization'].should =~ /^Bearer/
245 it 'should not be able to execute improperly authorized requests' do
246 @client.authorization = :oauth_1
247 @client.authorization.token_credential_key = '12345'
248 @client.authorization.token_credential_secret = '12345'
249 result = @client.execute(
250 @prediction.training.insert,
253 result.response.status.should == 401
256 it 'should not be able to execute improperly authorized requests' do
257 @client.authorization = :oauth_2
258 @client.authorization.access_token = '12345'
259 result = @client.execute(
260 @prediction.training.insert,
263 result.response.status.should == 401
266 it 'should not be able to execute improperly authorized requests' do
268 @client.authorization = :oauth_1
269 @client.authorization.token_credential_key = '12345'
270 @client.authorization.token_credential_secret = '12345'
271 result = @client.execute!(
272 @prediction.training.insert,
275 end).should raise_error(Google::APIClient::ClientError)
278 it 'should not be able to execute improperly authorized requests' do
280 @client.authorization = :oauth_2
281 @client.authorization.access_token = '12345'
282 result = @client.execute!(
283 @prediction.training.insert,
286 end).should raise_error(Google::APIClient::ClientError)
289 it 'should correctly handle unnamed parameters' do
290 @client.authorization = :oauth_2
291 @client.authorization.access_token = '12345'
292 result = @client.execute(
293 @prediction.training.insert,
295 MultiJson.dump({"id" => "bucket/object"}),
296 {'Content-Type' => 'application/json'}
298 result.request.headers['Content-Type'].should == 'application/json'
302 describe 'with the plus API' do
304 @client.authorization = nil
305 @plus = @client.discovered_api('plus')
308 it 'should correctly determine the discovery URI' do
309 @client.discovery_uri('plus').should ===
310 'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest'
313 it 'should find APIs that are in the discovery document' do
314 @client.discovered_api('plus').name.should == 'plus'
315 @client.discovered_api('plus').version.should == 'v1'
316 @client.discovered_api(:plus).name.should == 'plus'
317 @client.discovered_api(:plus).version.should == 'v1'
320 it 'should find methods that are in the discovery document' do
321 # TODO(bobaman) Fix this when the RPC names are correct
322 @client.discovered_method(
323 'plus.activities.list', 'plus'
324 ).name.should == 'list'
327 it 'should not find methods that are not in the discovery document' do
328 @client.discovered_method('plus.bogus', 'plus').should == nil
331 it 'should generate requests against the correct URIs' do
332 request = @client.generate_request(
333 :api_method => @plus.activities.list,
335 'userId' => '107807692475771887386', 'collection' => 'public'
337 :authenticated => false
339 request.to_env(Faraday.default_connection)[:url].should === (
340 'https://www.googleapis.com/plus/v1/' +
341 'people/107807692475771887386/activities/public'
345 it 'should correctly validate parameters' do
347 @client.generate_request(
348 :api_method => @plus.activities.list,
349 :parameters => {'alt' => 'json'},
350 :authenticated => false
352 end).should raise_error(ArgumentError)
355 it 'should correctly validate parameters' do
357 @client.generate_request(
358 :api_method => @plus.activities.list,
360 'userId' => '107807692475771887386', 'collection' => 'bogus'
362 :authenticated => false
364 end).should raise_error(ArgumentError)
368 describe 'with the latitude API' do
370 @client.authorization = nil
371 @latitude = @client.discovered_api('latitude')
374 it 'should correctly determine the discovery URI' do
375 @client.discovery_uri('latitude').should ===
376 'https://www.googleapis.com/discovery/v1/apis/latitude/v1/rest'
379 it 'should find APIs that are in the discovery document' do
380 @client.discovered_api('latitude').name.should == 'latitude'
381 @client.discovered_api('latitude').version.should == 'v1'
384 it 'should find methods that are in the discovery document' do
385 @client.discovered_method(
386 'latitude.currentLocation.get', 'latitude'
387 ).name.should == 'get'
390 it 'should not find methods that are not in the discovery document' do
391 @client.discovered_method('latitude.bogus', 'latitude').should == nil
394 it 'should generate requests against the correct URIs' do
395 request = @client.generate_request(
396 :api_method => 'latitude.currentLocation.get',
397 :authenticated => false
399 request.to_env(Faraday.default_connection)[:url].should ===
400 'https://www.googleapis.com/latitude/v1/currentLocation'
403 it 'should generate requests against the correct URIs' do
404 request = @client.generate_request(
405 :api_method => @latitude.current_location.get,
406 :authenticated => false
408 request.to_env(Faraday.default_connection)[:url].should ===
409 'https://www.googleapis.com/latitude/v1/currentLocation'
412 it 'should not be able to execute requests without authorization' do
413 result = @client.execute(
414 :api_method => 'latitude.currentLocation.get',
415 :authenticated => false
417 result.response.status.should == 401
421 describe 'with the moderator API' do
423 @client.authorization = nil
424 @moderator = @client.discovered_api('moderator')
427 it 'should correctly determine the discovery URI' do
428 @client.discovery_uri('moderator').should ===
429 'https://www.googleapis.com/discovery/v1/apis/moderator/v1/rest'
432 it 'should find APIs that are in the discovery document' do
433 @client.discovered_api('moderator').name.should == 'moderator'
434 @client.discovered_api('moderator').version.should == 'v1'
437 it 'should find methods that are in the discovery document' do
438 @client.discovered_method(
439 'moderator.profiles.get', 'moderator'
440 ).name.should == 'get'
443 it 'should not find methods that are not in the discovery document' do
444 @client.discovered_method('moderator.bogus', 'moderator').should == nil
447 it 'should generate requests against the correct URIs' do
448 request = @client.generate_request(
449 :api_method => 'moderator.profiles.get',
450 :authenticated => false
452 request.to_env(Faraday.default_connection)[:url].should ===
453 'https://www.googleapis.com/moderator/v1/profiles/@me'
456 it 'should generate requests against the correct URIs' do
457 request = @client.generate_request(
458 :api_method => @moderator.profiles.get,
459 :authenticated => false
461 request.to_env(Faraday.default_connection)[:url].should ===
462 'https://www.googleapis.com/moderator/v1/profiles/@me'
465 it 'should not be able to execute requests without authorization' do
466 result = @client.execute(
467 'moderator.profiles.get',
471 {:authenticated => false}
473 result.response.status.should == 401
477 describe 'with the adsense API' do
479 @client.authorization = nil
480 @adsense = @client.discovered_api('adsense', 'v1')
483 it 'should correctly determine the discovery URI' do
484 @client.discovery_uri('adsense').should ===
485 'https://www.googleapis.com/discovery/v1/apis/adsense/v1/rest'
488 it 'should find APIs that are in the discovery document' do
489 @client.discovered_api('adsense').name.should == 'adsense'
490 @client.discovered_api('adsense').version.should == 'v1'
493 it 'should find methods that are in the discovery document' do
494 @client.discovered_method(
495 'adsense.reports.generate', 'adsense'
496 ).name.should == 'generate'
499 it 'should not find methods that are not in the discovery document' do
500 @client.discovered_method('adsense.bogus', 'adsense').should == nil
503 it 'should generate requests against the correct URIs' do
504 request = @client.generate_request(
505 :api_method => 'adsense.adclients.list',
506 :authenticated => false
508 request.to_env(Faraday.default_connection)[:url].should ===
509 'https://www.googleapis.com/adsense/v1/adclients'
512 it 'should generate requests against the correct URIs' do
513 request = @client.generate_request(
514 :api_method => @adsense.adclients.list,
515 :authenticated => false
517 request.to_env(Faraday.default_connection)[:url].should ===
518 'https://www.googleapis.com/adsense/v1/adclients'
521 it 'should not be able to execute requests without authorization' do
522 result = @client.execute(
523 :api_method => 'adsense.adclients.list',
524 :authenticated => false
526 result.response.status.should == 401
529 it 'should fail when validating missing required parameters' do
531 @client.generate_request(
532 :api_method => @adsense.reports.generate,
533 :authenticated => false
535 end).should raise_error(ArgumentError)
538 it 'should succeed when validating parameters in a correct call' do
540 @client.generate_request(
541 :api_method => @adsense.reports.generate,
543 'startDate' => '2000-01-01',
544 'endDate' => '2010-01-01',
545 'dimension' => 'DATE',
546 'metric' => 'PAGE_VIEWS'
548 :authenticated => false
550 end).should_not raise_error
553 it 'should fail when validating parameters with invalid values' do
555 @client.generate_request(
556 :api_method => @adsense.reports.generate,
558 'startDate' => '2000-01-01',
559 'endDate' => '2010-01-01',
560 'dimension' => 'BAD_CHARACTERS=-&*(£&',
561 'metric' => 'PAGE_VIEWS'
563 :authenticated => false
565 end).should raise_error(ArgumentError)
568 it 'should succeed when validating repeated parameters in a correct call' do
570 @client.generate_request(
571 :api_method => @adsense.reports.generate,
573 'startDate' => '2000-01-01',
574 'endDate' => '2010-01-01',
575 'dimension' => ['DATE', 'PRODUCT_CODE'],
576 'metric' => ['PAGE_VIEWS', 'CLICKS']
578 :authenticated => false
580 end).should_not raise_error
583 it 'should fail when validating incorrect repeated parameters' do
585 @client.generate_request(
586 :api_method => @adsense.reports.generate,
588 'startDate' => '2000-01-01',
589 'endDate' => '2010-01-01',
590 'dimension' => ['DATE', 'BAD_CHARACTERS=-&*(£&'],
591 'metric' => ['PAGE_VIEWS', 'CLICKS']
593 :authenticated => false
595 end).should raise_error(ArgumentError)
599 describe 'with the Drive API' do
601 @client.authorization = nil
602 @drive = @client.discovered_api('drive', 'v1')
605 it 'should include media upload info methods' do
606 @drive.files.insert.media_upload.should_not == nil
609 it 'should include accepted media types' do
610 @drive.files.insert.media_upload.accepted_types.should_not be_empty
613 it 'should have an upload path' do
614 @drive.files.insert.media_upload.uri_template.should_not == nil
617 it 'should have a max file size' do
618 @drive.files.insert.media_upload.max_size.should_not == nil