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.
17 require 'signet/oauth_1/client'
18 require 'httpadapter/adapters/net_http'
20 require 'google/api_client'
21 require 'google/api_client/version'
22 require 'google/api_client/parsers/json_parser'
24 describe Google::APIClient do
26 @client = Google::APIClient.new
29 it 'should raise a type error for bogus authorization' do
31 Google::APIClient.new(:authorization => 42)
32 end).should raise_error(TypeError)
35 it 'should not be able to retrieve the discovery document for a bogus API' do
37 @client.discovery_document('bogus')
38 end).should raise_error(Google::APIClient::TransmissionError)
40 @client.discovered_api('bogus')
41 end).should raise_error(Google::APIClient::TransmissionError)
44 it 'should raise an error for bogus services' do
46 @client.discovered_api(42)
47 end).should raise_error(TypeError)
50 it 'should raise an error for bogus services' do
52 @client.preferred_version(42)
53 end).should raise_error(TypeError)
56 it 'should raise an error for bogus methods' do
58 @client.generate_request(42)
59 end).should raise_error(TypeError)
62 it 'should not return a preferred version for bogus service names' do
63 @client.preferred_version('bogus').should == nil
66 describe 'with the prediction API' do
68 @client.authorization = nil
69 # The prediction API no longer exposes a v1, so we have to be
70 # careful about looking up the wrong API version.
71 @prediction = @client.discovered_api('prediction', 'v1.2')
74 it 'should correctly determine the discovery URI' do
75 @client.discovery_uri('prediction').should ===
76 'https://www.googleapis.com/discovery/v1/apis/prediction/v1/rest'
79 it 'should correctly generate API objects' do
80 @client.discovered_api('prediction', 'v1.2').name.should == 'prediction'
81 @client.discovered_api('prediction', 'v1.2').version.should == 'v1.2'
82 @client.discovered_api(:prediction, 'v1.2').name.should == 'prediction'
83 @client.discovered_api(:prediction, 'v1.2').version.should == 'v1.2'
86 it 'should discover methods' do
87 @client.discovered_method(
88 'prediction.training.insert', 'prediction', 'v1.2'
89 ).name.should == 'insert'
90 @client.discovered_method(
91 :'prediction.training.insert', :prediction, 'v1.2'
92 ).name.should == 'insert'
93 @client.discovered_method(
94 'prediction.training.delete', 'prediction', 'v1.2'
95 ).name.should == 'delete'
98 it 'should not find methods that are not in the discovery document' do
99 @client.discovered_method(
100 'prediction.bogus', 'prediction', 'v1.2'
104 it 'should raise an error for bogus methods' do
106 @client.discovered_method(42, 'prediction', 'v1.2')
107 end).should raise_error(TypeError)
110 it 'should raise an error for bogus methods' do
112 @client.generate_request(@client.discovered_api('prediction', 'v1.2'))
113 end).should raise_error(TypeError)
116 it 'should correctly determine the preferred version' do
117 @client.preferred_version('prediction').version.should_not == 'v1'
118 @client.preferred_version(:prediction).version.should_not == 'v1'
121 it 'should generate valid requests' do
122 request = @client.generate_request(
123 :api_method => @prediction.training.insert,
124 :parameters => {'data' => '12345', }
126 method, uri, headers, body = request
127 method.should == 'POST'
129 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
130 (headers.inject({}) { |h,(k,v)| h[k]=v; h }).should == {}
131 body.should respond_to(:each)
134 it 'should generate requests against the correct URIs' do
135 request = @client.generate_request(
136 :api_method => @prediction.training.insert,
137 :parameters => {'data' => '12345'}
139 method, uri, headers, body = request
141 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
144 it 'should generate requests against the correct URIs' do
145 request = @client.generate_request(
146 :api_method => @prediction.training.insert,
147 :parameters => {'data' => '12345'}
149 method, uri, headers, body = request
151 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
154 it 'should allow modification to the base URIs for testing purposes' do
155 prediction = @client.discovered_api('prediction', 'v1.2')
156 prediction.method_base =
157 'https://testing-domain.googleapis.com/prediction/v1.2/'
158 request = @client.generate_request(
159 :api_method => prediction.training.insert,
160 :parameters => {'data' => '123'}
162 method, uri, headers, body = request
164 'https://testing-domain.googleapis.com/' +
165 'prediction/v1.2/training?data=123'
169 it 'should generate OAuth 1 requests' do
170 @client.authorization = :oauth_1
171 @client.authorization.token_credential_key = '12345'
172 @client.authorization.token_credential_secret = '12345'
173 request = @client.generate_request(
174 :api_method => @prediction.training.insert,
175 :parameters => {'data' => '12345'}
177 method, uri, headers, body = request
178 headers = headers.inject({}) { |h,(k,v)| h[k]=v; h }
179 headers.keys.should include('Authorization')
180 headers['Authorization'].should =~ /^OAuth/
183 it 'should generate OAuth 2 requests' do
184 @client.authorization = :oauth_2
185 @client.authorization.access_token = '12345'
186 request = @client.generate_request(
187 :api_method => @prediction.training.insert,
188 :parameters => {'data' => '12345'}
190 method, uri, headers, body = request
191 headers = headers.inject({}) { |h,(k,v)| h[k]=v; h }
192 headers.keys.should include('Authorization')
193 headers['Authorization'].should =~ /^OAuth/
196 it 'should not be able to execute improperly authorized requests' do
197 @client.authorization = :oauth_1
198 @client.authorization.token_credential_key = '12345'
199 @client.authorization.token_credential_secret = '12345'
200 result = @client.execute(
201 @prediction.training.insert,
204 status, headers, body = result.response
208 it 'should not be able to execute improperly authorized requests' do
209 @client.authorization = :oauth_2
210 @client.authorization.access_token = '12345'
211 result = @client.execute(
212 @prediction.training.insert,
215 status, headers, body = result.response
219 it 'should not be able to execute improperly authorized requests' do
221 @client.authorization = :oauth_1
222 @client.authorization.token_credential_key = '12345'
223 @client.authorization.token_credential_secret = '12345'
224 result = @client.execute!(
225 @prediction.training.insert,
228 end).should raise_error(Google::APIClient::ClientError)
231 it 'should not be able to execute improperly authorized requests' do
233 @client.authorization = :oauth_2
234 @client.authorization.access_token = '12345'
235 result = @client.execute!(
236 @prediction.training.insert,
239 end).should raise_error(Google::APIClient::ClientError)
243 describe 'with the buzz API' do
245 @client.authorization = nil
246 @buzz = @client.discovered_api('buzz')
249 it 'should correctly determine the discovery URI' do
250 @client.discovery_uri('buzz').should ===
251 'https://www.googleapis.com/discovery/v1/apis/buzz/v1/rest'
254 it 'should find APIs that are in the discovery document' do
255 @client.discovered_api('buzz').name.should == 'buzz'
256 @client.discovered_api('buzz').version.should == 'v1'
257 @client.discovered_api(:buzz).name.should == 'buzz'
258 @client.discovered_api(:buzz).version.should == 'v1'
261 it 'should find methods that are in the discovery document' do
262 # TODO(bobaman) Fix this when the RPC names are correct
263 @client.discovered_method(
264 'chili.activities.list', 'buzz'
265 ).name.should == 'list'
268 it 'should not find methods that are not in the discovery document' do
269 @client.discovered_method('buzz.bogus', 'buzz').should == nil
272 it 'should fail for string RPC names that do not match API name' do
274 @client.generate_request(
275 :api_method => 'chili.activities.list',
276 :parameters => {'alt' => 'json'},
277 :authenticated => false
279 end).should raise_error(Google::APIClient::TransmissionError)
282 it 'should generate requests against the correct URIs' do
283 request = @client.generate_request(
284 :api_method => @buzz.activities.list,
285 :parameters => {'userId' => 'hikingfan', 'scope' => '@public'},
286 :authenticated => false
288 method, uri, headers, body = request
290 'https://www.googleapis.com/buzz/v1/activities/hikingfan/@public'
293 it 'should correctly validate parameters' do
295 @client.generate_request(
296 :api_method => @buzz.activities.list,
297 :parameters => {'alt' => 'json'},
298 :authenticated => false
300 end).should raise_error(ArgumentError)
303 it 'should correctly validate parameters' do
305 @client.generate_request(
306 :api_method => @buzz.activities.list,
307 :parameters => {'userId' => 'hikingfan', 'scope' => '@bogus'},
308 :authenticated => false
310 end).should raise_error(ArgumentError)
313 it 'should be able to execute requests without authorization' do
314 result = @client.execute(
315 @buzz.activities.list,
316 {'alt' => 'json', 'userId' => 'hikingfan', 'scope' => '@public'},
319 :authenticated => false
321 status, headers, body = result.response
325 it 'should not be able to execute requests without authorization' do
326 result = @client.execute(
327 @buzz.activities.list,
328 'alt' => 'json', 'userId' => '@me', 'scope' => '@self'
330 status, headers, body = result.response
335 describe 'with the latitude API' do
337 @client.authorization = nil
338 @latitude = @client.discovered_api('latitude')
341 it 'should correctly determine the discovery URI' do
342 @client.discovery_uri('latitude').should ===
343 'https://www.googleapis.com/discovery/v1/apis/latitude/v1/rest'
346 it 'should find APIs that are in the discovery document' do
347 @client.discovered_api('latitude').name.should == 'latitude'
348 @client.discovered_api('latitude').version.should == 'v1'
351 it 'should find methods that are in the discovery document' do
352 @client.discovered_method(
353 'latitude.currentLocation.get', 'latitude'
354 ).name.should == 'get'
357 it 'should not find methods that are not in the discovery document' do
358 @client.discovered_method('latitude.bogus', 'latitude').should == nil
361 it 'should generate requests against the correct URIs' do
362 request = @client.generate_request(
363 :api_method => 'latitude.currentLocation.get',
364 :authenticated => false
366 method, uri, headers, body = request
368 'https://www.googleapis.com/latitude/v1/currentLocation'
371 it 'should generate requests against the correct URIs' do
372 request = @client.generate_request(
373 :api_method => @latitude.current_location.get,
374 :authenticated => false
376 method, uri, headers, body = request
378 'https://www.googleapis.com/latitude/v1/currentLocation'
381 it 'should not be able to execute requests without authorization' do
382 result = @client.execute(
383 :api_method => 'latitude.currentLocation.get',
384 :authenticated => false
386 status, headers, body = result.response
391 describe 'with the moderator API' do
393 @client.authorization = nil
394 @moderator = @client.discovered_api('moderator')
397 it 'should correctly determine the discovery URI' do
398 @client.discovery_uri('moderator').should ===
399 'https://www.googleapis.com/discovery/v1/apis/moderator/v1/rest'
402 it 'should find APIs that are in the discovery document' do
403 @client.discovered_api('moderator').name.should == 'moderator'
404 @client.discovered_api('moderator').version.should == 'v1'
407 it 'should find methods that are in the discovery document' do
408 @client.discovered_method(
409 'moderator.profiles.get', 'moderator'
410 ).name.should == 'get'
413 it 'should not find methods that are not in the discovery document' do
414 @client.discovered_method('moderator.bogus', 'moderator').should == nil
417 it 'should generate requests against the correct URIs' do
418 request = @client.generate_request(
419 :api_method => 'moderator.profiles.get',
420 :authenticated => false
422 method, uri, headers, body = request
424 'https://www.googleapis.com/moderator/v1/profiles/@me'
427 it 'should generate requests against the correct URIs' do
428 request = @client.generate_request(
429 :api_method => @moderator.profiles.get,
430 :authenticated => false
432 method, uri, headers, body = request
434 'https://www.googleapis.com/moderator/v1/profiles/@me'
437 it 'should not be able to execute requests without authorization' do
438 result = @client.execute(
439 'moderator.profiles.get',
443 {:authenticated => false}
445 status, headers, body = result.response