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'
|
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'
|
|
|
|
|
2014-09-17 15:03:33 +00:00
|
|
|
RSpec.describe Google::APIClient do
|
2012-09-24 23:09:17 +00:00
|
|
|
include ConnectionHelpers
|
2012-12-30 19:27:45 +00:00
|
|
|
CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') 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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(lambda do
|
2012-12-30 19:27:45 +00:00
|
|
|
Google::APIClient.new(:application_name => 'API Client Tests', :authorization => 42)
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to 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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(lambda do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovery_document('bogus')
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to raise_error(Google::APIClient::TransmissionError)
|
|
|
|
expect(lambda do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_api('bogus')
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to raise_error(Google::APIClient::TransmissionError)
|
2010-09-30 21:33:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should raise an error for bogus services' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(lambda do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_api(42)
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to raise_error(TypeError)
|
2010-09-30 21:33:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should raise an error for bogus services' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(lambda do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.preferred_version(42)
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to 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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(lambda do
|
2012-09-24 23:09:17 +00:00
|
|
|
CLIENT.execute(42)
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to raise_error(TypeError)
|
2010-09-30 21:33:23 +00:00
|
|
|
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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.preferred_version('bogus')).to eq(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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.discovery_uri('prediction')).to be ===
|
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'
|
2013-10-04 13:46:14 +00:00
|
|
|
|
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|
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2012-09-24 23:09:17 +00:00
|
|
|
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|
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2012-09-24 23:09:17 +00:00
|
|
|
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|
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2012-09-24 23:09:17 +00:00
|
|
|
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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.discovered_api('prediction', 'v1.2').name).to eq('prediction')
|
|
|
|
expect(CLIENT.discovered_api('prediction', 'v1.2').version).to eq('v1.2')
|
|
|
|
expect(CLIENT.discovered_api(:prediction, 'v1.2').name).to eq('prediction')
|
|
|
|
expect(CLIENT.discovered_api(:prediction, 'v1.2').version).to eq('v1.2')
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should discover methods' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.discovered_method(
|
2011-07-29 22:07:04 +00:00
|
|
|
'prediction.training.insert', 'prediction', 'v1.2'
|
2014-08-14 15:54:16 +00:00
|
|
|
).name).to eq('insert')
|
|
|
|
expect(CLIENT.discovered_method(
|
2011-07-29 22:07:04 +00:00
|
|
|
:'prediction.training.insert', :prediction, 'v1.2'
|
2014-08-14 15:54:16 +00:00
|
|
|
).name).to eq('insert')
|
|
|
|
expect(CLIENT.discovered_method(
|
2011-07-29 22:07:04 +00:00
|
|
|
'prediction.training.delete', 'prediction', 'v1.2'
|
2014-08-14 15:54:16 +00:00
|
|
|
).name).to eq('delete')
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
2012-05-16 19:05:24 +00:00
|
|
|
it 'should define the origin API in discovered methods' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.discovered_method(
|
2012-05-16 19:05:24 +00:00
|
|
|
'prediction.training.insert', 'prediction', 'v1.2'
|
2014-08-14 15:54:16 +00:00
|
|
|
).api.name).to eq('prediction')
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should not find methods that are not in the discovery document' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.discovered_method(
|
2011-07-29 22:07:04 +00:00
|
|
|
'prediction.bogus', 'prediction', 'v1.2'
|
2014-08-14 15:54:16 +00:00
|
|
|
)).to eq(nil)
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should raise an error for bogus methods' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(lambda do
|
2012-07-31 20:56:13 +00:00
|
|
|
CLIENT.discovered_method(42, 'prediction', 'v1.2')
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to raise_error(TypeError)
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should raise an error for bogus methods' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(lambda do
|
2012-09-24 23:09:17 +00:00
|
|
|
CLIENT.execute(:api_method => CLIENT.discovered_api('prediction', 'v1.2'))
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to raise_error(TypeError)
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly determine the preferred version' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.preferred_version('prediction').version).not_to eq('v1')
|
|
|
|
expect(CLIENT.preferred_version(:prediction).version).not_to eq('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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.discovered_api('prediction', 'v1.2').batch_path).not_to 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|
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(env[:body]).to eq('')
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2012-09-24 23:09:17 +00:00
|
|
|
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
|
|
|
|
2012-11-18 06:22:25 +00:00
|
|
|
it 'should generate valid requests when parameter value includes semicolon' do
|
|
|
|
conn = stub_connection do |stub|
|
2013-10-04 13:46:14 +00:00
|
|
|
# semicolon (;) in parameter value was being converted to
|
|
|
|
# bare ampersand (&) in 0.4.7. ensure that it gets converted
|
2012-11-18 06:22:25 +00:00
|
|
|
# to a CGI-escaped semicolon (%3B) instead.
|
|
|
|
stub.post('/prediction/v1.2/training?data=12345%3B67890') do |env|
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(env[:body]).to eq('')
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2012-11-18 06:22:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
request = CLIENT.execute(
|
|
|
|
:api_method => @prediction.training.insert,
|
|
|
|
:parameters => {'data' => '12345;67890'},
|
|
|
|
:connection => conn
|
|
|
|
)
|
|
|
|
conn.verify
|
|
|
|
end
|
|
|
|
|
2013-05-13 21:03:49 +00:00
|
|
|
it 'should generate valid requests when multivalued parameters are passed' do
|
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|
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(env.params['data']).to include('1', '2')
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2012-09-24 23:09:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
request = CLIENT.execute(
|
2011-12-01 14:04:40 +00:00
|
|
|
:api_method => @prediction.training.insert,
|
2013-05-13 21:03:49 +00:00
|
|
|
:parameters => {'data' => ['1', '2']},
|
2012-09-24 23:09:17 +00:00
|
|
|
: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|
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2012-09-24 23:09:17 +00:00
|
|
|
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|
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2012-09-24 23:09:17 +00:00
|
|
|
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 =
|
2012-12-30 19:27:45 +00:00
|
|
|
Google::APIClient.new(:application_name => 'API Client Tests').discovered_api('prediction', 'v1.2')
|
2012-07-31 20:56:13 +00:00
|
|
|
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|
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(env[:url].host).to eq('testing-domain.example.com')
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2012-09-24 23:09:17 +00:00
|
|
|
end
|
|
|
|
end
|
2013-10-04 13:46:14 +00:00
|
|
|
|
2012-09-24 23:09:17 +00:00
|
|
|
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|
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(env[:request_headers]).to have_key('Authorization')
|
|
|
|
expect(env[:request_headers]['Authorization']).to match(/^OAuth/)
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2012-09-24 23:09:17 +00:00
|
|
|
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|
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(env[:request_headers]).to have_key('Authorization')
|
|
|
|
expect(env[:request_headers]['Authorization']).to match(/^Bearer/)
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2012-09-24 23:09:17 +00:00
|
|
|
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'}
|
|
|
|
)
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(result.response.status).to eq(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'}
|
|
|
|
)
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(result.response.status).to eq(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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(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'}
|
|
|
|
)
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to raise_error(Google::APIClient::ClientError)
|
2011-07-29 22:07:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not be able to execute improperly authorized requests' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(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'}
|
|
|
|
)
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to raise_error(Google::APIClient::ClientError)
|
2011-07-29 22:07:04 +00:00
|
|
|
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|
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(env[:request_headers]).to have_key('Content-Type')
|
|
|
|
expect(env[:request_headers]['Content-Type']).to eq('application/json')
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2012-09-24 23:09:17 +00:00
|
|
|
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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.discovery_uri('plus')).to be ===
|
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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.discovered_api('plus').name).to eq('plus')
|
|
|
|
expect(CLIENT.discovered_api('plus').version).to eq('v1')
|
|
|
|
expect(CLIENT.discovered_api(:plus).name).to eq('plus')
|
|
|
|
expect(CLIENT.discovered_api(:plus).version).to eq('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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.discovered_method(
|
2011-11-16 13:40:42 +00:00
|
|
|
'plus.activities.list', 'plus'
|
2014-08-14 15:54:16 +00:00
|
|
|
).name).to eq('list')
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
2012-05-16 19:05:24 +00:00
|
|
|
it 'should define the origin API in discovered methods' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.discovered_method(
|
2012-05-16 19:05:24 +00:00
|
|
|
'plus.activities.list', 'plus'
|
2014-08-14 15:54:16 +00:00
|
|
|
).api.name).to eq('plus')
|
2012-05-16 19:05:24 +00:00
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should not find methods that are not in the discovery document' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.discovered_method('plus.bogus', 'plus')).to eq(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|
|
2013-01-23 06:14:50 +00:00
|
|
|
stub.get('/plus/v1/people/107807692475771887386/activities/public') do |env|
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2012-09-24 23:09:17 +00:00
|
|
|
end
|
|
|
|
end
|
2013-10-04 13:46:14 +00:00
|
|
|
|
2012-09-24 23:09:17 +00:00
|
|
|
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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(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
|
|
|
)
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to raise_error(ArgumentError)
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly validate parameters' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(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
|
2013-05-13 21:03:49 +00:00
|
|
|
).to_env(CLIENT.connection)
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to raise_error(ArgumentError)
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
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
|
2013-09-04 17:23:13 +00:00
|
|
|
@adsense = CLIENT.discovered_api('adsense', 'v1.3')
|
2012-05-11 09:48:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should correctly determine the discovery URI' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.discovery_uri('adsense', 'v1.3').to_s).to be ===
|
2013-09-04 17:23:13 +00:00
|
|
|
'https://www.googleapis.com/discovery/v1/apis/adsense/v1.3/rest'
|
2012-05-11 09:48:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should find APIs that are in the discovery document' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.discovered_api('adsense', 'v1.3').name).to eq('adsense')
|
|
|
|
expect(CLIENT.discovered_api('adsense', 'v1.3').version).to eq('v1.3')
|
2012-05-11 09:48:30 +00:00
|
|
|
end
|
|
|
|
|
2012-05-16 19:05:24 +00:00
|
|
|
it 'should return a batch path' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.discovered_api('adsense', 'v1.3').batch_path).not_to 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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.discovered_method(
|
2013-10-04 13:46:14 +00:00
|
|
|
'adsense.reports.generate', 'adsense', 'v1.3'
|
2014-08-14 15:54:16 +00:00
|
|
|
).name).to eq('generate')
|
2012-05-11 09:48:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not find methods that are not in the discovery document' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(CLIENT.discovered_method('adsense.bogus', 'adsense', 'v1.3')).to eq(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|
|
2013-09-04 17:23:13 +00:00
|
|
|
stub.get('/adsense/v1.3/adclients') do |env|
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2012-09-24 23:09:17 +00:00
|
|
|
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
|
|
|
|
)
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(result.response.status).to eq(401)
|
2012-05-11 09:48:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should fail when validating missing required parameters' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(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
|
|
|
)
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to raise_error(ArgumentError)
|
2012-05-11 09:48:30 +00:00
|
|
|
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|
|
2013-09-04 17:23:13 +00:00
|
|
|
stub.get('/adsense/v1.3/reports?dimension=DATE&endDate=2010-01-01&metric=PAGE_VIEWS&startDate=2000-01-01') do |env|
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2012-09-24 23:09:17 +00:00
|
|
|
end
|
|
|
|
end
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(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
|
|
|
|
)
|
2014-08-14 15:54:16 +00:00
|
|
|
end).not_to 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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(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
|
|
|
)
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to raise_error(ArgumentError)
|
2012-05-11 09:48:30 +00:00
|
|
|
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|
|
2013-09-04 17:23:13 +00:00
|
|
|
stub.get('/adsense/v1.3/reports?dimension=DATE&dimension=PRODUCT_CODE'+
|
2013-02-15 22:25:25 +00:00
|
|
|
'&endDate=2010-01-01&metric=CLICKS&metric=PAGE_VIEWS&'+
|
2012-09-24 23:09:17 +00:00
|
|
|
'startDate=2000-01-01') do |env|
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2012-09-24 23:09:17 +00:00
|
|
|
end
|
|
|
|
end
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(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
|
|
|
)
|
2014-08-14 15:54:16 +00:00
|
|
|
end).not_to 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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(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
|
|
|
)
|
2014-08-14 15:54:16 +00:00
|
|
|
end).to raise_error(ArgumentError)
|
2012-05-11 09:48:30 +00:00
|
|
|
end
|
2013-09-05 11:30:30 +00:00
|
|
|
|
|
|
|
it 'should generate valid requests when multivalued parameters are passed' do
|
|
|
|
conn = stub_connection do |stub|
|
|
|
|
stub.get('/adsense/v1.3/reports?dimension=DATE&dimension=PRODUCT_CODE'+
|
|
|
|
'&endDate=2010-01-01&metric=CLICKS&metric=PAGE_VIEWS&'+
|
|
|
|
'startDate=2000-01-01') do |env|
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(env.params['dimension']).to include('DATE', 'PRODUCT_CODE')
|
|
|
|
expect(env.params['metric']).to include('CLICKS', 'PAGE_VIEWS')
|
2014-01-22 21:54:06 +00:00
|
|
|
[200, {}, '{}']
|
2013-09-05 11:30:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
request = CLIENT.execute(
|
|
|
|
:api_method => @adsense.reports.generate,
|
|
|
|
:parameters => {
|
|
|
|
'startDate' => '2000-01-01',
|
|
|
|
'endDate' => '2010-01-01',
|
|
|
|
'dimension' => ['DATE', 'PRODUCT_CODE'],
|
|
|
|
'metric' => ['PAGE_VIEWS', 'CLICKS']
|
|
|
|
},
|
|
|
|
:authenticated => false,
|
|
|
|
:connection => conn
|
|
|
|
)
|
|
|
|
conn.verify
|
|
|
|
end
|
2012-05-11 09:48:30 +00:00
|
|
|
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
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@drive.files.insert.media_upload).not_to eq(nil)
|
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 accepted media types' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@drive.files.insert.media_upload.accepted_types).not_to be_empty
|
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 have an upload path' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@drive.files.insert.media_upload.uri_template).not_to eq(nil)
|
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 have a max file size' do
|
2014-08-14 15:54:16 +00:00
|
|
|
expect(@drive.files.insert.media_upload.max_size).not_to eq(nil)
|
2012-06-04 23:53:37 +00:00
|
|
|
end
|
|
|
|
end
|
2010-09-30 21:57:13 +00:00
|
|
|
end
|