deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject [23/31] git commit: Revamp the get helper method
Date Wed, 25 Jul 2012 13:56:38 GMT
Revamp the get helper method

  * Make it look a little more like the one from Rack::Test
  * Introduce a few magic params to make setting important headers
    easy


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/783a9145
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/783a9145
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/783a9145

Branch: refs/heads/master
Commit: 783a91456a129688058cff5b44edaafcd8183f04
Parents: de3543c
Author: David Lutterkort <lutter@redhat.com>
Authored: Fri Jul 20 17:12:33 2012 -0700
Committer: marios <marios@redhat.com>
Committed: Tue Jul 24 12:01:36 2012 +0300

----------------------------------------------------------------------
 tests/deltacloud/base_api_test.rb |   53 ++++++++++++++++++-------------
 tests/deltacloud/buckets_test.rb  |   46 ++++++++++++++--------------
 tests/deltacloud/test_setup.rb    |   44 +++++++++++++++++++++++---
 3 files changed, 93 insertions(+), 50 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/783a9145/tests/deltacloud/base_api_test.rb
----------------------------------------------------------------------
diff --git a/tests/deltacloud/base_api_test.rb b/tests/deltacloud/base_api_test.rb
index 3fe3109..0eeeb55 100644
--- a/tests/deltacloud/base_api_test.rb
+++ b/tests/deltacloud/base_api_test.rb
@@ -19,108 +19,117 @@ require "deltacloud/test_setup.rb"
 
 describe "Deltacloud API Entry Point" do
 
+  # Get the API entrypoint without any authentication
+  def get_api(params={})
+    get("/", params.update(:noauth => true))
+  end
+
   it 'return status 200 OK when accessing API entrypoint' do
-    res = get
+    res = get_api
     res.code.must_equal 200
   end
 
   it 'advertise the current driver in API entrypoint' do
-    res = get({:accept => :xml})
+    res = get_api :accept => :xml
     driver = xml_response(res).root[:driver]
     driver.wont_be_nil
     DRIVERS.include?(driver).must_equal true
   end
 
   it 'advertise the current API version in API entrypoint' do
-    res = get({:accept => :xml})
+    res = get_api :accept => :xml
     version = xml_response(res).root[:version]
     version.wont_be_nil
     version.must_equal API_VERSION
   end
 
   it 'advertise the current API version in HTTP headers' do
-    res = get
+    res = get_api
     res.headers[:server].must_equal "Apache-Deltacloud/#{API_VERSION}"
   end
 
   it 'must include the ETag in HTTP headers' do
-    res = get
+    res = get_api
     res.headers[:etag].wont_be_nil
   end
 
   it 'advertise collections in API entrypoint' do
-    res = get({:accept => :xml})
+    res = get_api :accept => :xml
     xml_response(res).xpath('//api/link').wont_be_empty
   end
 
   it 'include the :href and :rel attribute for each collection in API entrypoint' do
-    xml_response(get({:accept => :xml})).xpath("//api/link").each do |collection|
+    xml_response(get_api(:accept => :xml)).xpath("//api/link").each do |collection|
       collection[:href].wont_be_nil
       collection[:rel].wont_be_nil
     end
   end
 
   it 'uses the absolute URI in the :href attribute for each collection in API entrypoint'
do
-    xml_response(get({:accept => :xml})).xpath("//api/link").each do |collection|
+    xml_response(get_api(:accept => :xml)).xpath("//api/link").each do |collection|
       collection[:href].must_match /^http/
     end
   end
 
   it 'advertise features for some collections in API entrypoint' do
-    xml_doc = xml_response(get({:accept => :xml}))
+    xml_doc = xml_response(get_api(:accept => :xml))
     xml_doc.xpath("//api/link/feature").wont_be_empty
   end
 
   it 'advertise the name of the feature for some collections in API entrypoint' do
-    xml_response(get({:accept => :xml})).xpath("//api/link/feature").each do |feature|
+    xml_response(get_api(:accept => :xml)).xpath("//api/link/feature").each do |feature|
       feature[:name].wont_be_nil
     end
   end
 
   it 'must change the media type from XML to JSON using Accept headers' do
-    res = get({:accept => :json})
+    res = get_api(:accept => :json)
     res.headers[:content_type].must_equal 'application/json'
   end
 
   it 'must change the media type to JSON using the "?format" parameter in URL' do
-    res = get({}, "?format=json")
+    res = get_api(:format => :json)
     res.headers[:content_type].must_equal 'application/json'
   end
 
   it 'must change the driver when using X-Deltacloud-Driver HTTP header' do
