2010-08-19 23:21:45 +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.
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2010-09-13 21:54:43 +00:00
|
|
|
require 'signet/oauth_1/client'
|
|
|
|
require 'httpadapter/adapters/net_http'
|
2011-01-05 01:09:52 +00:00
|
|
|
require 'httpadapter/adapters/mock'
|
2010-09-13 21:54:43 +00:00
|
|
|
|
2010-08-19 23:21:45 +00:00
|
|
|
require 'google/api_client'
|
|
|
|
require 'google/api_client/version'
|
2010-09-18 00:30:02 +00:00
|
|
|
require 'google/api_client/parsers/json_parser'
|
2010-08-19 23:21:45 +00:00
|
|
|
|
2011-01-05 01:09:52 +00:00
|
|
|
shared_examples_for 'configurable user agent' do
|
|
|
|
it 'should allow the user agent to be modified' do
|
|
|
|
@client.user_agent = 'Custom User Agent/1.2.3'
|
|
|
|
@client.user_agent.should == 'Custom User Agent/1.2.3'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should allow the user agent to be set to nil' do
|
|
|
|
@client.user_agent = nil
|
|
|
|
@client.user_agent.should == nil
|
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should not allow the user agent to be used with bogus values' do
|
2011-01-05 01:09:52 +00:00
|
|
|
(lambda do
|
|
|
|
@client.user_agent = 42
|
2011-05-04 11:44:35 +00:00
|
|
|
@client.transmit_request(
|
|
|
|
['GET', 'http://www.google.com/', [], []]
|
|
|
|
)
|
2011-01-05 01:09:52 +00:00
|
|
|
end).should raise_error(TypeError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should transmit a User-Agent header when sending requests' do
|
|
|
|
@client.user_agent = 'Custom User Agent/1.2.3'
|
|
|
|
request = ['GET', 'http://www.google.com/', [], []]
|
2011-05-04 11:44:35 +00:00
|
|
|
adapter = HTTPAdapter::MockAdapter.create do |request_ary, connection|
|
|
|
|
method, uri, headers, body = request_ary
|
2011-01-05 01:09:52 +00:00
|
|
|
headers.should be_any { |k, v| k.downcase == 'user-agent' }
|
|
|
|
headers.each do |k, v|
|
|
|
|
v.should == @client.user_agent if k.downcase == 'user-agent'
|
|
|
|
end
|
|
|
|
[200, [], ['']]
|
|
|
|
end
|
|
|
|
@client.transmit_request(request, adapter)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
describe Google::APIClient do
|
2010-08-19 23:21:45 +00:00
|
|
|
before do
|
|
|
|
@client = Google::APIClient.new
|
|
|
|
end
|
|
|
|
|
2010-10-12 20:39:09 +00:00
|
|
|
it 'should make its version number available' do
|
|
|
|
::Google::APIClient::VERSION::STRING.should be_instance_of(String)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should use the default JSON parser' do
|
|
|
|
@client.parser.should be(Google::APIClient::JSONParser)
|
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should default to OAuth 2' do
|
|
|
|
Signet::OAuth2::Client.should === @client.authorization
|
2010-10-12 20:39:09 +00:00
|
|
|
end
|
2011-01-05 01:09:52 +00:00
|
|
|
|
|
|
|
it_should_behave_like 'configurable user agent'
|
2010-10-12 20:39:09 +00:00
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
describe 'configured for OAuth 1' do
|
|
|
|
before do
|
|
|
|
@client.authorization = :oauth_1
|
|
|
|
end
|
2010-10-12 20:39:09 +00:00
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should use the default OAuth1 client configuration' do
|
|
|
|
@client.authorization.temporary_credential_uri.to_s.should ==
|
|
|
|
'https://www.google.com/accounts/OAuthGetRequestToken'
|
|
|
|
@client.authorization.authorization_uri.to_s.should include(
|
|
|
|
'https://www.google.com/accounts/OAuthAuthorizeToken'
|
|
|
|
)
|
|
|
|
@client.authorization.token_credential_uri.to_s.should ==
|
|
|
|
'https://www.google.com/accounts/OAuthGetAccessToken'
|
|
|
|
@client.authorization.client_credential_key.should == 'anonymous'
|
|
|
|
@client.authorization.client_credential_secret.should == 'anonymous'
|
|
|
|
end
|
2010-08-19 23:21:45 +00:00
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it_should_behave_like 'configurable user agent'
|
2010-08-19 23:21:45 +00:00
|
|
|
end
|
2010-09-13 21:54:43 +00:00
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
describe 'configured for OAuth 2' do
|
|
|
|
before do
|
|
|
|
@client.authorization = :oauth_2
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO
|
|
|
|
it_should_behave_like 'configurable user agent'
|
2010-09-13 21:54:43 +00:00
|
|
|
end
|
2011-01-05 01:09:52 +00:00
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
describe 'with custom pluggable parser' do
|
|
|
|
before do
|
|
|
|
class FakeJsonParser
|
|
|
|
def serialize(value)
|
|
|
|
return "42"
|
|
|
|
end
|
2010-08-19 23:21:45 +00:00
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
def parse(value)
|
|
|
|
return 42
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@client.parser = FakeJsonParser.new
|
2010-08-19 23:21:45 +00:00
|
|
|
end
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it 'should use the custom parser' do
|
|
|
|
@client.parser.should be_instance_of(FakeJsonParser)
|
|
|
|
end
|
2010-08-19 23:21:45 +00:00
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
it_should_behave_like 'configurable user agent'
|
2010-08-19 23:21:45 +00:00
|
|
|
end
|
|
|
|
end
|