1 # Copyright 2010 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 require 'signet/oauth_1/client'
18 require 'httpadapter/adapters/net_http'
19 require 'httpadapter/adapters/mock'
21 require 'google/api_client'
22 require 'google/api_client/version'
23 require 'google/api_client/parsers/json_parser'
25 shared_examples_for 'configurable user agent' do
26 it 'should allow the user agent to be modified' do
27 @client.user_agent = 'Custom User Agent/1.2.3'
28 @client.user_agent.should == 'Custom User Agent/1.2.3'
31 it 'should allow the user agent to be set to nil' do
32 @client.user_agent = nil
33 @client.user_agent.should == nil
36 it 'should not allow the user agent to be used with bogus values' do
38 @client.user_agent = 42
40 ['GET', 'http://www.google.com/', [], []]
42 end).should raise_error(TypeError)
45 it 'should transmit a User-Agent header when sending requests' do
46 @client.user_agent = 'Custom User Agent/1.2.3'
47 request = ['GET', 'http://www.google.com/', [], []]
48 adapter = HTTPAdapter::MockAdapter.create do |request_ary, connection|
49 method, uri, headers, body = request_ary
50 headers.should be_any { |k, v| k.downcase == 'user-agent' }
51 headers.each do |k, v|
52 v.should == @client.user_agent if k.downcase == 'user-agent'
56 @client.transmit(request, adapter)
60 describe Google::APIClient do
62 @client = Google::APIClient.new
65 it 'should make its version number available' do
66 Google::APIClient::VERSION::STRING.should be_instance_of(String)
69 it 'should default to OAuth 2' do
70 Signet::OAuth2::Client.should === @client.authorization
73 it_should_behave_like 'configurable user agent'
75 describe 'configured for OAuth 1' do
77 @client.authorization = :oauth_1
80 it 'should use the default OAuth1 client configuration' do
81 @client.authorization.temporary_credential_uri.to_s.should ==
82 'https://www.google.com/accounts/OAuthGetRequestToken'
83 @client.authorization.authorization_uri.to_s.should include(
84 'https://www.google.com/accounts/OAuthAuthorizeToken'
86 @client.authorization.token_credential_uri.to_s.should ==
87 'https://www.google.com/accounts/OAuthGetAccessToken'
88 @client.authorization.client_credential_key.should == 'anonymous'
89 @client.authorization.client_credential_secret.should == 'anonymous'
92 it_should_behave_like 'configurable user agent'
95 describe 'configured for OAuth 2' do
97 @client.authorization = :oauth_2
101 it_should_behave_like 'configurable user agent'