deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject [3/3] git commit: Tests: Added tests for Rack extensions
Date Thu, 30 Aug 2012 09:48:47 GMT
Tests: Added tests for Rack extensions

* These are tests for Rack middleware extensions that
  Deltacloud use.

* Test are part of 'test:base' Rake task


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

Branch: refs/heads/master
Commit: e6694466250f19fe2c44d180c941e0976031d7aa
Parents: 3cb4248
Author: Michal Fojtik <mfojtik@redhat.com>
Authored: Wed Aug 29 13:20:59 2012 +0200
Committer: Michal fojtik <mfojtik@redhat.com>
Committed: Wed Aug 29 13:20:59 2012 +0200

----------------------------------------------------------------------
 server/Rakefile                                    |    1 +
 server/tests/helpers/rack/common.rb                |    3 +
 server/tests/helpers/rack/rack_accept_test.rb      |   65 +++++++++++++
 server/tests/helpers/rack/rack_date_test.rb        |   27 ++++++
 .../tests/helpers/rack/rack_driver_select_test.rb  |   53 +++++++++++
 server/tests/helpers/rack/rack_etag_test.rb        |   29 ++++++
 .../tests/helpers/rack/rack_matrix_params_test.rb  |   72 +++++++++++++++
 7 files changed, 250 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/e6694466/server/Rakefile
