Begin consolidation of request building in reference. Further changes coming to simpl...
[arvados.git] / spec / google / api_client_spec.rb
index e5ee0d7daf8fcfe8a0cca6054521891e18d7cca0..ae25518fbb350f75cffceeeb08ac80cb5dd9f2e9 100644 (file)
 
 require 'spec_helper'
 
+require 'faraday'
+require 'faraday/utils'
 require 'signet/oauth_1/client'
-require 'httpadapter/adapters/net_http'
-require 'httpadapter/adapters/mock'
-
 require 'google/api_client'
 require 'google/api_client/version'
-require 'google/api_client/parsers/json_parser'
 
 shared_examples_for 'configurable user agent' do
   it 'should allow the user agent to be modified' do
-    @client.user_agent = 'Custom User Agent/1.2.3'
-    @client.user_agent.should == 'Custom User Agent/1.2.3'
+    client.user_agent = 'Custom User Agent/1.2.3'
+    client.user_agent.should == 'Custom User Agent/1.2.3'
   end
 
   it 'should allow the user agent to be set to nil' do
-    @client.user_agent = nil
-    @client.user_agent.should == nil
+    client.user_agent = nil
+    client.user_agent.should == nil
   end
 
-  it 'should not allow the user agent to be set to bogus values' do
+  it 'should not allow the user agent to be used with bogus values' do
     (lambda do
-      @client.user_agent = 42
+      client.user_agent = 42
+      client.transmit(
+        ['GET', 'http://www.google.com/', [], []]
+      )
     end).should raise_error(TypeError)
   end
 
   it 'should transmit a User-Agent header when sending requests' do
-    @client.user_agent = 'Custom User Agent/1.2.3'
-    request = ['GET', 'http://www.google.com/', [], []]
-    adapter = HTTPAdapter::MockAdapter.request_adapter do |request, connection|
-      method, uri, headers, body = request
-      headers.should be_any { |k, v| k.downcase == 'user-agent' }
-      headers.each do |k, v|
-        v.should == @client.user_agent if k.downcase == 'user-agent'
+    client.user_agent = 'Custom User Agent/1.2.3'
+
+    stubs = Faraday::Adapter::Test::Stubs.new do |stub|
+      stub.get('/') do |env|
+        headers = env[:request_headers]
+        headers.should have_key('User-Agent')
+        headers['User-Agent'].should == client.user_agent
+        [200, {}, ['']]
       end
-      [200, [], ['']]
     end
-    @client.transmit_request(request, adapter)
+    connection = Faraday.new(:url => 'https://www.google.com') do |builder|
+      builder.adapter(:test, stubs)
+    end
+    request = connection.build_request(:get) do |req|
+      req.url('http://www.google.com/')
+    end
+    client.transmit(:request => request, :connection => connection)
+    stubs.verify_stubbed_calls
   end
 end
 
-describe Google::APIClient, 'with default configuration' do
-  before do
-    @client = Google::APIClient.new
-  end
+describe Google::APIClient do
+  let(:client) { Google::APIClient.new }
 
   it 'should make its version number available' do
-    ::Google::APIClient::VERSION::STRING.should be_instance_of(String)
-  end
-
-  it 'should use the default JSON parser' do
-    @client.parser.should be(Google::APIClient::JSONParser)
+    Google::APIClient::VERSION::STRING.should be_instance_of(String)
   end
 
-  it 'should not use an authorization mechanism' do
-    @client.authorization.should be_nil
+  it 'should default to OAuth 2' do
+    Signet::OAuth2::Client.should === client.authorization
   end
 
   it_should_behave_like 'configurable user agent'
-end
-
-describe Google::APIClient, 'with default oauth configuration' do
-  before do
-    @client = Google::APIClient.new(:authorization => :oauth_1)
-  end
 
-  it 'should make its version number available' do
-    ::Google::APIClient::VERSION::STRING.should be_instance_of(String)
-  end
+  describe 'configured for OAuth 1' do
+    before do
+      client.authorization = :oauth_1
+    end
 
-  it 'should use the default JSON parser' do
-    @client.parser.should be(Google::APIClient::JSONParser)
-  end
+    it 'should use the default OAuth1 client configuration' do
+      client.authorization.temporary_credential_uri.to_s.should ==
+        'https://www.google.com/accounts/OAuthGetRequestToken'
+      client.authorization.authorization_uri.to_s.should include(
+        'https://www.google.com/accounts/OAuthAuthorizeToken'
+      )
+      client.authorization.token_credential_uri.to_s.should ==
+        'https://www.google.com/accounts/OAuthGetAccessToken'
+      client.authorization.client_credential_key.should == 'anonymous'
+      client.authorization.client_credential_secret.should == 'anonymous'
+    end
 
-  it 'should use the default OAuth1 client configuration' do
-    @client.authorization.temporary_credential_uri.to_s.should ==
-      'https://www.google.com/accounts/OAuthGetRequestToken'
-    @client.authorization.authorization_uri.to_s.should include(
-      'https://www.google.com/accounts/OAuthAuthorizeToken'
-    )
-    @client.authorization.token_credential_uri.to_s.should ==
-      'https://www.google.com/accounts/OAuthGetAccessToken'
-    @client.authorization.client_credential_key.should == 'anonymous'
-    @client.authorization.client_credential_secret.should == 'anonymous'
+    it_should_behave_like 'configurable user agent'
   end
 
-  it_should_behave_like 'configurable user agent'
-end
-
-describe Google::APIClient, 'with custom pluggable parser' do
-  before do
-    class FakeJsonParser
+  describe 'configured for OAuth 2' do
+    before do
+      client.authorization = :oauth_2
     end
 
-    @client = Google::APIClient.new(:parser => FakeJsonParser.new)
-  end
-
-  it 'should use the custom parser' do
-    @client.parser.should be_instance_of(FakeJsonParser)
+    # TODO
+    it_should_behave_like 'configurable user agent'
   end
+  
+  describe 'when executing requests' do
+    before do
+      client.authorization = :oauth_2
+      @connection = Faraday.new(:url => 'https://www.googleapis.com') do |builder|
+        stubs = Faraday::Adapter::Test::Stubs.new do |stub|
+          stub.get('/test') do |env|
+            env[:request_headers]['Authorization'].should == 'Bearer 12345'
+          end
+        end
+        builder.adapter(:test, stubs)
+      end
+    end
+     
+    it 'should use default authorization' do
+      client.authorization.access_token = "12345"
+      client.execute(:http_method => :get, 
+                      :uri => 'https://www.googleapis.com/test',
+                      :connection => @connection)
+    end
 
-  it_should_behave_like 'configurable user agent'
+    it 'should use request scoped authorization when provided' do
+      client.authorization.access_token = "abcdef"
+      new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
+      client.execute(:http_method => :get, 
+                      :uri => 'https://www.googleapis.com/test',
+                      :connection => @connection,
+                      :authorization => new_auth)
+    end
+  end  
 end