2012-05-11 09:55:00 +00:00
|
|
|
# encoding:utf-8
|
|
|
|
|
2010-09-18 00:30:02 +00:00
|
|
|
# Copyright 2010 Google Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2012-05-11 09:55:00 +00:00
|
|
|
|
2010-09-18 00:30:02 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2012-01-27 14:57:57 +00:00
|
|
|
require 'faraday'
|
|
|
|
require 'faraday/utils'
|
2012-01-05 10:05:56 +00:00
|
|
|
require 'multi_json'
|
2012-08-30 09:15:59 +00:00
|
|
|
require 'compat/multi_json'
|
2010-09-18 00:30:02 +00:00
|
|
|
require 'signet/oauth_1/client'
|
|
|
|
require 'google/api_client'
|
|
|
|
require 'google/api_client/version'
|
|
|
|
|
2012-09-24 23:09:17 +00:00
|
|
|
def TestHandler
|
|
|
|
def initialize(&block)
|
|
|
|
@block = block
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
@block.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def mock_connection(&block)
|
|
|
|
connection = Faraday.new do |builder|
|
|
|
|
use TestHandler block
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
describe Google::APIClient do
|
2012-09-24 23:09:17 +00:00
|
|
|
include ConnectionHelpers
|
2012-10-10 16:59:55 +00:00
|
|
|
CLIENT = Google::APIClient.new unless defined?(CLIENT)
|
2012-07-31 20:56:13 +00:00
|
|
|
|
|
|
|
after do
|
|
|
|
# Reset client to not-quite-pristine state
|
|
|
|
CLIENT.key = nil
|
|
|
|
CLIENT.user_ip = nil
|
|
|
|
end
|
2010-09-28 23:09:07 +00:00
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should raise a type error for bogus authorization' do
|
2010-09-28 23:09:07 +00:00
|
|
|
(lambda do
|
2011-05-04 11:44:35 +00:00
|
|
|
Google::APIClient.new(:authorization => 42)
|
|
|
|
end).should raise_error(TypeError)
|
2010-09-28 23:09:07 +00:00
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should not be able to retrieve the discovery document for a bogus API' do
|
2010-09-28 23:09:07 +00:00
|
|
|
(lambda do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovery_document('bogus')
|
2010-09-28 23:09:07 +00:00
|
|
|
end).should raise_error(Google::APIClient::TransmissionError)
|
2010-09-30 21:33:23 +00:00
|
|
|
(lambda do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_api('bogus')
|
2011-05-04 11:44:35 +00:00
|
|
|
end).should raise_error(Google::APIClient::TransmissionError)
|
2010-09-30 21:33:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should raise an error for bogus services' do
|
|
|
|
(lambda do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_api(42)
|
2010-09-30 21:33:23 +00:00
|
|
|
end).should raise_error(TypeError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should raise an error for bogus services' do
|
|
|
|
(lambda do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.preferred_version(42)
|
2010-09-30 21:33:23 +00:00
|
|
|
end).should raise_error(TypeError)
|
2010-09-18 00:30:02 +00:00
|
|
|
end
|
|
|
|
|
2010-09-30 21:33:23 +00:00
|
|
|
it 'should raise an error for bogus methods' do
|
|
|
|
(lambda do
|
2012-09-24 23:09:17 +00:00
|
|
|
CLIENT.execute(42)
|
2010-09-30 21:33:23 +00:00
|
|
|
end).should raise_error(TypeError)
|
|
|
|
end
|
2010-10-09 02:41:38 +00:00
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should not return a preferred version for bogus service names' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.preferred_version('bogus').should == nil
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with the prediction API' do
|
|
|
|
before do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.authorization = nil
|
2011-07-29 22:07:04 +00:00
|
|
|
# The prediction API no longer exposes a v1, so we have to be
|
|
|
|
# careful about looking up the wrong API version.
|
2012-07-31 20:56:13 +00:00
|
|
|
@prediction = CLIENT.discovered_api('prediction', 'v1.2')
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly determine the discovery URI' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovery_uri('prediction').should ===
|
2011-05-12 20:14:15 +00:00
|
|
|
'https://www.googleapis.com/discovery/v1/apis/prediction/v1/rest'
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
2011-09-22 10:49:46 +00:00
|
|
|
it 'should correctly determine the discovery URI if :user_ip is set' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.user_ip = '127.0.0.1'
|
2012-09-24 23:09:17 +00:00
|
|
|
|
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.get('/discovery/v1/apis/prediction/v1.2/rest?userIp=127.0.0.1') do |env|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CLIENT.execute(
|
2011-09-22 10:49:46 +00:00
|
|
|
:http_method => 'GET',
|
2012-07-31 20:56:13 +00:00
|
|
|
:uri => CLIENT.discovery_uri('prediction', 'v1.2'),
|
2012-09-24 23:09:17 +00:00
|
|
|
:authenticated => false,
|
|
|
|
:connection => conn
|
2011-09-22 10:49:46 +00:00
|
|
|
)
|
2012-09-24 23:09:17 +00:00
|
|
|
conn.verify
|
2011-09-22 10:49:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly determine the discovery URI if :key is set' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.key = 'qwerty'
|
2012-09-24 23:09:17 +00:00
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.get('/discovery/v1/apis/prediction/v1.2/rest?key=qwerty') do |env|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
request = CLIENT.execute(
|
2011-09-22 10:49:46 +00:00
|
|
|
:http_method => 'GET',
|
2012-07-31 20:56:13 +00:00
|
|
|
:uri => CLIENT.discovery_uri('prediction', 'v1.2'),
|
2012-09-24 23:09:17 +00:00
|
|
|
:authenticated => false,
|
|
|
|
:connection => conn
|
|
|
|
)
|
|
|
|
conn.verify
|
2011-09-22 10:49:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly determine the discovery URI if both are set' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.key = 'qwerty'
|
|
|
|
CLIENT.user_ip = '127.0.0.1'
|
2012-09-24 23:09:17 +00:00
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.get('/discovery/v1/apis/prediction/v1.2/rest?key=qwerty&userIp=127.0.0.1') do |env|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
request = CLIENT.execute(
|
2011-09-22 10:49:46 +00:00
|
|
|
:http_method => 'GET',
|
2012-07-31 20:56:13 +00:00
|
|
|
:uri => CLIENT.discovery_uri('prediction', 'v1.2'),
|
2012-09-24 23:09:17 +00:00
|
|
|
:authenticated => false,
|
|
|
|
:connection => conn
|
|
|
|
)
|
|
|
|
conn.verify
|
2011-09-22 10:49:46 +00:00
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should correctly generate API objects' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_api('prediction', 'v1.2').name.should == 'prediction'
|
|
|
|
CLIENT.discovered_api('prediction', 'v1.2').version.should == 'v1.2'
|
|
|
|
CLIENT.discovered_api(:prediction, 'v1.2').name.should == 'prediction'
|
|
|
|
CLIENT.discovered_api(:prediction, 'v1.2').version.should == 'v1.2'
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should discover methods' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method(
|
2011-07-29 22:07:04 +00:00
|
|
|
'prediction.training.insert', 'prediction', 'v1.2'
|
2011-05-04 11:44:35 +00:00
|
|
|
).name.should == 'insert'
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method(
|
2011-07-29 22:07:04 +00:00
|
|
|
:'prediction.training.insert', :prediction, 'v1.2'
|
2011-05-04 11:44:35 +00:00
|
|
|
).name.should == 'insert'
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method(
|
2011-07-29 22:07:04 +00:00
|
|
|
'prediction.training.delete', 'prediction', 'v1.2'
|
2011-05-04 11:44:35 +00:00
|
|
|
).name.should == 'delete'
|
|
|
|
end
|
|
|
|
|
2012-05-16 19:05:24 +00:00
|
|
|
it 'should define the origin API in discovered methods' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method(
|
2012-05-16 19:05:24 +00:00
|
|
|
'prediction.training.insert', 'prediction', 'v1.2'
|
|
|
|
).api.name.should == 'prediction'
|
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should not find methods that are not in the discovery document' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method(
|
2011-07-29 22:07:04 +00:00
|
|
|
'prediction.bogus', 'prediction', 'v1.2'
|
2011-05-04 11:44:35 +00:00
|
|
|
).should == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should raise an error for bogus methods' do
|
|
|
|
(lambda do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method(42, 'prediction', 'v1.2')
|
2011-05-04 11:44:35 +00:00
|
|
|
end).should raise_error(TypeError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should raise an error for bogus methods' do
|
|
|
|
(lambda do
|
2012-09-24 23:09:17 +00:00
|
|
|
CLIENT.execute(:api_method => CLIENT.discovered_api('prediction', 'v1.2'))
|
2011-05-04 11:44:35 +00:00
|
|
|
end).should raise_error(TypeError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly determine the preferred version' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.preferred_version('prediction').version.should_not == 'v1'
|
|
|
|
CLIENT.preferred_version(:prediction).version.should_not == 'v1'
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
2012-05-16 19:05:24 +00:00
|
|
|
it 'should return a batch path' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_api('prediction', 'v1.2').batch_path.should_not be_nil
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should generate valid requests' do
|
2012-09-24 23:09:17 +00:00
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.post('/prediction/v1.2/training?data=12345') do |env|
|
|
|
|
env[:body].should == ''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
request = CLIENT.execute(
|
2011-07-29 22:07:04 +00:00
|
|
|
:api_method => @prediction.training.insert,
|
2012-09-24 23:09:17 +00:00
|
|
|
:parameters => {'data' => '12345'},
|
|
|
|
:connection => conn
|
2011-05-04 11:44:35 +00:00
|
|
|
)
|
2012-09-24 23:09:17 +00:00
|
|
|
conn.verify
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
2012-01-27 14:57:57 +00:00
|
|
|
|
2011-12-01 14:04:40 +00:00
|
|
|
it 'should generate valid requests when repeated parameters are passed' do
|
2012-10-10 16:47:02 +00:00
|
|
|
pending("This is caused by Faraday's encoding of query parameters.")
|
2012-09-24 23:09:17 +00:00
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.post('/prediction/v1.2/training?data=1&data=2') do |env|
|
|
|
|
env[:params]['data'].should include('1', '2')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
request = CLIENT.execute(
|
2011-12-01 14:04:40 +00:00
|
|
|
:api_method => @prediction.training.insert,
|
2012-09-24 23:09:17 +00:00
|
|
|
:parameters => [['data', '1'], ['data','2']],
|
|
|
|
:connection => conn
|
2011-12-01 14:04:40 +00:00
|
|
|
)
|
2012-09-24 23:09:17 +00:00
|
|
|
conn.verify
|
2011-12-01 14:04:40 +00:00
|
|
|
end
|
2012-01-27 14:57:57 +00:00
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should generate requests against the correct URIs' do
|
2012-09-24 23:09:17 +00:00
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.post('/prediction/v1.2/training?data=12345') do |env|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
request = CLIENT.execute(
|
2011-07-29 22:07:04 +00:00
|
|
|
:api_method => @prediction.training.insert,
|
2012-09-24 23:09:17 +00:00
|
|
|
:parameters => {'data' => '12345'},
|
|
|
|
:connection => conn
|
2011-05-04 11:44:35 +00:00
|
|
|
)
|
2012-09-24 23:09:17 +00:00
|
|
|
conn.verify
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should generate requests against the correct URIs' do
|
2012-09-24 23:09:17 +00:00
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.post('/prediction/v1.2/training?data=12345') do |env|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
request = CLIENT.execute(
|
2011-07-29 22:07:04 +00:00
|
|
|
:api_method => @prediction.training.insert,
|
2012-09-24 23:09:17 +00:00
|
|
|
:parameters => {'data' => '12345'},
|
|
|
|
:connection => conn
|
2011-05-04 11:44:35 +00:00
|
|
|
)
|
2012-09-24 23:09:17 +00:00
|
|
|
conn.verify
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should allow modification to the base URIs for testing purposes' do
|
2012-07-31 20:56:13 +00:00
|
|
|
# Using a new client instance here to avoid caching rebased discovery doc
|
|
|
|
prediction_rebase =
|
|
|
|
Google::APIClient.new.discovered_api('prediction', 'v1.2')
|
|
|
|
prediction_rebase.method_base =
|
|
|
|
'https://testing-domain.example.com/prediction/v1.2/'
|
2012-09-24 23:09:17 +00:00
|
|
|
|
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.post('/prediction/v1.2/training') do |env|
|
|
|
|
env[:url].host.should == 'testing-domain.example.com'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
request = CLIENT.execute(
|
2012-07-31 20:56:13 +00:00
|
|
|
:api_method => prediction_rebase.training.insert,
|
2012-09-24 23:09:17 +00:00
|
|
|
:parameters => {'data' => '123'},
|
|
|
|
:connection => conn
|
2011-07-29 22:07:04 +00:00
|
|
|
)
|
2012-09-24 23:09:17 +00:00
|
|
|
conn.verify
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should generate OAuth 1 requests' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.authorization = :oauth_1
|
|
|
|
CLIENT.authorization.token_credential_key = '12345'
|
|
|
|
CLIENT.authorization.token_credential_secret = '12345'
|
2012-09-24 23:09:17 +00:00
|
|
|
|
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.post('/prediction/v1.2/training?data=12345') do |env|
|
|
|
|
env[:request_headers].should have_key('Authorization')
|
|
|
|
env[:request_headers]['Authorization'].should =~ /^OAuth/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
request = CLIENT.execute(
|
2011-07-29 22:07:04 +00:00
|
|
|
:api_method => @prediction.training.insert,
|
2012-09-24 23:09:17 +00:00
|
|
|
:parameters => {'data' => '12345'},
|
|
|
|
:connection => conn
|
2011-05-04 11:44:35 +00:00
|
|
|
)
|
2012-09-24 23:09:17 +00:00
|
|
|
conn.verify
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should generate OAuth 2 requests' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.authorization = :oauth_2
|
|
|
|
CLIENT.authorization.access_token = '12345'
|
2012-09-24 23:09:17 +00:00
|
|
|
|
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.post('/prediction/v1.2/training?data=12345') do |env|
|
|
|
|
env[:request_headers].should have_key('Authorization')
|
|
|
|
env[:request_headers]['Authorization'].should =~ /^Bearer/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
request = CLIENT.execute(
|
2011-07-29 22:07:04 +00:00
|
|
|
:api_method => @prediction.training.insert,
|
2012-09-24 23:09:17 +00:00
|
|
|
:parameters => {'data' => '12345'},
|
|
|
|
:connection => conn
|
2011-05-04 11:44:35 +00:00
|
|
|
)
|
2012-09-24 23:09:17 +00:00
|
|
|
conn.verify
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not be able to execute improperly authorized requests' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.authorization = :oauth_1
|
|
|
|
CLIENT.authorization.token_credential_key = '12345'
|
|
|
|
CLIENT.authorization.token_credential_secret = '12345'
|
|
|
|
result = CLIENT.execute(
|
2011-07-29 22:07:04 +00:00
|
|
|
@prediction.training.insert,
|
2011-05-04 11:44:35 +00:00
|
|
|
{'data' => '12345'}
|
|
|
|
)
|
2012-01-27 14:57:57 +00:00
|
|
|
result.response.status.should == 401
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not be able to execute improperly authorized requests' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.authorization = :oauth_2
|
|
|
|
CLIENT.authorization.access_token = '12345'
|
|
|
|
result = CLIENT.execute(
|
2011-07-29 22:07:04 +00:00
|
|
|
@prediction.training.insert,
|
2011-05-04 11:44:35 +00:00
|
|
|
{'data' => '12345'}
|
|
|
|
)
|
2012-01-27 14:57:57 +00:00
|
|
|
result.response.status.should == 401
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
2011-07-29 22:07:04 +00:00
|
|
|
|
|
|
|
it 'should not be able to execute improperly authorized requests' do
|
|
|
|
(lambda do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.authorization = :oauth_1
|
|
|
|
CLIENT.authorization.token_credential_key = '12345'
|
|
|
|
CLIENT.authorization.token_credential_secret = '12345'
|
|
|
|
result = CLIENT.execute!(
|
2011-07-29 22:07:04 +00:00
|
|
|
@prediction.training.insert,
|
|
|
|
{'data' => '12345'}
|
|
|
|
)
|
|
|
|
end).should raise_error(Google::APIClient::ClientError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not be able to execute improperly authorized requests' do
|
|
|
|
(lambda do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.authorization = :oauth_2
|
|
|
|
CLIENT.authorization.access_token = '12345'
|
|
|
|
result = CLIENT.execute!(
|
2011-07-29 22:07:04 +00:00
|
|
|
@prediction.training.insert,
|
|
|
|
{'data' => '12345'}
|
|
|
|
)
|
|
|
|
end).should raise_error(Google::APIClient::ClientError)
|
|
|
|
end
|
2011-08-02 20:20:54 +00:00
|
|
|
|
|
|
|
it 'should correctly handle unnamed parameters' do
|
2012-09-24 23:09:17 +00:00
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.post('/prediction/v1.2/training') do |env|
|
|
|
|
env[:request_headers].should have_key('Content-Type')
|
|
|
|
env[:request_headers]['Content-Type'].should == 'application/json'
|
|
|
|
end
|
|
|
|
end
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.authorization = :oauth_2
|
|
|
|
CLIENT.authorization.access_token = '12345'
|
2012-09-24 23:09:17 +00:00
|
|
|
CLIENT.execute(
|
|
|
|
:api_method => @prediction.training.insert,
|
|
|
|
:body => MultiJson.dump({"id" => "bucket/object"}),
|
|
|
|
:headers => {'Content-Type' => 'application/json'},
|
|
|
|
:connection => conn
|
2011-08-02 20:20:54 +00:00
|
|
|
)
|
2012-09-24 23:09:17 +00:00
|
|
|
conn.verify
|
2011-08-02 20:20:54 +00:00
|
|
|
end
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
2011-11-16 13:40:42 +00:00
|
|
|
describe 'with the plus API' do
|
2011-05-04 11:44:35 +00:00
|
|
|
before do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.authorization = nil
|
|
|
|
@plus = CLIENT.discovered_api('plus')
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly determine the discovery URI' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovery_uri('plus').should ===
|
2011-11-16 13:40:42 +00:00
|
|
|
'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest'
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should find APIs that are in the discovery document' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_api('plus').name.should == 'plus'
|
|
|
|
CLIENT.discovered_api('plus').version.should == 'v1'
|
|
|
|
CLIENT.discovered_api(:plus).name.should == 'plus'
|
|
|
|
CLIENT.discovered_api(:plus).version.should == 'v1'
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should find methods that are in the discovery document' do
|
|
|
|
# TODO(bobaman) Fix this when the RPC names are correct
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method(
|
2011-11-16 13:40:42 +00:00
|
|
|
'plus.activities.list', 'plus'
|
2011-05-04 11:44:35 +00:00
|
|
|
).name.should == 'list'
|
|
|
|
end
|
|
|
|
|
2012-05-16 19:05:24 +00:00
|
|
|
it 'should define the origin API in discovered methods' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method(
|
2012-05-16 19:05:24 +00:00
|
|
|
'plus.activities.list', 'plus'
|
|
|
|
).api.name.should == 'plus'
|
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should not find methods that are not in the discovery document' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method('plus.bogus', 'plus').should == nil
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should generate requests against the correct URIs' do
|
2012-09-24 23:09:17 +00:00
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.get('/plus/v1/people/107807692475771887386/activities/public' +
|
|
|
|
'?collection=public&userId=107807692475771887386') do |env|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
request = CLIENT.execute(
|
2011-11-16 13:40:42 +00:00
|
|
|
:api_method => @plus.activities.list,
|
|
|
|
:parameters => {
|
|
|
|
'userId' => '107807692475771887386', 'collection' => 'public'
|
|
|
|
},
|
2012-09-24 23:09:17 +00:00
|
|
|
:authenticated => false,
|
|
|
|
:connection => conn
|
2011-11-16 13:40:42 +00:00
|
|
|
)
|
2012-09-24 23:09:17 +00:00
|
|
|
conn.verify
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly validate parameters' do
|
|
|
|
(lambda do
|
2012-09-24 23:09:17 +00:00
|
|
|
CLIENT.execute(
|
2011-11-16 13:40:42 +00:00
|
|
|
:api_method => @plus.activities.list,
|
2011-07-29 22:07:04 +00:00
|
|
|
:parameters => {'alt' => 'json'},
|
|
|
|
:authenticated => false
|
2012-09-24 23:09:17 +00:00
|
|
|
)
|
2011-05-04 11:44:35 +00:00
|
|
|
end).should raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly validate parameters' do
|
|
|
|
(lambda do
|
2012-09-24 23:09:17 +00:00
|
|
|
CLIENT.execute(
|
2011-11-16 13:40:42 +00:00
|
|
|
:api_method => @plus.activities.list,
|
|
|
|
:parameters => {
|
|
|
|
'userId' => '107807692475771887386', 'collection' => 'bogus'
|
|
|
|
},
|
2011-07-29 22:07:04 +00:00
|
|
|
:authenticated => false
|
2012-09-24 23:09:17 +00:00
|
|
|
).to_env(Faraday.default_connection)
|
2011-05-04 11:44:35 +00:00
|
|
|
end).should raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
2012-09-05 19:36:03 +00:00
|
|
|
|
|
|
|
=begin
|
2011-05-04 11:44:35 +00:00
|
|
|
describe 'with the latitude API' do
|
|
|
|
before do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.authorization = nil
|
|
|
|
@latitude = CLIENT.discovered_api('latitude')
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly determine the discovery URI' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovery_uri('latitude').should ===
|
2011-05-12 20:14:15 +00:00
|
|
|
'https://www.googleapis.com/discovery/v1/apis/latitude/v1/rest'
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should find APIs that are in the discovery document' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_api('latitude').name.should == 'latitude'
|
|
|
|
CLIENT.discovered_api('latitude').version.should == 'v1'
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
2012-05-16 19:05:24 +00:00
|
|
|
it 'should return a batch path' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_api('latitude').batch_path.should_not be_nil
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should find methods that are in the discovery document' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method(
|
2011-05-04 11:44:35 +00:00
|
|
|
'latitude.currentLocation.get', 'latitude'
|
|
|
|
).name.should == 'get'
|
|
|
|
end
|
|
|
|
|
2012-05-16 19:05:24 +00:00
|
|
|
it 'should define the origin API in discovered methods' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method(
|
2012-05-16 19:05:24 +00:00
|
|
|
'latitude.currentLocation.get', 'latitude'
|
|
|
|
).api.name.should == 'latitude'
|
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should not find methods that are not in the discovery document' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method('latitude.bogus', 'latitude').should == nil
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should generate requests against the correct URIs' do
|
2012-07-31 20:56:13 +00:00
|
|
|
request = CLIENT.generate_request(
|
2011-07-29 22:07:04 +00:00
|
|
|
:api_method => 'latitude.currentLocation.get',
|
|
|
|
:authenticated => false
|
2011-05-04 11:44:35 +00:00
|
|
|
)
|
2012-09-24 23:09:17 +00:00
|
|
|
request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
2011-05-04 11:44:35 +00:00
|
|
|
'https://www.googleapis.com/latitude/v1/currentLocation'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should generate requests against the correct URIs' do
|
2012-07-31 20:56:13 +00:00
|
|
|
request = CLIENT.generate_request(
|
2011-07-29 22:07:04 +00:00
|
|
|
:api_method => @latitude.current_location.get,
|
|
|
|
:authenticated => false
|
2011-05-04 11:44:35 +00:00
|
|
|
)
|
2012-09-24 23:09:17 +00:00
|
|
|
request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
2011-05-04 11:44:35 +00:00
|
|
|
'https://www.googleapis.com/latitude/v1/currentLocation'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not be able to execute requests without authorization' do
|
2012-07-31 20:56:13 +00:00
|
|
|
result = CLIENT.execute(
|
2011-07-29 22:07:04 +00:00
|
|
|
:api_method => 'latitude.currentLocation.get',
|
|
|
|
:authenticated => false
|
2011-05-04 11:44:35 +00:00
|
|
|
)
|
2012-01-27 14:57:57 +00:00
|
|
|
result.response.status.should == 401
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
end
|
2012-09-05 19:36:03 +00:00
|
|
|
=end
|
2011-05-04 11:44:35 +00:00
|
|
|
|
|
|
|
describe 'with the moderator API' do
|
|
|
|
before do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.authorization = nil
|
|
|
|
@moderator = CLIENT.discovered_api('moderator')
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly determine the discovery URI' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovery_uri('moderator').should ===
|
2011-05-12 20:14:15 +00:00
|
|
|
'https://www.googleapis.com/discovery/v1/apis/moderator/v1/rest'
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should find APIs that are in the discovery document' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_api('moderator').name.should == 'moderator'
|
|
|
|
CLIENT.discovered_api('moderator').version.should == 'v1'
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should find methods that are in the discovery document' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method(
|
2011-05-04 11:44:35 +00:00
|
|
|
'moderator.profiles.get', 'moderator'
|
|
|
|
).name.should == 'get'
|
|
|
|
end
|
|
|
|
|
2012-05-16 19:05:24 +00:00
|
|
|
it 'should define the origin API in discovered methods' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method(
|
2012-05-16 19:05:24 +00:00
|
|
|
'moderator.profiles.get', 'moderator'
|
|
|
|
).api.name.should == 'moderator'
|
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should not find methods that are not in the discovery document' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method('moderator.bogus', 'moderator').should == nil
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
2012-05-16 19:05:24 +00:00
|
|
|
it 'should return a batch path' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_api('moderator').batch_path.should_not be_nil
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should generate requests against the correct URIs' do
|
2012-09-24 23:09:17 +00:00
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.get('/moderator/v1/profiles/@me') do |env|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
request = CLIENT.execute(
|
2011-07-29 22:07:04 +00:00
|
|
|
:api_method => @moderator.profiles.get,
|
2012-09-24 23:09:17 +00:00
|
|
|
:authenticated => false,
|
|
|
|
:connection => conn
|
2011-05-04 11:44:35 +00:00
|
|
|
)
|
2012-09-24 23:09:17 +00:00
|
|
|
conn.verify
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not be able to execute requests without authorization' do
|
2012-07-31 20:56:13 +00:00
|
|
|
result = CLIENT.execute(
|
2012-09-28 19:07:11 +00:00
|
|
|
@moderator.profiles.get,
|
2011-05-04 11:44:35 +00:00
|
|
|
{},
|
|
|
|
'',
|
|
|
|
[],
|
2011-07-29 22:07:04 +00:00
|
|
|
{:authenticated => false}
|
2011-05-04 11:44:35 +00:00
|
|
|
)
|
2012-01-27 14:57:57 +00:00
|
|
|
result.response.status.should == 401
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
2010-09-30 21:57:13 +00:00
|
|
|
end
|
2012-05-11 09:48:30 +00:00
|
|
|
|
|
|
|
describe 'with the adsense API' do
|
|
|
|
before do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.authorization = nil
|
|
|
|
@adsense = CLIENT.discovered_api('adsense', 'v1')
|
2012-05-11 09:48:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly determine the discovery URI' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovery_uri('adsense').should ===
|
2012-05-11 09:48:30 +00:00
|
|
|
'https://www.googleapis.com/discovery/v1/apis/adsense/v1/rest'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should find APIs that are in the discovery document' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_api('adsense').name.should == 'adsense'
|
|
|
|
CLIENT.discovered_api('adsense').version.should == 'v1'
|
2012-05-11 09:48:30 +00:00
|
|
|
end
|
|
|
|
|
2012-05-16 19:05:24 +00:00
|
|
|
it 'should return a batch path' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_api('adsense').batch_path.should_not be_nil
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
|
2012-05-11 09:48:30 +00:00
|
|
|
it 'should find methods that are in the discovery document' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method(
|
2012-05-11 09:48:30 +00:00
|
|
|
'adsense.reports.generate', 'adsense'
|
|
|
|
).name.should == 'generate'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not find methods that are not in the discovery document' do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method('adsense.bogus', 'adsense').should == nil
|
2012-05-11 09:48:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should generate requests against the correct URIs' do
|
2012-09-24 23:09:17 +00:00
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.get('/adsense/v1/adclients') do |env|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
request = CLIENT.execute(
|
2012-05-11 09:48:30 +00:00
|
|
|
:api_method => @adsense.adclients.list,
|
2012-09-24 23:09:17 +00:00
|
|
|
:authenticated => false,
|
|
|
|
:connection => conn
|
2012-05-11 09:48:30 +00:00
|
|
|
)
|
2012-09-24 23:09:17 +00:00
|
|
|
conn.verify
|
2012-05-11 09:48:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not be able to execute requests without authorization' do
|
2012-07-31 20:56:13 +00:00
|
|
|
result = CLIENT.execute(
|
2012-09-28 19:07:11 +00:00
|
|
|
:api_method => @adsense.adclients.list,
|
2012-05-11 09:48:30 +00:00
|
|
|
:authenticated => false
|
|
|
|
)
|
|
|
|
result.response.status.should == 401
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should fail when validating missing required parameters' do
|
|
|
|
(lambda do
|
2012-09-24 23:09:17 +00:00
|
|
|
CLIENT.execute(
|
2012-05-11 09:48:30 +00:00
|
|
|
:api_method => @adsense.reports.generate,
|
|
|
|
:authenticated => false
|
2012-09-24 23:09:17 +00:00
|
|
|
)
|
2012-05-11 09:48:30 +00:00
|
|
|
end).should raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should succeed when validating parameters in a correct call' do
|
2012-09-24 23:09:17 +00:00
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.get('/adsense/v1/reports?dimension=DATE&endDate=2010-01-01&metric=PAGE_VIEWS&startDate=2000-01-01') do |env|
|
|
|
|
end
|
|
|
|
end
|
2012-05-11 09:48:30 +00:00
|
|
|
(lambda do
|
2012-09-24 23:09:17 +00:00
|
|
|
CLIENT.execute(
|
2012-05-11 09:48:30 +00:00
|
|
|
:api_method => @adsense.reports.generate,
|
|
|
|
:parameters => {
|
|
|
|
'startDate' => '2000-01-01',
|
|
|
|
'endDate' => '2010-01-01',
|
|
|
|
'dimension' => 'DATE',
|
|
|
|
'metric' => 'PAGE_VIEWS'
|
|
|
|
},
|
2012-09-24 23:09:17 +00:00
|
|
|
:authenticated => false,
|
|
|
|
:connection => conn
|
|
|
|
)
|
2012-05-11 09:48:30 +00:00
|
|
|
end).should_not raise_error
|
2012-09-24 23:09:17 +00:00
|
|
|
conn.verify
|
2012-05-11 09:48:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should fail when validating parameters with invalid values' do
|
|
|
|
(lambda do
|
2012-09-24 23:09:17 +00:00
|
|
|
CLIENT.execute(
|
2012-05-11 09:48:30 +00:00
|
|
|
:api_method => @adsense.reports.generate,
|
|
|
|
:parameters => {
|
|
|
|
'startDate' => '2000-01-01',
|
|
|
|
'endDate' => '2010-01-01',
|
|
|
|
'dimension' => 'BAD_CHARACTERS=-&*(£&',
|
|
|
|
'metric' => 'PAGE_VIEWS'
|
|
|
|
},
|
|
|
|
:authenticated => false
|
2012-09-24 23:09:17 +00:00
|
|
|
)
|
2012-05-11 09:48:30 +00:00
|
|
|
end).should raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should succeed when validating repeated parameters in a correct call' do
|
2012-09-24 23:09:17 +00:00
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.get('/adsense/v1/reports?dimension%5B%5D=DATE&dimension%5B%5D=PRODUCT_CODE'+
|
|
|
|
'&endDate=2010-01-01&metric%5B%5D=CLICKS&metric%5B%5D=PAGE_VIEWS&'+
|
|
|
|
'startDate=2000-01-01') do |env|
|
|
|
|
end
|
|
|
|
end
|
2012-05-11 09:48:30 +00:00
|
|
|
(lambda do
|
2012-09-24 23:09:17 +00:00
|
|
|
CLIENT.execute(
|
2012-05-11 09:48:30 +00:00
|
|
|
:api_method => @adsense.reports.generate,
|
|
|
|
:parameters => {
|
|
|
|
'startDate' => '2000-01-01',
|
|
|
|
'endDate' => '2010-01-01',
|
|
|
|
'dimension' => ['DATE', 'PRODUCT_CODE'],
|
|
|
|
'metric' => ['PAGE_VIEWS', 'CLICKS']
|
|
|
|
},
|
2012-09-24 23:09:17 +00:00
|
|
|
:authenticated => false,
|
|
|
|
:connection => conn
|
2012-05-11 09:48:30 +00:00
|
|
|
)
|
|
|
|
end).should_not raise_error
|
2012-09-24 23:09:17 +00:00
|
|
|
conn.verify
|
2012-05-11 09:48:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should fail when validating incorrect repeated parameters' do
|
|
|
|
(lambda do
|
2012-09-24 23:09:17 +00:00
|
|
|
CLIENT.execute(
|
2012-05-11 09:48:30 +00:00
|
|
|
:api_method => @adsense.reports.generate,
|
|
|
|
:parameters => {
|
|
|
|
'startDate' => '2000-01-01',
|
|
|
|
'endDate' => '2010-01-01',
|
|
|
|
'dimension' => ['DATE', 'BAD_CHARACTERS=-&*(£&'],
|
|
|
|
'metric' => ['PAGE_VIEWS', 'CLICKS']
|
|
|
|
},
|
|
|
|
:authenticated => false
|
2012-09-24 23:09:17 +00:00
|
|
|
)
|
2012-05-11 09:48:30 +00:00
|
|
|
end).should raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-04 23:53:37 +00:00
|
|
|
describe 'with the Drive API' do
|
|
|
|
before do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.authorization = nil
|
|
|
|
@drive = CLIENT.discovered_api('drive', 'v1')
|
2012-06-04 23:53:37 +00:00
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-04 23:53:37 +00:00
|
|
|
it 'should include media upload info methods' do
|
|
|
|
@drive.files.insert.media_upload.should_not == nil
|
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-04 23:53:37 +00:00
|
|
|
it 'should include accepted media types' do
|
|
|
|
@drive.files.insert.media_upload.accepted_types.should_not be_empty
|
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-04 23:53:37 +00:00
|
|
|
it 'should have an upload path' do
|
|
|
|
@drive.files.insert.media_upload.uri_template.should_not == nil
|
|
|
|
end
|
2012-07-31 20:15:45 +00:00
|
|
|
|
2012-06-04 23:53:37 +00:00
|
|
|
it 'should have a max file size' do
|
|
|
|
@drive.files.insert.media_upload.max_size.should_not == nil
|
|
|
|
end
|
|
|
|
end
|
2010-09-30 21:57:13 +00:00
|
|
|
end
|