Switched to using RSpec's let method for memoization.

This commit is contained in:
Bob Aman 2012-07-31 23:15:45 +03:00
parent 10856af027
commit e5e0118b50
6 changed files with 183 additions and 190 deletions

View File

@ -18,22 +18,20 @@ require 'google/api_client'
require 'google/api_client/version' require 'google/api_client/version'
describe Google::APIClient::BatchRequest do describe Google::APIClient::BatchRequest do
before do let(:client) { Google::APIClient.new }
@client = Google::APIClient.new
end
it 'should raise an error if making an empty batch request' do it 'should raise an error if making an empty batch request' do
batch = Google::APIClient::BatchRequest.new batch = Google::APIClient::BatchRequest.new
(lambda do (lambda do
@client.execute(batch) client.execute(batch)
end).should raise_error(Google::APIClient::BatchError) end).should raise_error(Google::APIClient::BatchError)
end end
describe 'with the discovery API' do describe 'with the discovery API' do
before do before do
@client.authorization = nil client.authorization = nil
@discovery = @client.discovered_api('discovery', 'v1') @discovery = client.discovered_api('discovery', 'v1')
end end
describe 'with two valid requests' do describe 'with two valid requests' do
@ -69,7 +67,7 @@ describe Google::APIClient::BatchRequest do
batch.add(@call1, ids[0]) batch.add(@call1, ids[0])
batch.add(@call2, ids[1]) batch.add(@call2, ids[1])
@client.execute(batch) client.execute(batch)
block_called.should == 2 block_called.should == 2
end end
@ -86,7 +84,7 @@ describe Google::APIClient::BatchRequest do
result.status.should == 200 result.status.should == 200
end end
@client.execute(batch) client.execute(batch)
call1_returned.should == true call1_returned.should == true
call2_returned.should == true call2_returned.should == true
end end
@ -139,7 +137,7 @@ describe Google::APIClient::BatchRequest do
batch.add(@call1, ids[0]) batch.add(@call1, ids[0])
batch.add(@call2, ids[1]) batch.add(@call2, ids[1])
@client.execute(batch) client.execute(batch)
block_called.should == 2 block_called.should == 2
end end
@ -157,7 +155,7 @@ describe Google::APIClient::BatchRequest do
result.status.should < 500 result.status.should < 500
end end
@client.execute(batch) client.execute(batch)
call1_returned.should == true call1_returned.should == true
call2_returned.should == true call2_returned.should == true
end end
@ -166,8 +164,8 @@ describe Google::APIClient::BatchRequest do
describe 'with the calendar API' do describe 'with the calendar API' do
before do before do
@client.authorization = nil client.authorization = nil
@calendar = @client.discovered_api('calendar', 'v3') @calendar = client.discovered_api('calendar', 'v3')
end end
describe 'with two valid requests' do describe 'with two valid requests' do

View File

