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, 'unconfigured' do
26 @client = Google::APIClient.new
29 it 'should not be able to determine the discovery URI' do
32 end).should raise_error(ArgumentError)
36 describe Google::APIClient, 'configured for a bogus API' do
38 @client = Google::APIClient.new(:service => 'bogus')
41 it 'should not be able to retrieve the discovery document' do
43 @client.discovery_document
44 end).should raise_error(Google::APIClient::TransmissionError)
48 describe Google::APIClient, 'configured for bogus authorization' do
49 it 'should raise a type error' do
51 Google::APIClient.new(:service => 'prediction', :authorization => 42)
52 end).should raise_error(TypeError)
56 describe Google::APIClient, 'configured for the prediction API' do
58 @client = Google::APIClient.new(:service => 'prediction')
61 it 'should correctly determine the discovery URI' do
62 @client.discovery_uri.should ===
63 'http://www.googleapis.com/discovery/0.1/describe?api=prediction'
66 it 'should have multiple versions available' do
67 @client.discovered_services.size.should > 1
70 it 'should find APIs that are in the discovery document' do
71 @client.discovered_service('prediction').name.should == 'prediction'
72 @client.discovered_service('prediction').version.should == 'v1'
73 @client.discovered_service(:prediction).name.should == 'prediction'
74 @client.discovered_service(:prediction).version.should == 'v1'
77 it 'should find API versions that are in the discovery document' do
78 @client.discovered_service('prediction', 'v1.1').version.should == 'v1.1'
81 it 'should not find APIs that are not in the discovery document' do
82 @client.discovered_service('bogus').should == nil
85 it 'should raise an error for bogus services' do
87 @client.discovered_service(42)
88 end).should raise_error(TypeError)
91 it 'should find methods that are in the discovery document' do
92 @client.discovered_method('prediction.training.insert').name.should ==
94 @client.discovered_method(:'prediction.training.insert').name.should ==
98 it 'should find methods for versions that are in the discovery document' do
99 @client.discovered_method(
100 'prediction.training.delete', 'v1.1'
104 it 'should not find methods that are not in the discovery document' do
105 @client.discovered_method('prediction.training.delete', 'v1').should == nil
106 @client.discovered_method('prediction.bogus').should == nil
109 it 'should raise an error for bogus methods' do
111 @client.discovered_method(42)
112 end).should raise_error(TypeError)
115 it 'should correctly determine the latest version' do
116 @client.latest_service_version('prediction').version.should_not == 'v1'
117 @client.latest_service_version(:prediction).version.should_not == 'v1'
120 it 'should raise an error for bogus services' do
122 @client.latest_service_version(42)
123 end).should raise_error(TypeError)
126 it 'should correctly determine the latest version' do
127 # Sanity check the algorithm
128 @client.discovered_services.clear
129 @client.discovered_services <<
130 Google::APIClient::Service.new('magic', 'v1', {})
131 @client.discovered_services <<
132 Google::APIClient::Service.new('magic', 'v1.1', {})
133 @client.discovered_services <<
134 Google::APIClient::Service.new('magic', 'v1.10', {})
135 @client.discovered_services <<
136 Google::APIClient::Service.new('magic', 'v10.1', {})
137 @client.discovered_services <<
138 Google::APIClient::Service.new('magic', 'v2.1', {})
139 @client.discovered_services <<
140 Google::APIClient::Service.new('magic', 'v10.0', {})
141 @client.latest_service_version('magic').version.should == 'v10.1'
144 it 'should correctly determine the latest version' do
145 # Sanity check the algorithm
146 @client.discovered_services.clear
147 @client.discovered_services <<
148 Google::APIClient::Service.new('one', 'v3', {})
149 @client.discovered_services <<
150 Google::APIClient::Service.new('two', 'v1', {})
151 @client.discovered_services <<
152 Google::APIClient::Service.new('two', 'v1.1-r1c3', {})
153 @client.discovered_services <<
154 Google::APIClient::Service.new('two', 'v2', {})
155 @client.discovered_services <<
156 Google::APIClient::Service.new('two', 'v2beta1', {})
157 @client.latest_service_version('two').version.should == 'v2'
160 it 'should return nil for bogus service names' do
161 # Sanity check the algorithm
162 @client.latest_service_version('bogus').should == nil
165 it 'should generate valid requests' do
166 request = @client.generate_request(
167 'prediction.training.insert',
170 method, uri, headers, body = request
171 method.should == 'POST'
173 'https://www.googleapis.com/prediction/v1/training?query=12345'
174 Hash[headers].should == {}
175 body.should respond_to(:each)
178 it 'should generate requests against the correct URIs' do
179 request = @client.generate_request(
180 :'prediction.training.insert',
183 method, uri, headers, body = request
185 'https://www.googleapis.com/prediction/v1/training?query=12345'
188 it 'should generate requests against the correct URIs' do
189 prediction = @client.discovered_service('prediction', 'v1')
190 request = @client.generate_request(
191 prediction.training.insert,
194 method, uri, headers, body = request
196 'https://www.googleapis.com/prediction/v1/training?query=12345'
199 it 'should generate signed requests' do
200 @client.authorization = :oauth_1
201 @client.authorization.token_credential_key = '12345'
202 @client.authorization.token_credential_secret = '12345'
203 request = @client.generate_request(
204 'prediction.training.insert',
207 method, uri, headers, body = request
208 headers = Hash[headers]
209 headers.keys.should include('Authorization')
210 headers['Authorization'].should =~ /^OAuth/
213 it 'should not be able to execute improperly authorized requests' do
214 @client.authorization = :oauth_1
215 @client.authorization.token_credential_key = '12345'
216 @client.authorization.token_credential_secret = '12345'
217 response = @client.execute(
218 'prediction.training.insert',
221 status, headers, body = response
225 it 'should raise an error for bogus methods' do
227 @client.generate_request(42)
228 end).should raise_error(TypeError)
231 it 'should raise an error for bogus methods' do
233 @client.generate_request(@client.discovered_service('prediction'))
234 end).should raise_error(TypeError)
238 describe Google::APIClient, 'configured for the buzz API' do
240 @client = Google::APIClient.new(:service => 'buzz')
243 it 'should correctly determine the discovery URI' do
244 @client.discovery_uri.should ===
245 'http://www.googleapis.com/discovery/0.1/describe?api=buzz'
248 it 'should find APIs that are in the discovery document' do
249 @client.discovered_service('buzz').name.should == 'buzz'
250 @client.discovered_service('buzz').version.should == 'v1'
253 it 'should not find APIs that are not in the discovery document' do
254 @client.discovered_service('bogus').should == nil
257 it 'should find methods that are in the discovery document' do
258 # TODO(bobaman) Fix this when the RPC names are correct
259 @client.discovered_method('chili.activities.list').name.should == 'list'
262 it 'should not find methods that are not in the discovery document' do
263 @client.discovered_method('buzz.bogus').should == nil
266 it 'should generate requests against the correct URIs' do
267 # TODO(bobaman) Fix this when the RPC names are correct
268 request = @client.generate_request(
269 'chili.activities.list',
270 {'userId' => 'hikingfan', 'scope' => '@public'},
275 method, uri, headers, body = request
277 'https://www.googleapis.com/buzz/v1/activities/hikingfan/@public'
280 it 'should correctly validate parameters' do
281 # TODO(bobaman) Fix this when the RPC names are correct
283 @client.generate_request(
284 'chili.activities.list',
290 end).should raise_error(ArgumentError)
293 it 'should correctly validate parameters' do
294 # TODO(bobaman) Fix this when the RPC names are correct
296 @client.generate_request(
297 'chili.activities.list',
298 {'userId' => 'hikingfan', 'scope' => '@bogus'},
303 end).should raise_error(ArgumentError)
306 it 'should be able to execute requests without authorization' do
307 # TODO(bobaman) Fix this when the RPC names are correct
308 response = @client.execute(
309 'chili.activities.list',
310 {'alt' => 'json', 'userId' => 'hikingfan', 'scope' => '@public'},
315 status, headers, body = response
320 describe Google::APIClient, 'configured for the latitude API' do
322 @client = Google::APIClient.new(:service => 'latitude')
325 it 'should correctly determine the discovery URI' do
326 @client.discovery_uri.should ===
327 'http://www.googleapis.com/discovery/0.1/describe?api=latitude'
330 it 'should find APIs that are in the discovery document' do
331 @client.discovered_service('latitude').name.should == 'latitude'
332 @client.discovered_service('latitude').version.should == 'v1'
335 it 'should not find APIs that are not in the discovery document' do
336 @client.discovered_service('bogus').should == nil
339 it 'should find methods that are in the discovery document' do
340 @client.discovered_method('latitude.currentLocation.get').name.should ==
344 it 'should not find methods that are not in the discovery document' do
345 @client.discovered_method('latitude.bogus').should == nil
348 it 'should generate requests against the correct URIs' do
349 request = @client.generate_request(
350 'latitude.currentLocation.get',
356 method, uri, headers, body = request
358 'https://www.googleapis.com/latitude/v1/currentLocation'
361 it 'should not be able to execute requests without authorization' do
362 response = @client.execute(
363 'latitude.currentLocation.get',
369 status, headers, body = response
374 describe Google::APIClient, 'configured for the moderator API' do
376 @client = Google::APIClient.new(:service => 'moderator')
379 it 'should correctly determine the discovery URI' do
380 @client.discovery_uri.should ===
381 'http://www.googleapis.com/discovery/0.1/describe?api=moderator'
384 it 'should find APIs that are in the discovery document' do
385 @client.discovered_service('moderator').name.should == 'moderator'
386 @client.discovered_service('moderator').version.should == 'v1'
389 it 'should not find APIs that are not in the discovery document' do
390 @client.discovered_service('bogus').should == nil
393 it 'should find methods that are in the discovery document' do
394 @client.discovered_method('moderator.profiles.get').name.should ==
398 it 'should not find methods that are not in the discovery document' do
399 @client.discovered_method('moderator.bogus').should == nil
402 it 'should generate requests against the correct URIs' do
403 request = @client.generate_request(
404 'moderator.profiles.get',
410 method, uri, headers, body = request
412 'https://www.googleapis.com/moderator/v1/profiles/@me'
415 it 'should not be able to execute requests without authorization' do
416 response = @client.execute(
417 'moderator.profiles.get',
423 status, headers, body = response