----------------------------------------------------------------------
diff --git a/server/Rakefile b/server/Rakefile
index f38b05e..fb40f12 100644
--- a/server/Rakefile
+++ b/server/Rakefile
@@ -164,6 +164,7 @@ namespace :test do
     end
     t.test_files = FileList[
       'tests/helpers/core_ext/*test.rb',        # Deltacloud extensions (core_ext) and other
helpers
+      'tests/helpers/rack/*test.rb',            # Rack extensions Deltacloud use
       'tests/drivers/base/*test.rb',            # Deltacloud drivers API tests
       'tests/drivers/models/*test.rb',          # Deltacloud models tests
       'tests/deltacloud/*test.rb',              # Deltacloud internal API tests

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/e6694466/server/tests/helpers/rack/common.rb
----------------------------------------------------------------------
diff --git a/server/tests/helpers/rack/common.rb b/server/tests/helpers/rack/common.rb
new file mode 100644
index 0000000..5d67108
--- /dev/null
+++ b/server/tests/helpers/rack/common.rb
@@ -0,0 +1,3 @@
+require 'sinatra/base'
+require_relative '../../test_helper.rb'
+require_relative '../../../lib/sinatra.rb'

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/e6694466/server/tests/helpers/rack/rack_accept_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/helpers/rack/rack_accept_test.rb b/server/tests/helpers/rack/rack_accept_test.rb
new file mode 100644
index 0000000..30a5fca
--- /dev/null
+++ b/server/tests/helpers/rack/rack_accept_test.rb
@@ -0,0 +1,65 @@
+require 'rubygems'
+require 'require_relative' if RUBY_VERSION < '1.9'
+require_relative '../../test_helper.rb'
+require_relative './common.rb'
+
+class TestAcceptApp < Sinatra::Base
+  use Rack::Accept
+  use Rack::MediaType
+  register Rack::RespondTo
+  helpers Rack::RespondTo::Helpers
+  get '/' do
+    respond_to do |format|
+      format.html { 'html' }
+      format.xml { 'xml' }
+      format.json { 'json' }
+    end
+  end
+end
+
+describe TestAcceptApp do
+
+  before do
+    def app; TestAcceptApp; end
+  end
+
+  it 'should return HTML when Accept header requests HTML media type' do
+    header 'Accept', 'text/html'
+    get '/'
+    headers['Content-Type'].must_equal 'text/html'
+    response_body.strip.must_equal 'html'
+  end
+
+  it 'should return HTML when Accept header is set by Firefox' do
+    header 'Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
+    get '/'
+    headers['Content-Type'].must_equal 'text/html'
+    response_body.strip.must_equal 'html'
+  end
+
+  it 'should return XML when Accept header requests XML media type' do
+    header 'Accept', 'application/xml'
+    get '/'
+    headers['Content-Type'].must_equal 'application/xml'
+    response_body.strip.must_equal 'xml'
+  end
+
+  it 'should return JSON when Accept header requests JSON media type' do
+    header 'Accept', 'application/json'
+    get '/'
+    headers['Content-Type'].must_equal 'application/json'
+    response_body.strip.must_equal 'json'
+  end
+
+  it 'should return default media type when no Accept header is set' do
+    get '/'
+    headers['Content-Type'].must_equal 'application/xml'
+  end
+
+  it 'should return error when unknown Accept header is set' do
+    header 'Accept', 'unknown/header'
+    get '/'
+    status.must_equal 406
+  end
+
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/e6694466/server/tests/helpers/rack/rack_date_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/helpers/rack/rack_date_test.rb b/server/tests/helpers/rack/rack_date_test.rb
new file mode 100644
index 0000000..c011dce
--- /dev/null
+++ b/server/tests/helpers/rack/rack_date_test.rb
@@ -0,0 +1,27 @@
+require 'rubygems'
+require 'require_relative' if RUBY_VERSION < '1.9'
+require_relative '../../test_helper.rb'
+require_relative './common.rb'
+
+class TestDateApp < Sinatra::Base
+  use Rack::Date
+  get '/' do
+    'OK'
+  end
+end
+
+describe TestDateApp do
+
+  before do
+    def app; TestDateApp; end
+  end
+
+  it 'add the Date header to all responses' do
+    get '/'
+    status.must_equal 200
+    response_body.wont_be_empty
+    headers['Date'].wont_be_empty
+    Time.parse(headers['Date']).must_be_instance_of Time
+  end
+
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/e6694466/server/tests/helpers/rack/rack_driver_select_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/helpers/rack/rack_driver_select_test.rb b/server/tests/helpers/rack/rack_driver_select_test.rb
new file mode 100644
index 0000000..47f52ad
--- /dev/null
+++ b/server/tests/helpers/rack/rack_driver_select_test.rb
@@ -0,0 +1,53 @@
+require 'rubygems'
+require 'require_relative' if RUBY_VERSION < '1.9'
+require_relative '../../test_helper.rb'
+require_relative './common.rb'
+
+class TestDriverApp < Sinatra::Base
+  use Rack::DriverSelect
+  get '/' do
+    headers 'Driver' => Thread.current[:driver]
+    headers 'Provider' => Thread.current[:provider]
+    'OK'
+  end
+end
+
+describe TestDriverApp do
+
+  before do
+    def app; TestDriverApp; end
+  end
+
+  it 'should set the driver correctly when using X-Deltacloud-Driver request header' do
+    header 'X-Deltacloud-Driver', 'ec2'
+    get '/'
+    headers['Driver'].wont_be_empty
+    headers['Driver'].must_equal 'ec2'
+    headers['Provider'].must_be_nil
+    header 'X-Deltacloud-Driver', 'test'
+    get '/'
+    headers['Driver'].wont_be_empty
+    headers['Driver'].must_equal 'test'
+    headers['Provider'].must_be_nil
+  end
+
+  it 'should set the provider correctly when using X-Deltacloud-Provider header' do
+    header 'X-Deltacloud-Provider', 'default'
+    get '/'
+    headers['Provider'].wont_be_empty
+    headers['Provider'].must_equal 'default'
+    header 'X-Deltacloud-Provider', 'http://someurl.com:8774/api;1234-1234-1235-1235'
+    get '/'
+    headers['Provider'].wont_be_nil
+    headers['Provider'].must_equal 'http://someurl.com:8774/api;1234-1234-1235-1235'
+  end
+
+  it 'should set both provider and driver' do
+    header 'X-Deltacloud-Provider', 'default'
+    header 'X-Deltacloud-Driver', 'test'
+    get '/'
+    headers['Provider'].must_equal 'default'
+    headers['Driver'].must_equal 'test'
+  end
+
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/e6694466/server/tests/helpers/rack/rack_etag_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/helpers/rack/rack_etag_test.rb b/server/tests/helpers/rack/rack_etag_test.rb
new file mode 100644
index 0000000..ba06067
--- /dev/null
+++ b/server/tests/helpers/rack/rack_etag_test.rb
@@ -0,0 +1,29 @@
+require 'rubygems'
+require 'require_relative' if RUBY_VERSION < '1.9'
+require_relative '../../test_helper.rb'
+require_relative './common.rb'
+
+class TestEtagApp < Sinatra::Base
+  use Rack::ETag
+  get '/' do
+    params[:test]
+  end
+end
+
+describe TestEtagApp do
+
+  before do
+    def app; TestEtagApp; end
+  end
+
+  it 'add the ETag header to all responses' do
+    get '/?test=1'
+    status.must_equal 200
+    response_body.wont_be_empty
+    headers['ETag'].wont_be_empty
+    headers['ETag'].must_equal 'c4ca4238a0b923820dcc509a6f75849b'
+    get '/?test=2'
+    headers['ETag'].must_equal 'c81e728d9d4c2f636f067f89cc14862c'
+  end
+
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/e6694466/server/tests/helpers/rack/rack_matrix_params_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/helpers/rack/rack_matrix_params_test.rb b/server/tests/helpers/rack/rack_matrix_params_test.rb
new file mode 100644
index 0000000..135544b
--- /dev/null
+++ b/server/tests/helpers/rack/rack_matrix_params_test.rb
@@ -0,0 +1,72 @@
+require 'rubygems'
+require 'require_relative' if RUBY_VERSION < '1.9'
+require_relative '../../test_helper.rb'
+require_relative './common.rb'
+
+require 'json'
+
+class TestMatrixApp < Sinatra::Base
+  use Rack::MatrixParams
+  get '/' do
+    params.to_json
+  end
+  get '/test' do
+    params.to_json
+  end
+  get '/test/books' do
+    params.to_json
+  end
+end
+
+describe TestMatrixApp do
+
+  before do
+    def app; TestMatrixApp; end
+  end
+
+  it 'should set matrix param for entrypoint' do
+    get '/;test=1'
+    status.must_equal 200
+    json['test'].must_equal '1'
+  end
+
+  it 'should set multiple matrix params for entrypoint' do
+    get '/;test=1;foo=bar'
+    status.must_equal 200
+    json['test'].must_equal '1'
+    json['foo'].must_equal 'bar'
+  end
+
+  it 'should set matrix param for first part of URI' do
+    get '/test;foo=bar'
+    status.must_equal 200
+    json['test']['foo'].must_equal 'bar'
+  end
+
+  it 'should set multiple matrix params for first part of URI' do
+    get '/test;foo=bar;test1=blah'
+    status.must_equal 200
+    json['test']['foo'].must_equal 'bar'
+    json['test']['test1'].must_equal 'blah'
+  end
+
+  it 'should set matrix params for the last part of URI' do
+    get '/test/books;foo=bar'
+    status.must_equal 200
+    json['books']['foo'].must_equal 'bar'
+  end
+
+  it 'should set matrix params for multiple parts of URI' do
+    get '/test;test=1/books;foo=bar'
+    status.must_equal 200
+    json['books']['foo'].must_equal 'bar'
+    json['test']['test'].must_equal '1'
+  end
+
+  it 'should handle matrix params with wrong syntax' do
+    get '/test;;;/books;foo=bar'
+    json['books']['foo'].must_equal 'bar'
+    json['test'].must_be_nil
+  end
+
+end


Mime
View raw message