A significant update of the client functionality.

* updated to use v0.3 of the discovery API
* updated to use httpadapter 1.0.0
* added OAuth 2 support to the command line tool
* renamed some switches in the command line tool
* added additional configuration capabilities

git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@128 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef
This commit is contained in:
Bob Aman committed 2011-05-04 11:44:35 +00:00
1 parent 1dee705828
commit 286a7152f2
13 files changed
+1062 -805

No files matched your search

+371 -371
View File
@@ -21,422 +21,422 @@ require 'google/api_client'
require 'google/api_client/version'
require 'google/api_client/parsers/json_parser'
describe Google::APIClient, 'unconfigured' do
describe Google::APIClient do
before do
@client = Google::APIClient.new
end
it 'should not be able to determine the discovery URI' do
it 'should raise a type error for bogus authorization' do
(lambda do
@client.discovery_uri
end).should raise_error(ArgumentError)
end
end
describe Google::APIClient, 'configured for a bogus API' do
before do
@client = Google::APIClient.new(:service => 'bogus')
Google::APIClient.new(:authorization => 42)
end).should raise_error(TypeError)
end
it 'should not be able to retrieve the discovery document' do
it 'should not be able to retrieve the discovery document for a bogus API' do
(lambda do
@client.discovery_document
@client.discovery_document('bogus')
end).should raise_error(Google::APIClient::TransmissionError)
(lambda do
@client.discovered_api('bogus')
end).should raise_error(Google::APIClient::TransmissionError)
end
end
describe Google::APIClient, 'configured for bogus authorization' do
it 'should raise a type error' do
it 'should raise an error for bogus services' do
(lambda do
Google::APIClient.new(:service => 'prediction', :authorization => 42)
@client.discovered_api(42)
end).should raise_error(TypeError)
end
end
describe Google::APIClient, 'configured for the prediction API' do
before do
@client = Google::APIClient.new(:service => 'prediction')
end
it 'should correctly determine the discovery URI' do
@client.discovery_uri.should ===
'http://www.googleapis.com/discovery/0.1/describe?api=prediction'
end
it 'should have multiple versions available' do
@client.discovered_services.size.should > 1
end
it 'should find APIs that are in the discovery document' do
@client.discovered_service('prediction').name.should == 'prediction'
@client.discovered_service('prediction').version.should == 'v1'
@client.discovered_service(:prediction).name.should == 'prediction'
@client.discovered_service(:prediction).version.should == 'v1'
end
it 'should find API versions that are in the discovery document' do
@client.discovered_service('prediction', 'v1.1').version.should == 'v1.1'
end
it 'should not find APIs that are not in the discovery document' do
@client.discovered_service('bogus').should == nil
end
it 'should raise an error for bogus services' do
(lambda do
@client.discovered_service(42)
@client.preferred_version(42)
end).should raise_error(TypeError)
end
it 'should find methods that are in the discovery document' do
@client.discovered_method('prediction.training.insert').name.should ==
'insert'
@client.discovered_method(:'prediction.training.insert').name.should ==
'insert'
end
it 'should find methods for versions that are in the discovery document' do
@client.discovered_method(
'prediction.training.delete', 'v1.1'
).should_not == nil
end
it 'should not find methods that are not in the discovery document' do
@client.discovered_method('prediction.training.delete', 'v1').should == nil
@client.discovered_method('prediction.bogus').should == nil
end
it 'should raise an error for bogus methods' do
(lambda do
@client.discovered_method(42)
end).should raise_error(TypeError)
end
it 'should correctly determine the latest version' do
@client.latest_service_version('prediction').version.should_not == 'v1'
@client.latest_service_version(:prediction).version.should_not == 'v1'
end
it 'should raise an error for bogus services' do
(lambda do
@client.latest_service_version(42)
end).should raise_error(TypeError)
end
it 'should correctly determine the latest version' do
# Sanity check the algorithm
@client.discovered_services.clear
@client.discovered_services <<
Google::APIClient::Service.new('magic', 'v1', {})
@client.discovered_services <<
Google::APIClient::Service.new('magic', 'v1.1', {})
@client.discovered_services <<
Google::APIClient::Service.new('magic', 'v1.10', {})
@client.discovered_services <<
Google::APIClient::Service.new('magic', 'v10.0.1', {})
@client.discovered_services <<
Google::APIClient::Service.new('magic', 'v10.1', {})
@client.discovered_services <<
Google::APIClient::Service.new('magic', 'v2.1', {})
@client.discovered_services <<
Google::APIClient::Service.new('magic', 'v10.0', {})
@client.latest_service_version('magic').version.should == 'v10.1'
end
it 'should correctly determine the latest version' do
# Sanity check the algorithm
@client.discovered_services.clear
@client.discovered_services <<
Google::APIClient::Service.new('one', 'v3', {})
@client.discovered_services <<
Google::APIClient::Service.new('two', 'v1', {})
@client.discovered_services <<
Google::APIClient::Service.new('two', 'v1.1-r1c3', {})
@client.discovered_services <<
Google::APIClient::Service.new('two', 'v2', {})
@client.discovered_services <<
Google::APIClient::Service.new('two', 'v2beta1', {})
@client.discovered_services <<
Google::APIClient::Service.new('two', 'test2', {})
@client.latest_service_version('two').version.should == 'v2'
end
it 'should return nil for bogus service names' do
# Sanity check the algorithm
@client.latest_service_version('bogus').should == nil
end
it 'should generate valid requests' do
request = @client.generate_request(
'prediction.training.insert',
{'query' => '12345'}
)
method, uri, headers, body = request
method.should == 'POST'
uri.should ==
'https://www.googleapis.com/prediction/v1/training?query=12345'
(headers.inject({}) { |h,(k,v)| h[k]=v; h }).should == {}
body.should respond_to(:each)
end
it 'should generate requests against the correct URIs' do
request = @client.generate_request(
:'prediction.training.insert',
{'query' => '12345'}
)
method, uri, headers, body = request
uri.should ==
'https://www.googleapis.com/prediction/v1/training?query=12345'
end
it 'should generate requests against the correct URIs' do
prediction = @client.discovered_service('prediction', 'v1')
request = @client.generate_request(
prediction.training.insert,
{'query' => '12345'}
)
method, uri, headers, body = request
uri.should ==
'https://www.googleapis.com/prediction/v1/training?query=12345'
end
it 'should allow modification to the base URIs for testing purposes' do
prediction = @client.discovered_service('prediction', 'v1')
prediction.base = 'https://testing-domain.googleapis.com/prediction/v1/'
request = @client.generate_request(
prediction.training.insert,
{'query' => '123'}
)
method, uri, headers, body = request
uri.should ==
'https://testing-domain.googleapis.com/prediction/v1/training?query=123'
end
it 'should generate signed requests' do
@client.authorization = :oauth_1
@client.authorization.token_credential_key = '12345'
@client.authorization.token_credential_secret = '12345'
request = @client.generate_request(
'prediction.training.insert',
{'query' => '12345'}
)
method, uri, headers, body = request
headers = headers.inject({}) { |h,(k,v)| h[k]=v; h }
headers.keys.should include('Authorization')
headers['Authorization'].should =~ /^OAuth/
end
it 'should not be able to execute improperly authorized requests' do
@client.authorization = :oauth_1
@client.authorization.token_credential_key = '12345'
@client.authorization.token_credential_secret = '12345'
response = @client.execute(
'prediction.training.insert',
{'query' => '12345'}
)
status, headers, body = response
status.should == 401
end
it 'should raise an error for bogus methods' do
(lambda do
@client.generate_request(42)
end).should raise_error(TypeError)
end
it 'should raise an error for bogus methods' do
(lambda do
@client.generate_request(@client.discovered_service('prediction'))
end).should raise_error(TypeError)
end
end
describe Google::APIClient, 'configured for the buzz API' do
before do
@client = Google::APIClient.new(:service => 'buzz')
it 'should not return a preferred version for bogus service names' do
@client.preferred_version('bogus').should == nil
end
it 'should correctly determine the discovery URI' do
@client.discovery_uri.should ===
'http://www.googleapis.com/discovery/0.1/describe?api=buzz'
describe 'with the prediction API' do
before do
@client.authorization = nil
end
it 'should correctly determine the discovery URI' do
@client.discovery_uri('prediction').should ===
'https://www.googleapis.com/discovery/v0.3/describe/prediction/v1'
end
it 'should correctly generate API objects' do
@client.discovered_api('prediction').name.should == 'prediction'
@client.discovered_api('prediction').version.should == 'v1'
@client.discovered_api(:prediction).name.should == 'prediction'
@client.discovered_api(:prediction).version.should == 'v1'
end
it 'should discover methods' do
@client.discovered_method(
'prediction.training.insert', 'prediction'
).name.should == 'insert'
@client.discovered_method(
:'prediction.training.insert', :prediction
).name.should == 'insert'
end
it 'should discover methods' do
@client.discovered_method(
'prediction.training.delete', 'prediction', 'v1.1'
).name.should == 'delete'
end
it 'should not find methods that are not in the discovery document' do
@client.discovered_method(
'prediction.training.delete', 'prediction', 'v1'
).should == nil
@client.discovered_method(
'prediction.bogus', 'prediction', 'v1'
).should == nil
end
it 'should raise an error for bogus methods' do
(lambda do
@client.discovered_method(42, 'prediction', 'v1')
end).should raise_error(TypeError)
end
it 'should raise an error for bogus methods' do
(lambda do
@client.generate_request(@client.discovered_api('prediction'))
end).should raise_error(TypeError)
end
it 'should correctly determine the preferred version' do
@client.preferred_version('prediction').version.should_not == 'v1'
@client.preferred_version(:prediction).version.should_not == 'v1'
end
it 'should generate valid requests' do
request = @client.generate_request(
'prediction.training.insert',
{'data' => '12345', }
)
method, uri, headers, body = request
method.should == 'POST'
uri.should ==
'https://www.googleapis.com/prediction/v1/training?data=12345'
(headers.inject({}) { |h,(k,v)| h[k]=v; h }).should == {}
body.should respond_to(:each)
end
it 'should generate requests against the correct URIs' do
request = @client.generate_request(
:'prediction.training.insert',
{'data' => '12345'}
)
method, uri, headers, body = request
uri.should ==
'https://www.googleapis.com/prediction/v1/training?data=12345'
end
it 'should generate requests against the correct URIs' do
prediction = @client.discovered_api('prediction', 'v1')
request = @client.generate_request(
prediction.training.insert,
{'data' => '12345'}
)
method, uri, headers, body = request
uri.should ==
'https://www.googleapis.com/prediction/v1/training?data=12345'
end
it 'should allow modification to the base URIs for testing purposes' do
prediction = @client.discovered_api('prediction', 'v1')
prediction.rest_base =
'https://testing-domain.googleapis.com/prediction/v1/'
request = @client.generate_request(
prediction.training.insert,
{'data' => '123'}
)
method, uri, headers, body = request
uri.should ==
'https://testing-domain.googleapis.com/prediction/v1/training?data=123'
end
it 'should generate OAuth 1 requests' do
@client.authorization = :oauth_1
@client.authorization.token_credential_key = '12345'
@client.authorization.token_credential_secret = '12345'
request = @client.generate_request(
'prediction.training.insert',
{'data' => '12345'}
)
method, uri, headers, body = request
headers = headers.inject({}) { |h,(k,v)| h[k]=v; h }
headers.keys.should include('Authorization')
headers['Authorization'].should =~ /^OAuth/
end
it 'should generate OAuth 2 requests' do
@client.authorization = :oauth_2
@client.authorization.access_token = '12345'
request = @client.generate_request(
'prediction.training.insert',
{'data' => '12345'}
)
method, uri, headers, body = request
headers = headers.inject({}) { |h,(k,v)| h[k]=v; h }
headers.keys.should include('Authorization')
headers['Authorization'].should =~ /^OAuth/
end
it 'should not be able to execute improperly authorized requests' do
@client.authorization = :oauth_1
@client.authorization.token_credential_key = '12345'
@client.authorization.token_credential_secret = '12345'
response = @client.execute(
'prediction.training.insert',
{'data' => '12345'}
)
status, headers, body = response
status.should == 401
end
it 'should not be able to execute improperly authorized requests' do
@client.authorization = :oauth_2
@client.authorization.access_token = '12345'
response = @client.execute(
'prediction.training.insert',
{'data' => '12345'}
)
status, headers, body = response
status.should == 401
end
end
it 'should find APIs that are in the discovery document' do
@client.discovered_service('buzz').name.should == 'buzz'
@client.discovered_service('buzz').version.should == 'v1'
end
describe 'with the buzz API' do
before do
@client.authorization = nil
@buzz = @client.discovered_api('buzz')
end
it 'should not find APIs that are not in the discovery document' do
@client.discovered_service('bogus').should == nil
end
it 'should correctly determine the discovery URI' do
@client.discovery_uri('buzz').should ===
'https://www.googleapis.com/discovery/v0.3/describe/buzz/v1'
end
it 'should find methods that are in the discovery document' do
# TODO(bobaman) Fix this when the RPC names are correct
@client.discovered_method('chili.activities.list').name.should == 'list'
end
it 'should find APIs that are in the discovery document' do
@client.discovered_api('buzz').name.should == 'buzz'
@client.discovered_api('buzz').version.should == 'v1'
@client.discovered_api(:buzz).name.should == 'buzz'
@client.discovered_api(:buzz).version.should == 'v1'
end
it 'should not find methods that are not in the discovery document' do
@client.discovered_method('buzz.bogus').should == nil
end
it 'should find methods that are in the discovery document' do
# TODO(bobaman) Fix this when the RPC names are correct
@client.discovered_method(
'chili.activities.list', 'buzz'
).name.should == 'list'
end
it 'should generate requests against the correct URIs' do
# TODO(bobaman) Fix this when the RPC names are correct
request = @client.generate_request(
'chili.activities.list',
{'userId' => 'hikingfan', 'scope' => '@public'},
'',
[],
{:signed => false}
)
method, uri, headers, body = request
uri.should ==
'https://www.googleapis.com/buzz/v1/activities/hikingfan/@public'
end
it 'should not find methods that are not in the discovery document' do
@client.discovered_method('buzz.bogus', 'buzz').should == nil
end
it 'should correctly validate parameters' do
# TODO(bobaman) Fix this when the RPC names are correct
(lambda do
@client.generate_request(
'chili.activities.list',
{'alt' => 'json'},
it 'should fail for string RPC names that do not match API name' do
(lambda do
@client.generate_request(
'chili.activities.list',
{'alt' => 'json'},
'',
[],
{:signed => false}
)
end).should raise_error(Google::APIClient::TransmissionError)
end
it 'should generate requests against the correct URIs' do
request = @client.generate_request(
@buzz.activities.list,
{'userId' => 'hikingfan', 'scope' => '@public'},
'',
[],
{:signed => false}
)
end).should raise_error(ArgumentError)
end
method, uri, headers, body = request
uri.should ==
'https://www.googleapis.com/buzz/v1/activities/hikingfan/@public'
end
it 'should correctly validate parameters' do
# TODO(bobaman) Fix this when the RPC names are correct
(lambda do
@client.generate_request(
'chili.activities.list',
{'userId' => 'hikingfan', 'scope' => '@bogus'},
it 'should correctly validate parameters' do
(lambda do
@client.generate_request(
@buzz.activities.list,
{'alt' => 'json'},
'',
[],
{:signed => false}
)
end).should raise_error(ArgumentError)
end
it 'should correctly validate parameters' do
(lambda do
@client.generate_request(
@buzz.activities.list,
{'userId' => 'hikingfan', 'scope' => '@bogus'},
'',
[],
{:signed => false}
)
end).should raise_error(ArgumentError)
end
it 'should be able to execute requests without authorization' do
response = @client.execute(
@buzz.activities.list,
{'alt' => 'json', 'userId' => 'hikingfan', 'scope' => '@public'},
'',
[],
{:signed => false}
)
end).should raise_error(ArgumentError)
status, headers, body = response
status.should == 200
end
end
it 'should be able to execute requests without authorization' do
# TODO(bobaman) Fix this when the RPC names are correct
response = @client.execute(
'chili.activities.list',
{'alt' => 'json', 'userId' => 'hikingfan', 'scope' => '@public'},
'',
[],
{:signed => false}
)
status, headers, body = response
status.should == 200
end
end
describe Google::APIClient, 'configured for the latitude API' do
before do
@client = Google::APIClient.new(:service => 'latitude')
end
it 'should correctly determine the discovery URI' do
@client.discovery_uri.should ===
'http://www.googleapis.com/discovery/0.1/describe?api=latitude'
end
it 'should find APIs that are in the discovery document' do
@client.discovered_service('latitude').name.should == 'latitude'
@client.discovered_service('latitude').version.should == 'v1'
end
it 'should not find APIs that are not in the discovery document' do
@client.discovered_service('bogus').should == nil
end
it 'should find methods that are in the discovery document' do
@client.discovered_method('latitude.currentLocation.get').name.should ==
'get'
end
it 'should not find methods that are not in the discovery document' do
@client.discovered_method('latitude.bogus').should == nil
end
it 'should generate requests against the correct URIs' do
request = @client.generate_request(
'latitude.currentLocation.get',
{},
'',
[],
{:signed => false}
)
method, uri, headers, body = request
uri.should ==
'https://www.googleapis.com/latitude/v1/currentLocation'
end
it 'should not be able to execute requests without authorization' do
response = @client.execute(
'latitude.currentLocation.get',
{},
'',
[],
{:signed => false}
)
status, headers, body = response
status.should == 401
end
end
describe Google::APIClient, 'configured for the moderator API' do
before do
@client = Google::APIClient.new(:service => 'moderator')
end
it 'should correctly determine the discovery URI' do
@client.discovery_uri.should ===
'http://www.googleapis.com/discovery/0.1/describe?api=moderator'
end
it 'should find APIs that are in the discovery document' do
@client.discovered_service('moderator').name.should == 'moderator'
@client.discovered_service('moderator').version.should == 'v1'
end
it 'should not find APIs that are not in the discovery document' do
@client.discovered_service('bogus').should == nil
end
it 'should find methods that are in the discovery document' do
@client.discovered_method('moderator.profiles.get').name.should ==
'get'
end
it 'should not find methods that are not in the discovery document' do
@client.discovered_method('moderator.bogus').should == nil
end
it 'should generate requests against the correct URIs' do
request = @client.generate_request(
'moderator.profiles.get',
{},
'',
[],
{:signed => false}
)
method, uri, headers, body = request
uri.should ==
'https://www.googleapis.com/moderator/v1/profiles/@me'
end
it 'should not be able to execute requests without authorization' do
response = @client.execute(
'moderator.profiles.get',
{},
'',
[],
{:signed => false}
)
status, headers, body = response
status.should == 401
describe 'with the latitude API' do
before do
@client.authorization = nil
@latitude = @client.discovered_api('latitude')
end
it 'should correctly determine the discovery URI' do
@client.discovery_uri('latitude').should ===
'https://www.googleapis.com/discovery/v0.3/describe/latitude/v1'
end
it 'should find APIs that are in the discovery document' do
@client.discovered_api('latitude').name.should == 'latitude'
@client.discovered_api('latitude').version.should == 'v1'
end
it 'should find methods that are in the discovery document' do
@client.discovered_method(
'latitude.currentLocation.get', 'latitude'
).name.should == 'get'
end
it 'should not find methods that are not in the discovery document' do
@client.discovered_method('latitude.bogus', 'latitude').should == nil
end
it 'should generate requests against the correct URIs' do
request = @client.generate_request(
'latitude.currentLocation.get',
{},
'',
[],
{:signed => false}
)
method, uri, headers, body = request
uri.should ==
'https://www.googleapis.com/latitude/v1/currentLocation'
end
it 'should generate requests against the correct URIs' do
request = @client.generate_request(
@latitude.current_location.get,
{},
'',
[],
{:signed => false}
)
method, uri, headers, body = request
uri.should ==
'https://www.googleapis.com/latitude/v1/currentLocation'
end
it 'should not be able to execute requests without authorization' do
response = @client.execute(
'latitude.currentLocation.get',
{},
'',
[],
{:signed => false}
)
status, headers, body = response
status.should == 401
end
end
describe 'with the moderator API' do
before do
@client.authorization = nil
@moderator = @client.discovered_api('moderator')
end
it 'should correctly determine the discovery URI' do
@client.discovery_uri('moderator').should ===
'https://www.googleapis.com/discovery/v0.3/describe/moderator/v1'
end
it 'should find APIs that are in the discovery document' do
@client.discovered_api('moderator').name.should == 'moderator'
@client.discovered_api('moderator').version.should == 'v1'
end
it 'should find methods that are in the discovery document' do
@client.discovered_method(
'moderator.profiles.get', 'moderator'
).name.should == 'get'
end
it 'should not find methods that are not in the discovery document' do
@client.discovered_method('moderator.bogus', 'moderator').should == nil
end
it 'should generate requests against the correct URIs' do
request = @client.generate_request(
'moderator.profiles.get',
{},
'',
[],
{:signed => false}
)
method, uri, headers, body = request
uri.should ==
'https://www.googleapis.com/moderator/v1/profiles/@me'
end
it 'should generate requests against the correct URIs' do
request = @client.generate_request(
@moderator.profiles.get,
{},
'',
[],
{:signed => false}
)
method, uri, headers, body = request
uri.should ==
'https://www.googleapis.com/moderator/v1/profiles/@me'
end
it 'should not be able to execute requests without authorization' do
response = @client.execute(
'moderator.profiles.get',
{},
'',
[],
{:signed => false}
)
status, headers, body = response
status.should == 401
end
end
end