-    res = xml_response(get({'X-Deltacloud-Driver'=> 'ec2', :accept=> :xml}))
+    res = xml_response(get_api(:driver => 'ec2', :accept=> :xml))
     res.root[:driver].must_equal 'ec2'
-    res = xml_response(get({'X-Deltacloud-Driver'=> 'mock', :accept=> :xml}))
+    res = xml_response(get_api(:driver => 'mock', :accept=> :xml))
     res.root[:driver].must_equal 'mock'
   end
 
   it 'must change the features when driver is swapped using HTTP headers' do
-    res = xml_response(get({'X-Deltacloud-Driver'=> 'ec2', :accept=> :xml}))
+    res = xml_response(get_api(:driver => 'ec2', :accept=> :xml))
     # The 'user_name' feature is not supported currently for the EC2 driver
     (res/'api/link/feature').map { |f| f[:name] }.wont_include 'user_name'
-    res = xml_response(get({'X-Deltacloud-Driver'=> 'mock', :accept=> :xml}))
+    res = xml_response(get_api(:driver => 'mock', :accept=> :xml))
     # But it's supported in Mock driver
     (res/'api/link/feature').map { |f| f[:name] }.must_include 'user_name'
   end
 
   it 'must re-validate the driver credentials when using "?force_auth" parameter in URL'
