Begin consolidation of request building in reference. Further changes coming to simpl...
[arvados.git] / spec / google / api_client_spec.rb
1 # Copyright 2010 Google Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 require 'spec_helper'
16
17 require 'faraday'
18 require 'faraday/utils'
19 require 'signet/oauth_1/client'
20 require 'google/api_client'
21 require 'google/api_client/version'
22
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'
27   end
28
29   it 'should allow the user agent to be set to nil' do
30     client.user_agent = nil
31     client.user_agent.should == nil
32   end
33
34   it 'should not allow the user agent to be used with bogus values' do
35     (lambda do
36       client.user_agent = 42
37       client.transmit(
38         ['GET', 'http://www.google.com/', [], []]
39       )
40     end).should raise_error(TypeError)
41   end
42
43   it 'should transmit a User-Agent header when sending requests' do
44     client.user_agent = 'Custom User Agent/1.2.3'
45
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
51         [200, {}, ['']]
52       end
53     end
54     connection = Faraday.new(:url => 'https://www.google.com') do |builder|
55       builder.adapter(:test, stubs)
56     end
57     request = connection.build_request(:get) do |req|
58       req.url('http://www.google.com/')
59     end
60     client.transmit(:request => request, :connection => connection)
61     stubs.verify_stubbed_calls
62   end
63 end
64
65 describe Google::APIClient do
66   let(:client) { Google::APIClient.new }
67
68   it 'should make its version number available' do
69     Google::APIClient::VERSION::STRING.should be_instance_of(String)
70   end
71
72   it 'should default to OAuth 2' do
73     Signet::OAuth2::Client.should === client.authorization
74   end
75
76   it_should_behave_like 'configurable user agent'
77
78   describe 'configured for OAuth 1' do
79     before do
80       client.authorization = :oauth_1
81     end
82
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'
88       )
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'
93     end
94
95     it_should_behave_like 'configurable user agent'
96   end
97
98   describe 'configured for OAuth 2' do
99     before do
100       client.authorization = :oauth_2
101     end
102
103     # TODO
104     it_should_behave_like 'configurable user agent'
105   end
106   
107   describe 'when executing requests' do
108     before 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'
114           end
115         end
116         builder.adapter(:test, stubs)
117       end
118     end
119      
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)
125     end
126
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)
134     end
135   end  
136 end