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)
178 it 'should generate requests against the correct URIs' do
179 request = @client.generate_request(
180 :api_method => @prediction.training.insert,
181 :parameters => {'data' => '12345'}
183 method, uri, headers, body = request
185 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
188 it 'should generate requests against the correct URIs' do
189 request = @client.generate_request(
190 :api_method => @prediction.training.insert,
191 :parameters => {'data' => '12345'}
193 method, uri, headers, body = request
195 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
198 it 'should allow modification to the base URIs for testing purposes' do
199 prediction = @client.discovered_api('prediction', 'v1.2')
200 prediction.method_base =
201 'https://testing-domain.googleapis.com/prediction/v1.2/'
202 request = @client.generate_request(
203 :api_method => prediction.training.insert,
204 :parameters => {'data' => '123'}
206 method, uri, headers, body = request
208 'https://testing-domain.googleapis.com/' +
209 'prediction/v1.2/training?data=123'
213 it 'should generate OAuth 1 requests' do
214 @client.authorization = :oauth_1
215 @client.authorization.token_credential_key = '12345'
216 @client.authorization.token_credential_secret = '12345'
217 request = @client.generate_request(
218 :api_method => @prediction.training.insert,
219 :parameters => {'data' => '12345'}
221 method, uri, headers, body = request
222 headers = headers.inject({}) { |h,(k,v)| h[k]=v; h }
223 headers.keys.should include('Authorization')
224 headers['Authorization'].should =~ /^OAuth/
227 it 'should generate OAuth 2 requests' do
228 @client.authorization = :oauth_2
229 @client.authorization.access_token = '12345'
230 request = @client.generate_request(
231 :api_method => @prediction.training.insert,
232 :parameters => {'data' => '12345'}
234 method, uri, headers, body = request
235 headers = headers.inject({}) { |h,(k,v)| h[k]=v; h }
236 headers.keys.should include('Authorization')
237 headers['Authorization'].should =~ /^OAuth/
240 it 'should not be able to execute improperly authorized requests' do
241 @client.authorization = :oauth_1
242 @client.authorization.token_credential_key = '12345'
243 @client.authorization.token_credential_secret = '12345'
244 result = @client.execute(
245 @prediction.training.insert,
248 status, headers, body = result.response
252 it 'should not be able to execute improperly authorized requests' do
253 @client.authorization = :oauth_2
254 @client.authorization.access_token = '12345'
255 result = @client.execute(
256 @prediction.training.insert,
259 status, headers, body = result.response
263 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 end).should raise_error(Google::APIClient::ClientError)
275 it 'should not be able to execute improperly authorized requests' do
277 @client.authorization = :oauth_2
278 @client.authorization.access_token = '12345'
279 result = @client.execute!(
280 @prediction.training.insert,
283 end).should raise_error(Google::APIClient::ClientError)
286 it 'should correctly handle unnamed parameters' do
287 @client.authorization = :oauth_2
288 @client.authorization.access_token = '12345'
289 result = @client.execute(
290 @prediction.training.insert,
292 JSON.generate({"id" => "bucket/object"}),
293 {'Content-Type' => 'application/json'}
295 method, uri, headers, body = result.request
296 Hash[headers]['Content-Type'].should == 'application/json'
300 describe 'with the plus API' do
302 @client.authorization = nil
303 @plus = @client.discovered_api('plus')
306 it 'should correctly determine the discovery URI' do
307 @client.discovery_uri('plus').should ===
308 'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest'
311 it 'should find APIs that are in the discovery document' do
312 @client.discovered_api('plus').name.should == 'plus'
313 @client.discovered_api('plus').version.should == 'v1'
314 @client.discovered_api(:plus).name.should == 'plus'
315 @client.discovered_api(:plus).version.should == 'v1'
318 it 'should find methods that are in the discovery document' do
319 # TODO(bobaman) Fix this when the RPC names are correct
320 @client.discovered_method(
321 'plus.activities.list', 'plus'
322 ).name.should == 'list'
325 it 'should not find methods that are not in the discovery document' do
326 @client.discovered_method('plus.bogus', 'plus').should == nil
329 it 'should generate requests against the correct URIs' do
330 request = @client.generate_request(
331 :api_method => @plus.activities.list,
333 'userId' => '107807692475771887386', 'collection' => 'public'
335 :authenticated => false
337 method, uri, headers, body = request
339 'https://www.googleapis.com/plus/v1/' +
340 'people/107807692475771887386/activities/public'
344 it 'should correctly validate parameters' do
346 @client.generate_request(
347 :api_method => @plus.activities.list,
348 :parameters => {'alt' => 'json'},
349 :authenticated => false
351 end).should raise_error(ArgumentError)
354 it 'should correctly validate parameters' do
356 @client.generate_request(
357 :api_method => @plus.activities.list,
359 'userId' => '107807692475771887386', 'collection' => 'bogus'
361 :authenticated => false
363 end).should raise_error(ArgumentError)
367 describe 'with the latitude API' do
369 @client.authorization = nil
370 @latitude = @client.discovered_api('latitude')
373 it 'should correctly determine the discovery URI' do
374 @client.discovery_uri('latitude').should ===
375 'https://www.googleapis.com/discovery/v1/apis/latitude/v1/rest'
378 it 'should find APIs that are in the discovery document' do
379 @client.discovered_api('latitude').name.should == 'latitude'
380 @client.discovered_api('latitude').version.should == 'v1'
383 it 'should find methods that are in the discovery document' do
384 @client.discovered_method(
385 'latitude.currentLocation.get', 'latitude'
386 ).name.should == 'get'
389 it 'should not find methods that are not in the discovery document' do
390 @client.discovered_method('latitude.bogus', 'latitude').should == nil
393 it 'should generate requests against the correct URIs' do
394 request = @client.generate_request(
395 :api_method => 'latitude.currentLocation.get',
396 :authenticated => false
398 method, uri, headers, body = request
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 method, uri, headers, body = request
410 'https://www.googleapis.com/latitude/v1/currentLocation'
413 it 'should not be able to execute requests without authorization' do
414 result = @client.execute(
415 :api_method => 'latitude.currentLocation.get',
416 :authenticated => false
418 status, headers, body = result.response
423 describe 'with the moderator API' do
425 @client.authorization = nil
426 @moderator = @client.discovered_api('moderator')
429 it 'should correctly determine the discovery URI' do
430 @client.discovery_uri('moderator').should ===
431 'https://www.googleapis.com/discovery/v1/apis/moderator/v1/rest'
434 it 'should find APIs that are in the discovery document' do
435 @client.discovered_api('moderator').name.should == 'moderator'
436 @client.discovered_api('moderator').version.should == 'v1'
439 it 'should find methods that are in the discovery document' do
440 @client.discovered_method(
441 'moderator.profiles.get', 'moderator'
442 ).name.should == 'get'
445 it 'should not find methods that are not in the discovery document' do
446 @client.discovered_method('moderator.bogus', 'moderator').should == nil
449 it 'should generate requests against the correct URIs' do
450 request = @client.generate_request(
451 :api_method => 'moderator.profiles.get',
452 :authenticated => false
454 method, uri, headers, body = request
456 'https://www.googleapis.com/moderator/v1/profiles/@me'
459 it 'should generate requests against the correct URIs' do
460 request = @client.generate_request(
461 :api_method => @moderator.profiles.get,
462 :authenticated => false
464 method, uri, headers, body = request
466 'https://www.googleapis.com/moderator/v1/profiles/@me'
469 it 'should not be able to execute requests without authorization' do
470 result = @client.execute(
471 'moderator.profiles.get',
475 {:authenticated => false}
477 status, headers, body = result.response