do
-    proc {get({ :params => {:force_auth => '1'} })}.must_raise RestClient::Request::Unauthorized
-    res = get({ "X-Deltacloud-Driver"=>"mock", :params=>{:force_auth => '1'}, :Authorization=>"Basic
#{Base64.encode64('mockuser:mockpassword')}" })
+    proc do
+      get_api(:force_auth => '1')
+    end.must_raise RestClient::Request::Unauthorized
+
+    res = get("/", :driver => "mock", :force_auth => '1',
+                   :user => "mockuser", :password => "mockpassword")
     res.code.must_equal 200
   end
 
   it 'must change the API PROVIDER using the /api;provider matrix parameter in URI' do
-    res = xml_response(get({}, ';provider=test1'))
+    res = xml_response(get(';provider=test1', :public => true))
     res.root[:provider].wont_be_nil
     res.root[:provider].must_equal 'test1'
-    res = xml_response(get({}, ';provider=test2'))
+    res = xml_response(get(';provider=test2', :public => true))
     res.root[:provider].must_equal 'test2'
   end
 
   it 'must change the API DRIVER using the /api;driver matrix parameter in URI' do
-    res = xml_response(get({}, ';driver=ec2'))
+    res = xml_response(get(';driver=ec2', :public => true))
     res.root[:driver].must_equal 'ec2'
-    res = xml_response(get({}, ';driver=mock'))
+    res = xml_response(get(';driver=mock', :public => true))
     res.root[:driver].must_equal 'mock'
   end
 

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/783a9145/tests/deltacloud/buckets_test.rb
----------------------------------------------------------------------
diff --git a/tests/deltacloud/buckets_test.rb b/tests/deltacloud/buckets_test.rb
index 2f7406b..58d6d6c 100644
--- a/tests/deltacloud/buckets_test.rb
+++ b/tests/deltacloud/buckets_test.rb
@@ -34,16 +34,16 @@ describe 'Deltacloud API buckets collection' do
   }
 
   it 'must advertise the buckets collection in API entrypoint' do
-    res = xml_response(get)
+    res = xml_response(get("/"))
     (res/'api/link[@rel=buckets]').wont_be_empty
   end
 
   it 'must require authentication to access the "bucket" collection' do
-    proc {  get({},BUCKETS) }.must_raise RestClient::Request::Unauthorized
+    proc {  get(BUCKETS, :noauth => true) }.must_raise RestClient::Request::Unauthorized
   end
 
   it 'should respond with HTTP_OK when accessing the :buckets collection with authentication'
do
-    res = get({}, BUCKETS, true)
+    res = get(BUCKETS)
     res.code.must_equal 200
   end
 
@@ -58,7 +58,7 @@ describe 'Deltacloud API buckets collection' do
     xml_res.xpath("//bucket").size.must_equal 1
     xml_res.xpath("//bucket")[0][:id].must_equal bucket_name
     #GET bucket
-    res = get({}, BUCKETS+"/"+bucket_name, true)
+    res = get(BUCKETS+"/"+bucket_name)
     res.code.must_equal 200
     #DELETE bucket
     res = delete({}, BUCKETS+"/"+bucket_name)
@@ -73,57 +73,57 @@ describe 'Deltacloud API buckets collection' do
 
 
   it 'should support the JSON media type' do
-    res = get({:accept=>:json}, BUCKETS, true)
+    res = get(BUCKETS, :accept=>:json)
     res.code.must_equal 200
     res.headers[:content_type].must_equal 'application/json'
     assert_silent {JSON.parse(res)}
   end
 
   it 'must include the ETag in HTTP headers' do
-    res = get({}, BUCKETS, true)
+    res = get(BUCKETS)
     res.headers[:etag].wont_be_nil
   end
 
   it 'must have the "buckets" element on top level' do
-    xml_res = xml_response(get({:accept=>:xml}, BUCKETS, true))
+    xml_res = xml_response(get(BUCKETS, :accept=>:xml))
     xml_res.root.name.must_equal 'buckets'
   end
 
   it 'must have some "bucket" elements inside "buckets"' do
-    xml_res = xml_response(get({:accept=>:xml}, BUCKETS, true))
+    xml_res = xml_response(get(BUCKETS, :accept=>:xml))
     (xml_res/'buckets/bucket').wont_be_empty
   end
 
   it 'must provide the :id attribute for each bucket in collection' do
-    xml_res = xml_response(get({:accept=>:xml}, BUCKETS, true))
+    xml_res = xml_response(get(BUCKETS, :accept=>:xml))
     (xml_res/'buckets/bucket').each do |r|
       r[:id].wont_be_nil
     end
   end
 
   it 'must include the :href attribute for each "bucket" element in collection' do
-    xml_res = xml_response(get({:accept=>:xml}, BUCKETS, true))
+    xml_res = xml_response(get(BUCKETS, :accept=>:xml))
     (xml_res/'buckets/bucket').each do |r|
       r[:href].wont_be_nil
     end
   end
 
   it 'must use the absolute URL in each :href attribute' do
-    xml_res = xml_response(get({:accept=>:xml}, BUCKETS, true))
+    xml_res = xml_response(get(BUCKETS, :accept=>:xml))
     (xml_res/'buckets/bucket').each do |r|
       r[:href].must_match /^http/
     end
   end
 
   it 'must have the URL ending with the :id of the bucket' do
-    xml_res = xml_response(get({:accept=>:xml}, BUCKETS, true))
+    xml_res = xml_response(get(BUCKETS, :accept=>:xml))
     (xml_res/'buckets/bucket').each do |r|
       r[:href].must_match /#{r[:id]}$/
     end
   end
 
   it 'must have the "name" element defined for each bucket in collection' do
-    xml_res = xml_response(get({:accept=>:xml}, BUCKETS, true))
+    xml_res = xml_response(get(BUCKETS, :accept => :xml))
     (xml_res/'buckets/bucket').each do |r|
       (r/'name').wont_be_nil
       (r/'name').wont_be_empty
@@ -131,7 +131,7 @@ describe 'Deltacloud API buckets collection' do
   end
 
   it 'must have the "size" element defined for each bucket in collection' do
-    xml_res = xml_response(get({:accept=>:xml}, BUCKETS, true))
+    xml_res = xml_response(get(BUCKETS, :accept => :xml))
     (xml_res/'buckets/bucket').each do |r|
       (r/'size').wont_be_nil
       (r/'size').wont_be_empty
@@ -139,26 +139,26 @@ describe 'Deltacloud API buckets collection' do
   end
 
   it 'must return 200 OK when following the URL in bucket element' do
-    xml_res = xml_response(get({:accept=>:xml}, BUCKETS, true))
+    xml_res = xml_response(get(BUCKETS, :accept => :xml))
     (xml_res/'buckets/bucket').each do |r|
-      bucket_res = RestClient.get(r[:href], {:Authorization=>BASIC_AUTH})
+      bucket_res = get r[:href]
       bucket_res.code.must_equal 200
     end
   end
 
   it 'must have the "name" element for the bucket and it should match with the one in collection'
do
-    xml_res = xml_response(get({:accept=>:xml}, BUCKETS, true))
+    xml_res = xml_response(get(BUCKETS, :accept => :xml))
     (xml_res/'buckets/bucket').each do |r|
-      bucket_xml = xml_response(get({:accept=>:xml}, BUCKETS+"/#{r[:id]}", true))
+      bucket_xml = xml_response(get(BUCKETS+"/#{r[:id]}", :accept=>:xml))
       (bucket_xml/'name').wont_be_empty
       (bucket_xml/'name').first.text.must_equal((r/'name').first.text)
     end
   end
 
   it 'all "blob" elements for the bucket should match the ones in collection' do
-    xml_res = xml_response(get({:accept=>:xml}, BUCKETS, true))
+    xml_res = xml_response(get(BUCKETS, :accept => :xml))
     (xml_res/'buckets/bucket').each do |r|
-      bucket_xml = xml_response(get({:accept=>:xml}, BUCKETS+"/#{r[:id]}", true))
+      bucket_xml = xml_response(get(BUCKETS+"/#{r[:id]}", :accept=>:xml))
       (bucket_xml/'bucket/blob').each do |b|
         b[:id].wont_be_nil
         b[:href].wont_be_nil
@@ -169,11 +169,11 @@ describe 'Deltacloud API buckets collection' do
   end
 
   it 'must allow to get all blobs details and the details should be set correctly' do
-    xml_res = xml_response(get({:accept=>:xml}, BUCKETS, true))
+    xml_res = xml_response(get(BUCKETS, :accept => :xml))
     (xml_res/'buckets/bucket').each do |r|
-      bucket_xml = xml_response(get({:accept=>:xml}, BUCKETS+"/#{r[:id]}", true))
+      bucket_xml = xml_response(get(BUCKETS+"/#{r[:id]}", :accept=>:xml))
       (bucket_xml/'bucket/blob').each do |b|
-        blob_xml = xml_response(get({:accept=>:xml}, BUCKETS+"/#{r[:id]}/#{b[:id]}", true))
+        blob_xml = xml_response(get(BUCKETS+"/#{r[:id]}/#{b[:id]}", :accept=>:xml))
         blob_xml.root.name.must_equal 'blob'
         blob_xml.root[:id].must_equal b[:id]
         (blob_xml/'bucket').wont_be_empty

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/783a9145/tests/deltacloud/test_setup.rb
----------------------------------------------------------------------
diff --git a/tests/deltacloud/test_setup.rb b/tests/deltacloud/test_setup.rb
index 14f6d8d..a46c1e7 100644
--- a/tests/deltacloud/test_setup.rb
+++ b/tests/deltacloud/test_setup.rb
@@ -48,11 +48,45 @@ def json_response(json)
   JSON.parse(json)
 end
 
-def get(params={}, path="", authenticate = false)
-  if authenticate
-    params.merge!({:Authorization=>BASIC_AUTH})
+# Make a GET request for +path+ and return the +RestClient::Response+. The
+# query string for the request is generated from +params+, with the
+# exception of a few special entries in params, which are used to set some
+# headers, and will not appear in the query string:
+#
+#   :noauth          : do not send an auth header
+#   :user, :password : use these for the auth header
+#   :accept          : can be :xml or :json, and sets the Accept header
+#   :driver, :provider : set driver and/or provider with the appropriate header
+#
+# If none of the auth relevant params are set, use the username and
+# password for the current driver from the config
+def get(path, params={})
+  path = "" if path == "/"
+  headers = {}
+  unless params.delete(:noauth)
+    if params[:user]
+      u = params.delete(:user)
+      p = params.delete(:password)
+      headers['Authorization'] = "Basic #{Base64.encode64("#{u}:#{p}")}"
+    else
+      headers['Authorization'] = BASIC_AUTH
+    end
+  end
+  headers["X-Deltacloud-Driver"] = params.delete(:driver) if params[:driver]
+  headers["X-Deltacloud-Provider"] = params.delete(:provider) if params[:providver]
+  headers["Accept"] = "application/#{params.delete(:accept)}" if params[:accept]
+
+  if path =~ /^https?:/
+    url = path
+  else
+    url = API_URL + path
+  end
+  url += "?" + params.map { |k,v| "#{k}=#{v}" }.join("&") unless params.empty?
+  if ENV["LOG"] && ENV["LOG"].include?("requests")
+    puts "GET #{url}"
+    headers.each { |k, v| puts "#{k}: #{v}" }
   end
-  RestClient.get API_URL+path, params
+  RestClient.get url, headers
 end
 
 def post(post_body = "", path= "", params={}, authenticate = false)
@@ -126,7 +160,7 @@ def delete_bucket_and_blob(bucket, blob)
 end
 
 def discover_features
-  res = xml_response(get)
+  res = xml_response(get("/"))
   features_hash = res.xpath("//api/link").inject({}) do |result, collection|
     result.merge!({collection[:rel] => []})
     collection.children.inject([]){|features, current_child| result[collection[:rel]] <<
current_child[:name] if current_child.name == "feature"}


Mime
View raw message