Examples have been moved out into their own repository.
[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 'signet/oauth_1/client'
18 require 'httpadapter/adapters/net_http'
19 require 'httpadapter/adapters/mock'
20
21 require 'google/api_client'
22 require 'google/api_client/version'
23 require 'google/api_client/parsers/json_parser'
24
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'
29   end
30
31   it 'should allow the user agent to be set to nil' do
32     @client.user_agent = nil
33     @client.user_agent.should == nil
34   end
35
36   it 'should not allow the user agent to be used with bogus values' do
37     (lambda do
38       @client.user_agent = 42
39       @client.transmit(
40         ['GET', 'http://www.google.com/', [], []]
41       )
42     end).should raise_error(TypeError)
43   end
44
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'
53       end
54       [200, [], ['']]
55     end
56     @client.transmit(request, adapter)
57   end
58 end
59
60 describe Google::APIClient do
61   before do
62     @client = Google::APIClient.new
63   end
64
65   it 'should make its version number available' do
66     Google::APIClient::VERSION::STRING.should be_instance_of(String)
67   end
68
69   it 'should default to OAuth 2' do
70     Signet::OAuth2::Client.should === @client.authorization
71   end
72
73   it_should_behave_like 'configurable user agent'
74
75   describe 'configured for OAuth 1' do
76     before do
77       @client.authorization = :oauth_1
78     end
79
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'
85       )
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'
90     end
91
92     it_should_behave_like 'configurable user agent'
93   end
94
95   describe 'configured for OAuth 2' do
96     before do
97       @client.authorization = :oauth_2
98     end
99
100     # TODO
101     it_should_behave_like 'configurable user agent'
102   end
103 end