Added User-Agent header.
git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@107 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef
This commit is contained in:
parent
76fe65a650
commit
0b49c3fd2e
|
@ -32,7 +32,9 @@ module Google
|
|||
|
||||
def initialize(options={})
|
||||
@options = {
|
||||
# TODO: What configuration options need to go here?
|
||||
:user_agent => (
|
||||
'google-api-ruby-client/' + Google::APIClient::VERSION::STRING
|
||||
)
|
||||
}.merge(options)
|
||||
# Force immediate type-checking and short-cut resolution
|
||||
self.parser
|
||||
|
@ -255,6 +257,29 @@ module Google
|
|||
end).sort.last
|
||||
end
|
||||
|
||||
##
|
||||
# Returns the user agent used by the client.
|
||||
#
|
||||
# @return [String]
|
||||
# The user agent string used in the User-Agent header.
|
||||
def user_agent
|
||||
return @options[:user_agent]
|
||||
end
|
||||
|
||||
##
|
||||
# Sets the user agent used by the client.
|
||||
#
|
||||
# @param [String, #to_str] new_user_agent
|
||||
# The new user agent string to use in the User-Agent header.
|
||||
def user_agent=(new_user_agent)
|
||||
unless new_user_agent == nil || new_user_agent.respond_to?(:to_str)
|
||||
raise TypeError, "Expected String, got #{new_user_agent.class}."
|
||||
end
|
||||
new_user_agent = new_user_agent.to_str unless new_user_agent == nil
|
||||
@options[:user_agent] = new_user_agent
|
||||
return self.user_agent
|
||||
end
|
||||
|
||||
##
|
||||
# Generates a request.
|
||||
#
|
||||
|
@ -371,7 +396,21 @@ module Google
|
|||
#
|
||||
# @return [Array] The response from the server.
|
||||
def transmit_request(request, adapter=self.http_adapter)
|
||||
::HTTPAdapter.transmit(request, adapter)
|
||||
if self.user_agent != nil
|
||||
# If there's no User-Agent header, set one.
|
||||
method, uri, headers, body = request
|
||||
unless headers.kind_of?(Enumerable)
|
||||
# We need to use some Enumerable methods, relying on the presence of
|
||||
# the #each method.
|
||||
class <<headers
|
||||
include Enumerable
|
||||
end
|
||||
end
|
||||
unless headers.any? { |k, v| k.downcase == 'user-agent' }
|
||||
headers = headers.to_a.insert(0, ['User-Agent', self.user_agent])
|
||||
end
|
||||
end
|
||||
::HTTPAdapter.transmit([method, uri, headers, body], adapter)
|
||||
end
|
||||
|
||||
##
|
||||
|
|
|
@ -16,11 +16,44 @@ require 'spec_helper'
|
|||
|
||||
require 'signet/oauth_1/client'
|
||||
require 'httpadapter/adapters/net_http'
|
||||
require 'httpadapter/adapters/mock'
|
||||
|
||||
require 'google/api_client'
|
||||
require 'google/api_client/version'
|
||||
require 'google/api_client/parsers/json_parser'
|
||||
|
||||
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
|
||||
|
||||
it 'should not allow the user agent to be set to bogus values' do
|
||||
(lambda do
|
||||
@client.user_agent = 42
|
||||
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/', [], []]
|
||||
adapter = HTTPAdapter::MockAdapter.request_adapter do |request, connection|
|
||||
method, uri, headers, body = request
|
||||
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
|
||||
|
||||
describe Google::APIClient, 'with default configuration' do
|
||||
before do
|
||||
@client = Google::APIClient.new
|
||||
|
@ -37,6 +70,8 @@ describe Google::APIClient, 'with default configuration' do
|
|||
it 'should not use an authorization mechanism' do
|
||||
@client.authorization.should be_nil
|
||||
end
|
||||
|
||||
it_should_behave_like 'configurable user agent'
|
||||
end
|
||||
|
||||
describe Google::APIClient, 'with default oauth configuration' do
|
||||
|
@ -63,6 +98,8 @@ describe Google::APIClient, 'with default oauth configuration' do
|
|||
@client.authorization.client_credential_key.should == 'anonymous'
|
||||
@client.authorization.client_credential_secret.should == 'anonymous'
|
||||
end
|
||||
|
||||
it_should_behave_like 'configurable user agent'
|
||||
end
|
||||
|
||||
describe Google::APIClient, 'with custom pluggable parser' do
|
||||
|
@ -76,4 +113,6 @@ describe Google::APIClient, 'with custom pluggable parser' do
|
|||
it 'should use the custom parser' do
|
||||
@client.parser.should be_instance_of(FakeJsonParser)
|
||||
end
|
||||
|
||||
it_should_behave_like 'configurable user agent'
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue