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.
18 require 'faraday/utils'
19 require 'signet/oauth_1/client'
20 require 'google/api_client'
21 require 'google/api_client/version'
23 shared_examples_for 'configurable user agent' do
24 it 'should allow the user agent to be modified' do
25 client.user_agent = 'Custom User Agent/1.2.3'
26 client.user_agent.should == 'Custom User Agent/1.2.3'
29 it 'should allow the user agent to be set to nil' do
30 client.user_agent = nil
31 client.user_agent.should == nil
34 it 'should not allow the user agent to be used with bogus values' do
36 client.user_agent = 42
38 ['GET', 'http://www.google.com/', [], []]
40 end).should raise_error(TypeError)
43 it 'should transmit a User-Agent header when sending requests' do
44 client.user_agent = 'Custom User Agent/1.2.3'
46 stubs = Faraday::Adapter::Test::Stubs.new do |stub|
47 stub.get('/') do |env|
48 headers = env[:request_headers]
49 headers.should have_key('User-Agent')
50 headers['User-Agent'].should == client.user_agent
54 connection = Faraday.new(:url => 'https://www.google.com') do |builder|
55 builder.adapter(:test, stubs)
57 request = connection.build_request(:get) do |req|
58 req.url('http://www.google.com/')
60 client.transmit(:request => request, :connection => connection)
61 stubs.verify_stubbed_calls
65 describe Google::APIClient do
66 let(:client) { Google::APIClient.new }
68 it 'should make its version number available' do
69 Google::APIClient::VERSION::STRING.should be_instance_of(String)
72 it 'should default to OAuth 2' do
73 Signet::OAuth2::Client.should === client.authorization
76 it_should_behave_like 'configurable user agent'
78 describe 'configured for OAuth 1' do
80 client.authorization = :oauth_1
83 it 'should use the default OAuth1 client configuration' do
84 client.authorization.temporary_credential_uri.to_s.should ==
85 'https://www.google.com/accounts/OAuthGetRequestToken'
86 client.authorization.authorization_uri.to_s.should include(
87 'https://www.google.com/accounts/OAuthAuthorizeToken'
89 client.authorization.token_credential_uri.to_s.should ==
90 'https://www.google.com/accounts/OAuthGetAccessToken'
91 client.authorization.client_credential_key.should == 'anonymous'
92 client.authorization.client_credential_secret.should == 'anonymous'
95 it_should_behave_like 'configurable user agent'
98 describe 'configured for OAuth 2' do
100 client.authorization = :oauth_2
104 it_should_behave_like 'configurable user agent'
107 describe 'when executing requests' do
109 client.authorization = :oauth_2
110 @connection = Faraday.new(:url => 'https://www.googleapis.com') do |builder|
111 stubs = Faraday::Adapter::Test::Stubs.new do |stub|
112 stub.get('/test') do |env|
113 env[:request_headers]['Authorization'].should == 'Bearer 12345'
116 builder.adapter(:test, stubs)
120 it 'should use default authorization' do
121 client.authorization.access_token = "12345"
122 client.execute(:http_method => :get,
123 :uri => 'https://www.googleapis.com/test',
124 :connection => @connection)
127 it 'should use request scoped authorization when provided' do
128 client.authorization.access_token = "abcdef"
129 new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
130 client.execute(:http_method => :get,
131 :uri => 'https://www.googleapis.com/test',
132 :connection => @connection,
133 :authorization => new_auth)