@ -29,9 +29,7 @@ require 'google/api_client'
require 'google/api_client/version' require 'google/api_client/version'
describe Google::APIClient do describe Google::APIClient do
before do let(:client) { Google::APIClient.new }
@client = Google::APIClient.new
end
it 'should raise a type error for bogus authorization' do it 'should raise a type error for bogus authorization' do
(lambda do (lambda do
@ -41,53 +39,53 @@ describe Google::APIClient do
it 'should not be able to retrieve the discovery document for a bogus API' do it 'should not be able to retrieve the discovery document for a bogus API' do
(lambda do (lambda do
@client.discovery_document('bogus') client.discovery_document('bogus')
end).should raise_error(Google::APIClient::TransmissionError) end).should raise_error(Google::APIClient::TransmissionError)
(lambda do (lambda do
@client.discovered_api('bogus') client.discovered_api('bogus')
end).should raise_error(Google::APIClient::TransmissionError) end).should raise_error(Google::APIClient::TransmissionError)
end end
it 'should raise an error for bogus services' do it 'should raise an error for bogus services' do
(lambda do (lambda do
@client.discovered_api(42) client.discovered_api(42)
end).should raise_error(TypeError) end).should raise_error(TypeError)
end end
it 'should raise an error for bogus services' do it 'should raise an error for bogus services' do
(lambda do (lambda do
@client.preferred_version(42) client.preferred_version(42)
end).should raise_error(TypeError) end).should raise_error(TypeError)
end end
it 'should raise an error for bogus methods' do it 'should raise an error for bogus methods' do
(lambda do (lambda do
@client.generate_request(42) client.generate_request(42)
end).should raise_error(TypeError) end).should raise_error(TypeError)
end end
it 'should not return a preferred version for bogus service names' do it 'should not return a preferred version for bogus service names' do
@client.preferred_version('bogus').should == nil client.preferred_version('bogus').should == nil
end end
describe 'with the prediction API' do describe 'with the prediction API' do
before do before do
@client.authorization = nil client.authorization = nil
# The prediction API no longer exposes a v1, so we have to be # The prediction API no longer exposes a v1, so we have to be
# careful about looking up the wrong API version. # careful about looking up the wrong API version.
@prediction = @client.discovered_api('prediction', 'v1.2') @prediction = client.discovered_api('prediction', 'v1.2')
end end
it 'should correctly determine the discovery URI' do it 'should correctly determine the discovery URI' do
@client.discovery_uri('prediction').should === client.discovery_uri('prediction').should ===
'https://www.googleapis.com/discovery/v1/apis/prediction/v1/rest' 'https://www.googleapis.com/discovery/v1/apis/prediction/v1/rest'
end end
it 'should correctly determine the discovery URI if :user_ip is set' do it 'should correctly determine the discovery URI if :user_ip is set' do
@client.user_ip = '127.0.0.1' client.user_ip = '127.0.0.1'
request = @client.generate_request( request = client.generate_request(
:http_method => 'GET', :http_method => 'GET',
:uri => @client.discovery_uri('prediction', 'v1.2'), :uri => client.discovery_uri('prediction', 'v1.2'),
:authenticated => false :authenticated => false
) )
request.to_env(Faraday.default_connection)[:url].to_s.should === ( request.to_env(Faraday.default_connection)[:url].to_s.should === (
@ -97,10 +95,10 @@ describe Google::APIClient do
end end
it 'should correctly determine the discovery URI if :key is set' do it 'should correctly determine the discovery URI if :key is set' do
@client.key = 'qwerty' client.key = 'qwerty'
request = @client.generate_request( request = client.generate_request(
:http_method => 'GET', :http_method => 'GET',
:uri => @client.discovery_uri('prediction', 'v1.2'), :uri => client.discovery_uri('prediction', 'v1.2'),
:authenticated => false :authenticated => false
) )
request.to_env(Faraday.default_connection)[:url].to_s.should === ( request.to_env(Faraday.default_connection)[:url].to_s.should === (
@ -110,11 +108,11 @@ describe Google::APIClient do
end end
it 'should correctly determine the discovery URI if both are set' do it 'should correctly determine the discovery URI if both are set' do
@client.key = 'qwerty' client.key = 'qwerty'
@client.user_ip = '127.0.0.1' client.user_ip = '127.0.0.1'
request = @client.generate_request( request = client.generate_request(
:http_method => 'GET', :http_method => 'GET',
:uri => @client.discovery_uri('prediction', 'v1.2'), :uri => client.discovery_uri('prediction', 'v1.2'),
:authenticated => false :authenticated => false
) )
Addressable::URI.parse( Addressable::URI.parse(
@ -126,59 +124,59 @@ describe Google::APIClient do
end end
it 'should correctly generate API objects' do it 'should correctly generate API objects' do
@client.discovered_api('prediction', 'v1.2').name.should == 'prediction' 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').version.should == 'v1.2'
@client.discovered_api(:prediction, 'v1.2').name.should == 'prediction' 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').version.should == 'v1.2'
end end
it 'should discover methods' do it 'should discover methods' do
@client.discovered_method( client.discovered_method(
'prediction.training.insert', 'prediction', 'v1.2' 'prediction.training.insert', 'prediction', 'v1.2'
).name.should == 'insert' ).name.should == 'insert'
@client.discovered_method( client.discovered_method(
:'prediction.training.insert', :prediction, 'v1.2' :'prediction.training.insert', :prediction, 'v1.2'
).name.should == 'insert' ).name.should == 'insert'
@client.discovered_method( client.discovered_method(
'prediction.training.delete', 'prediction', 'v1.2' 'prediction.training.delete', 'prediction', 'v1.2'
).name.should == 'delete' ).name.should == 'delete'
end end
it 'should define the origin API in discovered methods' do it 'should define the origin API in discovered methods' do
@client.discovered_method( client.discovered_method(
'prediction.training.insert', 'prediction', 'v1.2' 'prediction.training.insert', 'prediction', 'v1.2'
).api.name.should == 'prediction' ).api.name.should == 'prediction'
end end
it 'should not find methods that are not in the discovery document' do it 'should not find methods that are not in the discovery document' do
@client.discovered_method( client.discovered_method(
'prediction.bogus', 'prediction', 'v1.2' 'prediction.bogus', 'prediction', 'v1.2'
).should == nil ).should == nil
end end
it 'should raise an error for bogus methods' do it 'should raise an error for bogus methods' do
(lambda do (lambda do
@client.discovered_method(42, 'prediction', 'v1.2') client.discovered_method(42, 'prediction', 'v1.2')
end).should raise_error(TypeError) end).should raise_error(TypeError)
end end
it 'should raise an error for bogus methods' do it 'should raise an error for bogus methods' do
(lambda do (lambda do
@client.generate_request(@client.discovered_api('prediction', 'v1.2')) client.generate_request(client.discovered_api('prediction', 'v1.2'))
end).should raise_error(TypeError) end).should raise_error(TypeError)
end end
it 'should correctly determine the preferred version' do it 'should correctly determine the preferred version' do
@client.preferred_version('prediction').version.should_not == 'v1' client.preferred_version('prediction').version.should_not == 'v1'
@client.preferred_version(:prediction).version.should_not == 'v1' client.preferred_version(:prediction).version.should_not == 'v1'
end end
it 'should return a batch path' do it 'should return a batch path' do
@client.discovered_api('prediction', 'v1.2').batch_path.should_not be_nil client.discovered_api('prediction', 'v1.2').batch_path.should_not be_nil
end end
it 'should generate valid requests' do it 'should generate valid requests' do
request = @client.generate_request( request = client.generate_request(
:api_method => @prediction.training.insert, :api_method => @prediction.training.insert,
:parameters => {'data' => '12345'} :parameters => {'data' => '12345'}
) )
@ -190,7 +188,7 @@ describe Google::APIClient do
end end
it 'should generate valid requests when repeated parameters are passed' do it 'should generate valid requests when repeated parameters are passed' do
request = @client.generate_request( request = client.generate_request(
:api_method => @prediction.training.insert, :api_method => @prediction.training.insert,
:parameters => [['data', '1'], ['data','2']] :parameters => [['data', '1'], ['data','2']]
) )
@ -200,7 +198,7 @@ describe Google::APIClient do
end end
it 'should generate requests against the correct URIs' do it 'should generate requests against the correct URIs' do
request = @client.generate_request( request = client.generate_request(
:api_method => @prediction.training.insert, :api_method => @prediction.training.insert,
:parameters => {'data' => '12345'} :parameters => {'data' => '12345'}
) )
@ -209,7 +207,7 @@ describe Google::APIClient do
end end
it 'should generate requests against the correct URIs' do it 'should generate requests against the correct URIs' do
request = @client.generate_request( request = client.generate_request(
:api_method => @prediction.training.insert, :api_method => @prediction.training.insert,
:parameters => {'data' => '12345'} :parameters => {'data' => '12345'}
) )
@ -218,10 +216,10 @@ describe Google::APIClient do
end end
it 'should allow modification to the base URIs for testing purposes' do it 'should allow modification to the base URIs for testing purposes' do
prediction = @client.discovered_api('prediction', 'v1.2') prediction = client.discovered_api('prediction', 'v1.2')
prediction.method_base = prediction.method_base =
'https://testing-domain.googleapis.com/prediction/v1.2/' 'https://testing-domain.googleapis.com/prediction/v1.2/'
request = @client.generate_request( request = client.generate_request(
:api_method => prediction.training.insert, :api_method => prediction.training.insert,
:parameters => {'data' => '123'} :parameters => {'data' => '123'}
) )
@ -232,10 +230,10 @@ describe Google::APIClient do
end end
it 'should generate OAuth 1 requests' do it 'should generate OAuth 1 requests' do
@client.authorization = :oauth_1 client.authorization = :oauth_1
@client.authorization.token_credential_key = '12345' client.authorization.token_credential_key = '12345'
@client.authorization.token_credential_secret = '12345' client.authorization.token_credential_secret = '12345'
request = @client.generate_request( request = client.generate_request(
:api_method => @prediction.training.insert, :api_method => @prediction.training.insert,
:parameters => {'data' => '12345'} :parameters => {'data' => '12345'}
) )
@ -244,9 +242,9 @@ describe Google::APIClient do
end end
it 'should generate OAuth 2 requests' do it 'should generate OAuth 2 requests' do
@client.authorization = :oauth_2 client.authorization = :oauth_2
@client.authorization.access_token = '12345' client.authorization.access_token = '12345'
request = @client.generate_request( request = client.generate_request(
:api_method => @prediction.training.insert, :api_method => @prediction.training.insert,
:parameters => {'data' => '12345'} :parameters => {'data' => '12345'}
) )
@ -255,10 +253,10 @@ describe Google::APIClient do
end end
it 'should not be able to execute improperly authorized requests' do it 'should not be able to execute improperly authorized requests' do
@client.authorization = :oauth_1 client.authorization = :oauth_1
@client.authorization.token_credential_key = '12345' client.authorization.token_credential_key = '12345'
@client.authorization.token_credential_secret = '12345' client.authorization.token_credential_secret = '12345'
result = @client.execute( result = client.execute(
@prediction.training.insert, @prediction.training.insert,
{'data' => '12345'} {'data' => '12345'}
) )
@ -266,9 +264,9 @@ describe Google::APIClient do
end end
it 'should not be able to execute improperly authorized requests' do it 'should not be able to execute improperly authorized requests' do
@client.authorization = :oauth_2 client.authorization = :oauth_2
@client.authorization.access_token = '12345' client.authorization.access_token = '12345'
result = @client.execute( result = client.execute(
@prediction.training.insert, @prediction.training.insert,
{'data' => '12345'} {'data' => '12345'}
) )
@ -277,10 +275,10 @@ describe Google::APIClient do
it 'should not be able to execute improperly authorized requests' do it 'should not be able to execute improperly authorized requests' do
(lambda do (lambda do
@client.authorization = :oauth_1 client.authorization = :oauth_1
@client.authorization.token_credential_key = '12345' client.authorization.token_credential_key = '12345'
@client.authorization.token_credential_secret = '12345' client.authorization.token_credential_secret = '12345'
result = @client.execute!( result = client.execute!(
@prediction.training.insert, @prediction.training.insert,
{'data' => '12345'} {'data' => '12345'}
) )
@ -289,9 +287,9 @@ describe Google::APIClient do
it 'should not be able to execute improperly authorized requests' do it 'should not be able to execute improperly authorized requests' do
(lambda do (lambda do
@client.authorization = :oauth_2 client.authorization = :oauth_2
@client.authorization.access_token = '12345' client.authorization.access_token = '12345'
result = @client.execute!( result = client.execute!(
@prediction.training.insert, @prediction.training.insert,
{'data' => '12345'} {'data' => '12345'}
) )
@ -299,9 +297,9 @@ describe Google::APIClient do
end end
it 'should correctly handle unnamed parameters' do it 'should correctly handle unnamed parameters' do
@client.authorization = :oauth_2 client.authorization = :oauth_2
@client.authorization.access_token = '12345' client.authorization.access_token = '12345'
result = @client.execute( result = client.execute(
@prediction.training.insert, @prediction.training.insert,
{}, {},
MultiJson.dump({"id" => "bucket/object"}), MultiJson.dump({"id" => "bucket/object"}),
@ -313,41 +311,41 @@ describe Google::APIClient do
describe 'with the plus API' do describe 'with the plus API' do
before do before do
@client.authorization = nil client.authorization = nil
@plus = @client.discovered_api('plus') @plus = client.discovered_api('plus')
end end
it 'should correctly determine the discovery URI' do it 'should correctly determine the discovery URI' do
@client.discovery_uri('plus').should === client.discovery_uri('plus').should ===
'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest' 'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest'
end end
it 'should find APIs that are in the discovery document' do it 'should find APIs that are in the discovery document' do
@client.discovered_api('plus').name.should == 'plus' client.discovered_api('plus').name.should == 'plus'
@client.discovered_api('plus').version.should == 'v1' client.discovered_api('plus').version.should == 'v1'
@client.discovered_api(:plus).name.should == 'plus' client.discovered_api(:plus).name.should == 'plus'
@client.discovered_api(:plus).version.should == 'v1' client.discovered_api(:plus).version.should == 'v1'
end end
it 'should find methods that are in the discovery document' do it 'should find methods that are in the discovery document' do
# TODO(bobaman) Fix this when the RPC names are correct # TODO(bobaman) Fix this when the RPC names are correct
@client.discovered_method( client.discovered_method(
'plus.activities.list', 'plus' 'plus.activities.list', 'plus'
).name.should == 'list' ).name.should == 'list'
end end
it 'should define the origin API in discovered methods' do it 'should define the origin API in discovered methods' do
@client.discovered_method( client.discovered_method(
'plus.activities.list', 'plus' 'plus.activities.list', 'plus'
).api.name.should == 'plus' ).api.name.should == 'plus'
end end
it 'should not find methods that are not in the discovery document' do it 'should not find methods that are not in the discovery document' do
@client.discovered_method('plus.bogus', 'plus').should == nil client.discovered_method('plus.bogus', 'plus').should == nil
end end
it 'should generate requests against the correct URIs' do it 'should generate requests against the correct URIs' do
request = @client.generate_request( request = client.generate_request(
:api_method => @plus.activities.list, :api_method => @plus.activities.list,
:parameters => { :parameters => {
'userId' => '107807692475771887386', 'collection' => 'public' 'userId' => '107807692475771887386', 'collection' => 'public'
@ -362,7 +360,7 @@ describe Google::APIClient do
it 'should correctly validate parameters' do it 'should correctly validate parameters' do
(lambda do (lambda do
@client.generate_request( client.generate_request(
:api_method => @plus.activities.list, :api_method => @plus.activities.list,
:parameters => {'alt' => 'json'}, :parameters => {'alt' => 'json'},
:authenticated => false :authenticated => false
@ -372,7 +370,7 @@ describe Google::APIClient do
it 'should correctly validate parameters' do it 'should correctly validate parameters' do
(lambda do (lambda do
@client.generate_request( client.generate_request(
:api_method => @plus.activities.list, :api_method => @plus.activities.list,
:parameters => { :parameters => {
'userId' => '107807692475771887386', 'collection' => 'bogus' 'userId' => '107807692475771887386', 'collection' => 'bogus'
@ -385,42 +383,42 @@ describe Google::APIClient do
describe 'with the latitude API' do describe 'with the latitude API' do
before do before do
@client.authorization = nil client.authorization = nil
@latitude = @client.discovered_api('latitude') @latitude = client.discovered_api('latitude')
end end
it 'should correctly determine the discovery URI' do it 'should correctly determine the discovery URI' do
@client.discovery_uri('latitude').should === client.discovery_uri('latitude').should ===
'https://www.googleapis.com/discovery/v1/apis/latitude/v1/rest' 'https://www.googleapis.com/discovery/v1/apis/latitude/v1/rest'
end end
it 'should find APIs that are in the discovery document' do it 'should find APIs that are in the discovery document' do
@client.discovered_api('latitude').name.should == 'latitude' client.discovered_api('latitude').name.should == 'latitude'
@client.discovered_api('latitude').version.should == 'v1' client.discovered_api('latitude').version.should == 'v1'
end end
it 'should return a batch path' do it 'should return a batch path' do
@client.discovered_api('latitude').batch_path.should_not be_nil client.discovered_api('latitude').batch_path.should_not be_nil
end end
it 'should find methods that are in the discovery document' do it 'should find methods that are in the discovery document' do
@client.discovered_method( client.discovered_method(
'latitude.currentLocation.get', 'latitude' 'latitude.currentLocation.get', 'latitude'
).name.should == 'get' ).name.should == 'get'
end end
it 'should define the origin API in discovered methods' do it 'should define the origin API in discovered methods' do
@client.discovered_method( client.discovered_method(
'latitude.currentLocation.get', 'latitude' 'latitude.currentLocation.get', 'latitude'
).api.name.should == 'latitude' ).api.name.should == 'latitude'
end end
it 'should not find methods that are not in the discovery document' do it 'should not find methods that are not in the discovery document' do
@client.discovered_method('latitude.bogus', 'latitude').should == nil client.discovered_method('latitude.bogus', 'latitude').should == nil
end end
it 'should generate requests against the correct URIs' do it 'should generate requests against the correct URIs' do
request = @client.generate_request( request = client.generate_request(
:api_method => 'latitude.currentLocation.get', :api_method => 'latitude.currentLocation.get',
:authenticated => false :authenticated => false
) )
@ -429,7 +427,7 @@ describe Google::APIClient do
end end
it 'should generate requests against the correct URIs' do it 'should generate requests against the correct URIs' do
request = @client.generate_request( request = client.generate_request(
:api_method => @latitude.current_location.get, :api_method => @latitude.current_location.get,
:authenticated => false :authenticated => false
) )
@ -438,7 +436,7 @@ describe Google::APIClient do
end end
it 'should not be able to execute requests without authorization' do it 'should not be able to execute requests without authorization' do
result = @client.execute( result = client.execute(
:api_method => 'latitude.currentLocation.get', :api_method => 'latitude.currentLocation.get',
:authenticated => false :authenticated => false
) )
@ -448,42 +446,42 @@ describe Google::APIClient do
describe 'with the moderator API' do describe 'with the moderator API' do
before do before do
@client.authorization = nil client.authorization = nil
@moderator = @client.discovered_api('moderator') @moderator = client.discovered_api('moderator')
end end
it 'should correctly determine the discovery URI' do it 'should correctly determine the discovery URI' do
@client.discovery_uri('moderator').should === client.discovery_uri('moderator').should ===
'https://www.googleapis.com/discovery/v1/apis/moderator/v1/rest' 'https://www.googleapis.com/discovery/v1/apis/moderator/v1/rest'
end end
it 'should find APIs that are in the discovery document' do it 'should find APIs that are in the discovery document' do
@client.discovered_api('moderator').name.should == 'moderator' client.discovered_api('moderator').name.should == 'moderator'
@client.discovered_api('moderator').version.should == 'v1' client.discovered_api('moderator').version.should == 'v1'
end end
it 'should find methods that are in the discovery document' do it 'should find methods that are in the discovery document' do
@client.discovered_method( client.discovered_method(
'moderator.profiles.get', 'moderator' 'moderator.profiles.get', 'moderator'
).name.should == 'get' ).name.should == 'get'
end end
it 'should define the origin API in discovered methods' do it 'should define the origin API in discovered methods' do
@client.discovered_method( client.discovered_method(
'moderator.profiles.get', 'moderator' 'moderator.profiles.get', 'moderator'
).api.name.should == 'moderator' ).api.name.should == 'moderator'
end end
it 'should not find methods that are not in the discovery document' do it 'should not find methods that are not in the discovery document' do
@client.discovered_method('moderator.bogus', 'moderator').should == nil client.discovered_method('moderator.bogus', 'moderator').should == nil
end end
it 'should return a batch path' do it 'should return a batch path' do
@client.discovered_api('moderator').batch_path.should_not be_nil client.discovered_api('moderator').batch_path.should_not be_nil
end end
it 'should generate requests against the correct URIs' do it 'should generate requests against the correct URIs' do
request = @client.generate_request( request = client.generate_request(
:api_method => 'moderator.profiles.get', :api_method => 'moderator.profiles.get',
:authenticated => false :authenticated => false
) )
@ -492,7 +490,7 @@ describe Google::APIClient do
end end
it 'should generate requests against the correct URIs' do it 'should generate requests against the correct URIs' do
request = @client.generate_request( request = client.generate_request(
:api_method => @moderator.profiles.get, :api_method => @moderator.profiles.get,
:authenticated => false :authenticated => false
) )
@ -501,7 +499,7 @@ describe Google::APIClient do
end end
it 'should not be able to execute requests without authorization' do it 'should not be able to execute requests without authorization' do
result = @client.execute( result = client.execute(
'moderator.profiles.get', 'moderator.profiles.get',
{}, {},
'', '',
@ -514,36 +512,36 @@ describe Google::APIClient do
describe 'with the adsense API' do describe 'with the adsense API' do
before do before do
@client.authorization = nil client.authorization = nil
@adsense = @client.discovered_api('adsense', 'v1') @adsense = client.discovered_api('adsense', 'v1')
end end
it 'should correctly determine the discovery URI' do it 'should correctly determine the discovery URI' do
@client.discovery_uri('adsense').should === client.discovery_uri('adsense').should ===
'https://www.googleapis.com/discovery/v1/apis/adsense/v1/rest' 'https://www.googleapis.com/discovery/v1/apis/adsense/v1/rest'
end end
it 'should find APIs that are in the discovery document' do it 'should find APIs that are in the discovery document' do
@client.discovered_api('adsense').name.should == 'adsense' client.discovered_api('adsense').name.should == 'adsense'
@client.discovered_api('adsense').version.should == 'v1' client.discovered_api('adsense').version.should == 'v1'
end end
it 'should return a batch path' do it 'should return a batch path' do
@client.discovered_api('adsense').batch_path.should_not be_nil client.discovered_api('adsense').batch_path.should_not be_nil
end end
it 'should find methods that are in the discovery document' do it 'should find methods that are in the discovery document' do
@client.discovered_method( client.discovered_method(
'adsense.reports.generate', 'adsense' 'adsense.reports.generate', 'adsense'
).name.should == 'generate' ).name.should == 'generate'
end end
it 'should not find methods that are not in the discovery document' do it 'should not find methods that are not in the discovery document' do
@client.discovered_method('adsense.bogus', 'adsense').should == nil client.discovered_method('adsense.bogus', 'adsense').should == nil
end end
it 'should generate requests against the correct URIs' do it 'should generate requests against the correct URIs' do
request = @client.generate_request( request = client.generate_request(
:api_method => 'adsense.adclients.list', :api_method => 'adsense.adclients.list',
:authenticated => false :authenticated => false
) )
@ -552,7 +550,7 @@ describe Google::APIClient do
end end
it 'should generate requests against the correct URIs' do it 'should generate requests against the correct URIs' do
request = @client.generate_request( request = client.generate_request(
:api_method => @adsense.adclients.list, :api_method => @adsense.adclients.list,
:authenticated => false :authenticated => false
) )
@ -561,7 +559,7 @@ describe Google::APIClient do
end end
it 'should not be able to execute requests without authorization' do it 'should not be able to execute requests without authorization' do
result = @client.execute( result = client.execute(
:api_method => 'adsense.adclients.list', :api_method => 'adsense.adclients.list',
:authenticated => false :authenticated => false
) )
@ -570,7 +568,7 @@ describe Google::APIClient do
it 'should fail when validating missing required parameters' do it 'should fail when validating missing required parameters' do
(lambda do (lambda do
@client.generate_request( client.generate_request(
:api_method => @adsense.reports.generate, :api_method => @adsense.reports.generate,
:authenticated => false :authenticated => false
) )
@ -579,7 +577,7 @@ describe Google::APIClient do
it 'should succeed when validating parameters in a correct call' do it 'should succeed when validating parameters in a correct call' do
(lambda do (lambda do
@client.generate_request( client.generate_request(
:api_method => @adsense.reports.generate, :api_method => @adsense.reports.generate,
:parameters => { :parameters => {
'startDate' => '2000-01-01', 'startDate' => '2000-01-01',
@ -594,7 +592,7 @@ describe Google::APIClient do
it 'should fail when validating parameters with invalid values' do it 'should fail when validating parameters with invalid values' do
(lambda do (lambda do
@client.generate_request( client.generate_request(
:api_method => @adsense.reports.generate, :api_method => @adsense.reports.generate,
:parameters => { :parameters => {
'startDate' => '2000-01-01', 'startDate' => '2000-01-01',
@ -609,7 +607,7 @@ describe Google::APIClient do
it 'should succeed when validating repeated parameters in a correct call' do it 'should succeed when validating repeated parameters in a correct call' do
(lambda do (lambda do
@client.generate_request( client.generate_request(
:api_method => @adsense.reports.generate, :api_method => @adsense.reports.generate,
:parameters => { :parameters => {
'startDate' => '2000-01-01', 'startDate' => '2000-01-01',
@ -624,7 +622,7 @@ describe Google::APIClient do
it 'should fail when validating incorrect repeated parameters' do it 'should fail when validating incorrect repeated parameters' do
(lambda do (lambda do
@client.generate_request( client.generate_request(
:api_method => @adsense.reports.generate, :api_method => @adsense.reports.generate,
:parameters => { :parameters => {
'startDate' => '2000-01-01', 'startDate' => '2000-01-01',
@ -637,25 +635,25 @@ describe Google::APIClient do
end).should raise_error(ArgumentError) end).should raise_error(ArgumentError)
end end
end end
describe 'with the Drive API' do describe 'with the Drive API' do
before do before do
@client.authorization = nil client.authorization = nil
@drive = @client.discovered_api('drive', 'v1') @drive = client.discovered_api('drive', 'v1')
end end
it 'should include media upload info methods' do it 'should include media upload info methods' do
@drive.files.insert.media_upload.should_not == nil @drive.files.insert.media_upload.should_not == nil
end end
it 'should include accepted media types' do it 'should include accepted media types' do
@drive.files.insert.media_upload.accepted_types.should_not be_empty @drive.files.insert.media_upload.accepted_types.should_not be_empty
end end
it 'should have an upload path' do it 'should have an upload path' do
@drive.files.insert.media_upload.uri_template.should_not == nil @drive.files.insert.media_upload.uri_template.should_not == nil
end end
it 'should have a max file size' do it 'should have a max file size' do
@drive.files.insert.media_upload.max_size.should_not == nil @drive.files.insert.media_upload.max_size.should_not == nil
end end

View File

@ -25,22 +25,22 @@ describe Google::APIClient::UploadIO do
media = Google::APIClient::UploadIO.new('doesnotexist', 'text/plain') media = Google::APIClient::UploadIO.new('doesnotexist', 'text/plain')
end).should raise_error end).should raise_error
end end
describe 'with a file' do describe 'with a file' do
before do before do
@file = File.expand_path('files/sample.txt', fixtures_path) @file = File.expand_path('files/sample.txt', fixtures_path)
@media = Google::APIClient::UploadIO.new(@file, 'text/plain') @media = Google::APIClient::UploadIO.new(@file, 'text/plain')
end end
it 'should report the correct file length' do it 'should report the correct file length' do
@media.length.should == File.size(@file) @media.length.should == File.size(@file)
end end
it 'should have a mime type' do it 'should have a mime type' do
@media.content_type.should == 'text/plain' @media.content_type.should == 'text/plain'
end end
end end
describe 'with StringIO' do describe 'with StringIO' do
before do before do
@content = "hello world" @content = "hello world"
@ -50,7 +50,7 @@ describe Google::APIClient::UploadIO do
it 'should report the correct file length' do it 'should report the correct file length' do
@media.length.should == @content.length @media.length.should == @content.length
end end
it 'should have a mime type' do it 'should have a mime type' do
@media.content_type.should == 'text/plain' @media.content_type.should == 'text/plain'
end end
@ -58,9 +58,10 @@ describe Google::APIClient::UploadIO do
end end
describe Google::APIClient::ResumableUpload do describe Google::APIClient::ResumableUpload do
let(:client) { Google::APIClient.new }
before do before do
@client = Google::APIClient.new @drive = client.discovered_api('drive', 'v1')
@drive = @client.discovered_api('drive', 'v1')
@file = File.expand_path('files/sample.txt', fixtures_path) @file = File.expand_path('files/sample.txt', fixtures_path)
@media = Google::APIClient::UploadIO.new(@file, 'text/plain') @media = Google::APIClient::UploadIO.new(@file, 'text/plain')
@uploader = Google::APIClient::ResumableUpload.new( @uploader = Google::APIClient::ResumableUpload.new(
@ -68,7 +69,7 @@ describe Google::APIClient::ResumableUpload do
@media, @media,
'https://www.googleapis.com/upload/drive/v1/files/12345') 'https://www.googleapis.com/upload/drive/v1/files/12345')
end end
it 'should consider 20x status as complete' do it 'should consider 20x status as complete' do
api_client = stub('api', :execute => mock_result(200)) api_client = stub('api', :execute => mock_result(200))
@uploader.send_chunk(api_client) @uploader.send_chunk(api_client)
@ -93,8 +94,8 @@ describe Google::APIClient::ResumableUpload do
@uploader.send_chunk(api_client) @uploader.send_chunk(api_client)
@uploader.location.should == 'https://www.googleapis.com/upload/drive/v1/files/abcdef' @uploader.location.should == 'https://www.googleapis.com/upload/drive/v1/files/abcdef'
end end
it 'should resume from the saved range reported by the server' do it 'should resume from the saved range reported by the server' do
api_client = mock('api') api_client = mock('api')
api_client.should_receive(:execute).and_return(mock_result(308, 'range' => '0-99')) api_client.should_receive(:execute).and_return(mock_result(308, 'range' => '0-99'))
api_client.should_receive(:execute).with( api_client.should_receive(:execute).with(
@ -107,7 +108,7 @@ describe Google::APIClient::ResumableUpload do
@uploader.send_chunk(api_client) # Send bytes 0-199, only 0-99 saved @uploader.send_chunk(api_client) # Send bytes 0-199, only 0-99 saved
@uploader.send_chunk(api_client) # Send bytes 100-299 @uploader.send_chunk(api_client) # Send bytes 100-299
end end
it 'should resync the offset after 5xx errors' do it 'should resync the offset after 5xx errors' do
api_client = mock('api') api_client = mock('api')
api_client.should_receive(:execute).and_return(mock_result(500)) api_client.should_receive(:execute).and_return(mock_result(500))
@ -132,5 +133,5 @@ describe Google::APIClient::ResumableUpload do
reference = Google::APIClient::Reference.new(:api_method => @drive.files.insert) reference = Google::APIClient::Reference.new(:api_method => @drive.files.insert)
stub('result', :status => status, :headers => headers, :reference => reference) stub('result', :status => status, :headers => headers, :reference => reference)
end end
end end

View File

@ -18,14 +18,12 @@ require 'google/api_client'
require 'google/api_client/version' require 'google/api_client/version'
describe Google::APIClient::Result do describe Google::APIClient::Result do
before do let(:client) { Google::APIClient.new }
@client = Google::APIClient.new
end
describe 'with the plus API' do describe 'with the plus API' do
before do before do
@client.authorization = nil client.authorization = nil
@plus = @client.discovered_api('plus', 'v1') @plus = client.discovered_api('plus', 'v1')
@reference = Google::APIClient::Reference.new({ @reference = Google::APIClient::Reference.new({
:api_method => @plus.activities.list, :api_method => @plus.activities.list,
:parameters => { :parameters => {
@ -77,10 +75,10 @@ describe Google::APIClient::Result do
it 'should escape the next page token when calling next_page' do it 'should escape the next page token when calling next_page' do
reference = @result.next_page reference = @result.next_page
reference.parameters.should include('pageToken') Hash[reference.parameters].should include('pageToken')
reference.parameters['pageToken'].should == 'NEXT+PAGE+TOKEN' Hash[reference.parameters]['pageToken'].should == 'NEXT+PAGE+TOKEN'
path = reference.to_request.path.to_s url = reference.to_request.to_env(Faraday.default_connection)[:url]
path.should include 'pageToken=NEXT%2BPAGE%2BTOKEN' url.to_s.should include('pageToken=NEXT%2BPAGE%2BTOKEN')
end end
it 'should return content type correctly' do it 'should return content type correctly' do

View File

@ -17,21 +17,21 @@ require 'spec_helper'
require 'google/api_client' require 'google/api_client'
describe Google::APIClient::JWTAsserter do describe Google::APIClient::JWTAsserter do
before do before do
@key = OpenSSL::PKey::RSA.new 2048 @key = OpenSSL::PKey::RSA.new 2048
end end
it 'should generate valid JWTs' do it 'should generate valid JWTs' do
asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key) asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
jwt = asserter.to_jwt jwt = asserter.to_jwt
jwt.should_not == nil jwt.should_not == nil
claim = JWT.decode(jwt, @key.public_key, true) claim = JWT.decode(jwt, @key.public_key, true)
claim["iss"].should == 'client1' claim["iss"].should == 'client1'
claim["scope"].should == 'scope1 scope2' claim["scope"].should == 'scope1 scope2'
end end
it 'should send valid access token request' do it 'should send valid access token request' do
stubs = Faraday::Adapter::Test::Stubs.new do |stub| stubs = Faraday::Adapter::Test::Stubs.new do |stub|
stub.post('/o/oauth2/token') do |env| stub.post('/o/oauth2/token') do |env|
@ -48,7 +48,7 @@ describe Google::APIClient::JWTAsserter do
connection = Faraday.new(:url => 'https://accounts.google.com') do |builder| connection = Faraday.new(:url => 'https://accounts.google.com') do |builder|
builder.adapter(:test, stubs) builder.adapter(:test, stubs)
end end
asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key) asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
auth = asserter.authorize(nil, { :connection => connection}) auth = asserter.authorize(nil, { :connection => connection})
auth.should_not == nil? auth.should_not == nil?

View File

@ -26,26 +26,26 @@ require 'google/api_client/version'
shared_examples_for 'configurable user agent' do shared_examples_for 'configurable user agent' do
it 'should allow the user agent to be modified' do it 'should allow the user agent to be modified' do
@client.user_agent = 'Custom User Agent/1.2.3' client.user_agent = 'Custom User Agent/1.2.3'
@client.user_agent.should == 'Custom User Agent/1.2.3' client.user_agent.should == 'Custom User Agent/1.2.3'
end end
it 'should allow the user agent to be set to nil' do it 'should allow the user agent to be set to nil' do
@client.user_agent = nil client.user_agent = nil
@client.user_agent.should == nil client.user_agent.should == nil
end end
it 'should not allow the user agent to be used with bogus values' do it 'should not allow the user agent to be used with bogus values' do
(lambda do (lambda do
@client.user_agent = 42 client.user_agent = 42
@client.transmit( client.transmit(
['GET', 'http://www.google.com/', [], []] ['GET', 'http://www.google.com/', [], []]
) )
end).should raise_error(TypeError) end).should raise_error(TypeError)
end end
it 'should transmit a User-Agent header when sending requests' do it 'should transmit a User-Agent header when sending requests' do
@client.user_agent = 'Custom User Agent/1.2.3' client.user_agent = 'Custom User Agent/1.2.3'
request = Faraday::Request.new(:get) do |req| request = Faraday::Request.new(:get) do |req|
req.url('http://www.google.com/') req.url('http://www.google.com/')
end end
@ -53,48 +53,46 @@ shared_examples_for 'configurable user agent' do
stub.get('/') do |env| stub.get('/') do |env|
headers = env[:request_headers] headers = env[:request_headers]
headers.should have_key('User-Agent') headers.should have_key('User-Agent')
headers['User-Agent'].should == @client.user_agent headers['User-Agent'].should == client.user_agent
[200, {}, ['']] [200, {}, ['']]
end end
end end
connection = Faraday.new(:url => 'https://www.google.com') do |builder| connection = Faraday.new(:url => 'https://www.google.com') do |builder|
builder.adapter(:test, stubs) builder.adapter(:test, stubs)
end end
@client.transmit(:request => request, :connection => connection) client.transmit(:request => request, :connection => connection)
stubs.verify_stubbed_calls stubs.verify_stubbed_calls
end end
end end
describe Google::APIClient do describe Google::APIClient do
before do let(:client) { Google::APIClient.new }
@client = Google::APIClient.new
end
it 'should make its version number available' do it 'should make its version number available' do
Google::APIClient::VERSION::STRING.should be_instance_of(String) Google::APIClient::VERSION::STRING.should be_instance_of(String)
end end
it 'should default to OAuth 2' do it 'should default to OAuth 2' do
Signet::OAuth2::Client.should === @client.authorization Signet::OAuth2::Client.should === client.authorization
end end
it_should_behave_like 'configurable user agent' it_should_behave_like 'configurable user agent'
describe 'configured for OAuth 1' do describe 'configured for OAuth 1' do
before do before do
@client.authorization = :oauth_1 client.authorization = :oauth_1
end end
it 'should use the default OAuth1 client configuration' do it 'should use the default OAuth1 client configuration' do
@client.authorization.temporary_credential_uri.to_s.should == client.authorization.temporary_credential_uri.to_s.should ==
'https://www.google.com/accounts/OAuthGetRequestToken' 'https://www.google.com/accounts/OAuthGetRequestToken'
@client.authorization.authorization_uri.to_s.should include( client.authorization.authorization_uri.to_s.should include(
'https://www.google.com/accounts/OAuthAuthorizeToken' 'https://www.google.com/accounts/OAuthAuthorizeToken'
) )
@client.authorization.token_credential_uri.to_s.should == client.authorization.token_credential_uri.to_s.should ==
'https://www.google.com/accounts/OAuthGetAccessToken' 'https://www.google.com/accounts/OAuthGetAccessToken'
@client.authorization.client_credential_key.should == 'anonymous' client.authorization.client_credential_key.should == 'anonymous'
@client.authorization.client_credential_secret.should == 'anonymous' client.authorization.client_credential_secret.should == 'anonymous'
end end
it_should_behave_like 'configurable user agent' it_should_behave_like 'configurable user agent'
@ -102,7 +100,7 @@ describe Google::APIClient do
describe 'configured for OAuth 2' do describe 'configured for OAuth 2' do
before do before do
@client.authorization = :oauth_2 client.authorization = :oauth_2
end end
# TODO # TODO
@ -111,7 +109,7 @@ describe Google::APIClient do
describe 'when executing requests' do describe 'when executing requests' do
before do before do
@client.authorization = :oauth_2 client.authorization = :oauth_2
@connection = Faraday.new(:url => 'https://www.googleapis.com') do |builder| @connection = Faraday.new(:url => 'https://www.googleapis.com') do |builder|
stubs = Faraday::Adapter::Test::Stubs.new do |stub| stubs = Faraday::Adapter::Test::Stubs.new do |stub|
stub.get('/test') do |env| stub.get('/test') do |env|
@ -123,16 +121,16 @@ describe Google::APIClient do
end end
it 'should use default authorization' do it 'should use default authorization' do
@client.authorization.access_token = "12345" client.authorization.access_token = "12345"
@client.execute(:http_method => :get, client.execute(:http_method => :get,
:uri => 'https://www.googleapis.com/test', :uri => 'https://www.googleapis.com/test',
:connection => @connection) :connection => @connection)
end end
it 'should use request scoped authorization when provided' do it 'should use request scoped authorization when provided' do
@client.authorization.access_token = "abcdef" client.authorization.access_token = "abcdef"
new_auth = Signet::OAuth2::Client.new(:access_token => '12345') new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
@client.execute(:http_method => :get, client.execute(:http_method => :get,
:uri => 'https://www.googleapis.com/test', :uri => 'https://www.googleapis.com/test',
:connection => @connection, :connection => @connection,
:authorization => new_auth) :authorization => new_auth)