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 gem 'faraday', '~> 0.8.1'
19 require 'faraday/utils'
21 gem 'signet', '~> 0.4.0'
22 require 'signet/oauth_1/client'
24 require 'google/api_client'
25 require 'google/api_client/version'
27 shared_examples_for 'configurable user agent' do
28 it 'should allow the user agent to be modified' do
29 client.user_agent = 'Custom User Agent/1.2.3'
30 client.user_agent.should == 'Custom User Agent/1.2.3'
33 it 'should allow the user agent to be set to nil' do
34 client.user_agent = nil
35 client.user_agent.should == nil
38 it 'should not allow the user agent to be used with bogus values' do
40 client.user_agent = 42
42 ['GET', 'http://www.google.com/', [], []]
44 end).should raise_error(TypeError)
47 it 'should transmit a User-Agent header when sending requests' do
48 client.user_agent = 'Custom User Agent/1.2.3'
49 request = Faraday::Request.new(:get) do |req|
50 req.url('http://www.google.com/')
52 stubs = Faraday::Adapter::Test::Stubs.new do |stub|
53 stub.get('/') do |env|
54 headers = env[:request_headers]
55 headers.should have_key('User-Agent')
56 headers['User-Agent'].should == client.user_agent
60 connection = Faraday.new(:url => 'https://www.google.com') do |builder|
61 builder.adapter(:test, stubs)
63 client.transmit(:request => request, :connection => connection)
64 stubs.verify_stubbed_calls
68 describe Google::APIClient do
69 let(:client) { Google::APIClient.new }
71 it 'should make its version number available' do
72 Google::APIClient::VERSION::STRING.should be_instance_of(String)
75 it 'should default to OAuth 2' do
76 Signet::OAuth2::Client.should === client.authorization
79 it_should_behave_like 'configurable user agent'
81 describe 'configured for OAuth 1' do
83 client.authorization = :oauth_1
86 it 'should use the default OAuth1 client configuration' do
87 client.authorization.temporary_credential_uri.to_s.should ==
88 'https://www.google.com/accounts/OAuthGetRequestToken'
89 client.authorization.authorization_uri.to_s.should include(
90 'https://www.google.com/accounts/OAuthAuthorizeToken'
92 client.authorization.token_credential_uri.to_s.should ==
93 'https://www.google.com/accounts/OAuthGetAccessToken'
94 client.authorization.client_credential_key.should == 'anonymous'
95 client.authorization.client_credential_secret.should == 'anonymous'
98 it_should_behave_like 'configurable user agent'
101 describe 'configured for OAuth 2' do
103 client.authorization = :oauth_2
107 it_should_behave_like 'configurable user agent'
110 describe 'when executing requests' do
112 client.authorization = :oauth_2
113 @connection = Faraday.new(:url => 'https://www.googleapis.com') do |builder|
114 stubs = Faraday::Adapter::Test::Stubs.new do |stub|
115 stub.get('/test') do |env|
116 env[:request_headers]['Authorization'].should == 'Bearer 12345'
119 builder.adapter(:test, stubs)
123 it 'should use default authorization' do
124 client.authorization.access_token = "12345"
125 client.execute(:http_method => :get,
126 :uri => 'https://www.googleapis.com/test',
127 :connection => @connection)
130 it 'should use request scoped authorization when provided' do
131 client.authorization.access_token = "abcdef"
132 new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
133 client.execute(:http_method => :get,
134 :uri => 'https://www.googleapis.com/test',
135 :connection => @connection,
136 :authorization => new_auth)