1 # Copyright 2010 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 require 'signet/oauth_1/client'
19 require 'httpadapter/adapters/net_http'
21 require 'google/api_client'
22 require 'google/api_client/version'
23 require 'google/api_client/parsers/json_parser'
25 describe Google::APIClient do
27 @client = Google::APIClient.new
30 it 'should raise a type error for bogus authorization' do
32 Google::APIClient.new(:authorization => 42)
33 end).should raise_error(TypeError)
36 it 'should not be able to retrieve the discovery document for a bogus API' do
38 @client.discovery_document('bogus')
39 end).should raise_error(Google::APIClient::TransmissionError)
41 @client.discovered_api('bogus')
42 end).should raise_error(Google::APIClient::TransmissionError)
45 it 'should raise an error for bogus services' do
47 @client.discovered_api(42)
48 end).should raise_error(TypeError)
51 it 'should raise an error for bogus services' do
53 @client.preferred_version(42)
54 end).should raise_error(TypeError)
57 it 'should raise an error for bogus methods' do
59 @client.generate_request(42)
60 end).should raise_error(TypeError)
63 it 'should not return a preferred version for bogus service names' do
64 @client.preferred_version('bogus').should == nil
67 describe 'with the prediction API' do
69 @client.authorization = nil
70 # The prediction API no longer exposes a v1, so we have to be
71 # careful about looking up the wrong API version.
72 @prediction = @client.discovered_api('prediction', 'v1.2')
75 it 'should correctly determine the discovery URI' do
76 @client.discovery_uri('prediction').should ===
77 'https://www.googleapis.com/discovery/v1/apis/prediction/v1/rest'
80 it 'should correctly determine the discovery URI if :user_ip is set' do
81 @client.user_ip = '127.0.0.1'
82 request = @client.generate_request(
83 :http_method => 'GET',
84 :uri => @client.discovery_uri('prediction', 'v1.2'),
85 :authenticated => false
87 http_method, uri, headers, body = request
89 'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
94 it 'should correctly determine the discovery URI if :key is set' do
95 @client.key = 'qwerty'
96 request = @client.generate_request(
97 :http_method => 'GET',
98 :uri => @client.discovery_uri('prediction', 'v1.2'),
99 :authenticated => false
101 http_method, uri, headers, body = request
103 'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
108 it 'should correctly determine the discovery URI if both are set' do
109 @client.key = 'qwerty'
110 @client.user_ip = '127.0.0.1'
111 request = @client.generate_request(
112 :http_method => 'GET',
113 :uri => @client.discovery_uri('prediction', 'v1.2'),
114 :authenticated => false
116 http_method, uri, headers, body = request
118 'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
119 '?key=qwerty&userIp=127.0.0.1'
123 it 'should correctly generate API objects' do
124 @client.discovered_api('prediction', 'v1.2').name.should == 'prediction'
125 @client.discovered_api('prediction', 'v1.2').version.should == 'v1.2'
126 @client.discovered_api(:prediction, 'v1.2').name.should == 'prediction'
127 @client.discovered_api(:prediction, 'v1.2').version.should == 'v1.2'
130 it 'should discover methods' do
131 @client.discovered_method(
132 'prediction.training.insert', 'prediction', 'v1.2'
133 ).name.should == 'insert'
134 @client.discovered_method(
135 :'prediction.training.insert', :prediction, 'v1.2'
136 ).name.should == 'insert'
137 @client.discovered_method(
138 'prediction.training.delete', 'prediction', 'v1.2'
139 ).name.should == 'delete'
142 it 'should not find methods that are not in the discovery document' do
143 @client.discovered_method(
144 'prediction.bogus', 'prediction', 'v1.2'
148 it 'should raise an error for bogus methods' do
150 @client.discovered_method(42, 'prediction', 'v1.2')
151 end).should raise_error(TypeError)
154 it 'should raise an error for bogus methods' do
156 @client.generate_request(@client.discovered_api('prediction', 'v1.2'))
157 end).should raise_error(TypeError)
160 it 'should correctly determine the preferred version' do
161 @client.preferred_version('prediction').version.should_not == 'v1'
162 @client.preferred_version(:prediction).version.should_not == 'v1'
165 it 'should generate valid requests' do
166 request = @client.generate_request(
167 :api_method => @prediction.training.insert,
168 :parameters => {'data' => '12345', }
170 method, uri, headers, body = request
171 method.should == 'POST'
173 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
174 (headers.inject({}) { |h,(k,v)| h[k]=v; h }).should == {}
175 body.should respond_to(:each)
177 it 'should generate valid requests when repeated parameters are passed' do
178 request = @client.generate_request(
179 :api_method => @prediction.training.insert,
180 :parameters => [['data', '1'],['data','2']]
182 method, uri, headers, body = request
183 method.should == 'POST'
185 'https://www.googleapis.com/prediction/v1.2/training?data=1&data=2'
187 it 'should generate requests against the correct URIs' do
188 request = @client.generate_request(
189 :api_method => @prediction.training.insert,
190 :parameters => {'data' => '12345'}
192 method, uri, headers, body = request
194 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
197 it 'should generate requests against the correct URIs' do
198 request = @client.generate_request(
199 :api_method => @prediction.training.insert,
200 :parameters => {'data' => '12345'}
202 method, uri, headers, body = request
204 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
207 it 'should allow modification to the base URIs for testing purposes' do
208 prediction = @client.discovered_api('prediction', 'v1.2')
209 prediction.method_base =
210 'https://testing-domain.googleapis.com/prediction/v1.2/'
211 request = @client.generate_request(
212 :api_method => prediction.training.insert,
213 :parameters => {'data' => '123'}
215 method, uri, headers, body = request
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 method, uri, headers, body = request
231 headers = headers.inject({}) { |h,(k,v)| h[k]=v; h }
232 headers.keys.should include('Authorization')
233 headers['Authorization'].should =~ /^OAuth/
236 it 'should generate OAuth 2 requests' do
237 @client.authorization = :oauth_2
238 @client.authorization.access_token = '12345'
239 request = @client.generate_request(
240 :api_method => @prediction.training.insert,
241 :parameters => {'data' => '12345'}
243 method, uri, headers, body = request
244 headers = headers.inject({}) { |h,(k,v)| h[k]=v; h }
245 headers.keys.should include('Authorization')
246 headers['Authorization'].should =~ /^OAuth/
249 it 'should not be able to execute improperly authorized requests' do
250 @client.authorization = :oauth_1
251 @client.authorization.token_credential_key = '12345'
252 @client.authorization.token_credential_secret = '12345'
253 result = @client.execute(
254 @prediction.training.insert,
257 status, headers, body = result.response
261 it 'should not be able to execute improperly authorized requests' do
262 @client.authorization = :oauth_2
263 @client.authorization.access_token = '12345'
264 result = @client.execute(
265 @prediction.training.insert,
268 status, headers, body = result.response
272 it 'should not be able to execute improperly authorized requests' do
274 @client.authorization = :oauth_1
275 @client.authorization.token_credential_key = '12345'
276 @client.authorization.token_credential_secret = '12345'
277 result = @client.execute!(
278 @prediction.training.insert,
281 end).should raise_error(Google::APIClient::ClientError)
284 it 'should not be able to execute improperly authorized requests' do
286 @client.authorization = :oauth_2
287 @client.authorization.access_token = '12345'
288 result = @client.execute!(
289 @prediction.training.insert,
292 end).should raise_error(Google::APIClient::ClientError)
295 it 'should correctly handle unnamed parameters' do
296 @client.authorization = :oauth_2
297 @client.authorization.access_token = '12345'
298 result = @client.execute(
299 @prediction.training.insert,
301 JSON.generate({"id" => "bucket/object"}),
302 {'Content-Type' => 'application/json'}
304 method, uri, headers, body = result.request
305 Hash[headers]['Content-Type'].should == 'application/json'
309 describe 'with the plus API' do
311 @client.authorization = nil
312 @plus = @client.discovered_api('plus')
315 it 'should correctly determine the discovery URI' do
316 @client.discovery_uri('plus').should ===
317 'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest'
320 it 'should find APIs that are in the discovery document' do
321 @client.discovered_api('plus').name.should == 'plus'
322 @client.discovered_api('plus').version.should == 'v1'
323 @client.discovered_api(:plus).name.should == 'plus'
324 @client.discovered_api(:plus).version.should == 'v1'
327 it 'should find methods that are in the discovery document' do
328 # TODO(bobaman) Fix this when the RPC names are correct
329 @client.discovered_method(
330 'plus.activities.list', 'plus'
331 ).name.should == 'list'
334 it 'should not find methods that are not in the discovery document' do
335 @client.discovered_method('plus.bogus', 'plus').should == nil
338 it 'should generate requests against the correct URIs' do
339 request = @client.generate_request(
340 :api_method => @plus.activities.list,
342 'userId' => '107807692475771887386', 'collection' => 'public'
344 :authenticated => false
346 method, uri, headers, body = request
348 'https://www.googleapis.com/plus/v1/' +
349 'people/107807692475771887386/activities/public'
353 it 'should correctly validate parameters' do
355 @client.generate_request(
356 :api_method => @plus.activities.list,
357 :parameters => {'alt' => 'json'},
358 :authenticated => false
360 end).should raise_error(ArgumentError)
363 it 'should correctly validate parameters' do
365 @client.generate_request(
366 :api_method => @plus.activities.list,
368 'userId' => '107807692475771887386', 'collection' => 'bogus'
370 :authenticated => false
372 end).should raise_error(ArgumentError)
376 describe 'with the latitude API' do
378 @client.authorization = nil
379 @latitude = @client.discovered_api('latitude')
382 it 'should correctly determine the discovery URI' do
383 @client.discovery_uri('latitude').should ===
384 'https://www.googleapis.com/discovery/v1/apis/latitude/v1/rest'
387 it 'should find APIs that are in the discovery document' do
388 @client.discovered_api('latitude').name.should == 'latitude'
389 @client.discovered_api('latitude').version.should == 'v1'
392 it 'should find methods that are in the discovery document' do
393 @client.discovered_method(
394 'latitude.currentLocation.get', 'latitude'
395 ).name.should == 'get'
398 it 'should not find methods that are not in the discovery document' do
399 @client.discovered_method('latitude.bogus', 'latitude').should == nil
402 it 'should generate requests against the correct URIs' do
403 request = @client.generate_request(
404 :api_method => 'latitude.currentLocation.get',
405 :authenticated => false
407 method, uri, headers, body = request
409 'https://www.googleapis.com/latitude/v1/currentLocation'
412 it 'should generate requests against the correct URIs' do
413 request = @client.generate_request(
414 :api_method => @latitude.current_location.get,
415 :authenticated => false
417 method, uri, headers, body = request
419 'https://www.googleapis.com/latitude/v1/currentLocation'
422 it 'should not be able to execute requests without authorization' do
423 result = @client.execute(
424 :api_method => 'latitude.currentLocation.get',
425 :authenticated => false
427 status, headers, body = result.response
432 describe 'with the moderator API' do
434 @client.authorization = nil
435 @moderator = @client.discovered_api('moderator')
438 it 'should correctly determine the discovery URI' do
439 @client.discovery_uri('moderator').should ===
440 'https://www.googleapis.com/discovery/v1/apis/moderator/v1/rest'
443 it 'should find APIs that are in the discovery document' do
444 @client.discovered_api('moderator').name.should == 'moderator'
445 @client.discovered_api('moderator').version.should == 'v1'
448 it 'should find methods that are in the discovery document' do
449 @client.discovered_method(
450 'moderator.profiles.get', 'moderator'
451 ).name.should == 'get'
454 it 'should not find methods that are not in the discovery document' do
455 @client.discovered_method('moderator.bogus', 'moderator').should == nil
458 it 'should generate requests against the correct URIs' do
459 request = @client.generate_request(
460 :api_method => 'moderator.profiles.get',
461 :authenticated => false
463 method, uri, headers, body = request
465 'https://www.googleapis.com/moderator/v1/profiles/@me'
468 it 'should generate requests against the correct URIs' do
469 request = @client.generate_request(
470 :api_method => @moderator.profiles.get,
471 :authenticated => false
473 method, uri, headers, body = request
475 'https://www.googleapis.com/moderator/v1/profiles/@me'
478 it 'should not be able to execute requests without authorization' do
479 result = @client.execute(
480 'moderator.profiles.get',
484 {:authenticated => false}
486 status, headers, body = result.response