diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 41228d30d9f2399972bfd9044d5f801e787955f8..1e62def587df87672a28c9b1be3a73c5bccbcbb1 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,8 @@ *SVN* +* Integration tests: thoroughly test ActionController::Integration::Session. #6022 [Kevin Clark] + (tests skipped unless you `gem install mocha`) + * Added deprecation language for pagination which will become a plugin by Rails 2.0 [DHH] * Added deprecation language for in_place_editor and auto_complete_field that both pieces will become plugins by Rails 2.0 [DHH] diff --git a/actionpack/lib/action_controller/integration.rb b/actionpack/lib/action_controller/integration.rb index 7676d15c2d3eb7041ce24709e9b3c2c93d665490..57872e8e8b27f8e8dba7a1adef978800b016cba2 100644 --- a/actionpack/lib/action_controller/integration.rb +++ b/actionpack/lib/action_controller/integration.rb @@ -151,13 +151,24 @@ def get(path, parameters=nil, headers=nil) process :get, path, parameters, headers end - # keep the docs for #get - %w( post put delete head ).each do |method| - class_eval <<-EOV, __FILE__, __LINE__ - def #{method}(path, parameters=nil, headers=nil) - process :#{method}, path, parameters, headers - end - EOV + # Performs a POST request with the given parameters. See get() for more details. + def post(path, parameters=nil, headers=nil) + process :post, path, parameters, headers + end + + # Performs a PUT request with the given parameters. See get() for more details. + def put(path, parameters=nil, headers=nil) + process :put, path, parameters, headers + end + + # Performs a DELETE request with the given parameters. See get() for more details. + def delete(path, parameters=nil, headers=nil) + process :delete, path, parameters, headers + end + + # Performs a HEAD request with the given parameters. See get() for more details. + def head(path, parameters=nil, headers=nil) + process :head, path, parameters, headers end # Performs an XMLHttpRequest request with the given parameters, mimicing diff --git a/actionpack/test/controller/base_test.rb b/actionpack/test/controller/base_test.rb index 9cf035d1cb395c634168332a4457b0b30a76b9e4..bcc473263f7ca3914acd7d7aa339fa1930d294ce 100644 --- a/actionpack/test/controller/base_test.rb +++ b/actionpack/test/controller/base_test.rb @@ -74,13 +74,24 @@ def setup def test_action_methods @empty_controllers.each do |c| + hide_mocha_methods_from_controller(c) assert_equal Set.new, c.send(:action_methods), "#{c.controller_path} should be empty!" end @non_empty_controllers.each do |c| + hide_mocha_methods_from_controller(c) assert_equal Set.new('public_action'), c.send(:action_methods), "#{c.controller_path} should not be empty!" end end + protected + + # Mocha adds methods to Object which are then included in the public_instance_methods + # This method hides those from the controller so the above tests won't know the difference + def hide_mocha_methods_from_controller(controller) + mocha_methods = [:expects, :metaclass, :mocha, :mocha_inspect, :reset_mocha, :stubba_object, :stubba_method, :stubs, :verify] + controller.class.send(:hide_action, *mocha_methods) + end + end diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb new file mode 100644 index 0000000000000000000000000000000000000000..665c5901e544be41a62a92bf1251e1c76837576b --- /dev/null +++ b/actionpack/test/controller/integration_test.rb @@ -0,0 +1,154 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +$:.unshift File.dirname(__FILE__) + '/../../../railties/lib' +require 'action_controller/integration' + +begin # rescue LoadError +require 'mocha' +require 'stubba' + +# Stub process for testing. +module ActionController + module Integration + class Session + def process + end + + def generic_url_rewriter + end + end + end +end + +class SessionTest < Test::Unit::TestCase + def setup + @session = ActionController::Integration::Session.new + end + def test_https_bang_works_and_sets_truth_by_default + assert !@session.https? + @session.https! + assert @session.https? + @session.https! false + assert !@session.https? + end + + def test_host! + assert_not_equal "glu.ttono.us", @session.host + @session.host! "rubyonrails.com" + assert_equal "rubyonrails.com", @session.host + end + + def test_follow_redirect_raises_when_no_redirect + @session.stubs(:redirect?).returns(false) + assert_raise(RuntimeError) { @session.follow_redirect! } + end + + def test_follow_redirect_calls_get_and_returns_status + @session.stubs(:redirect?).returns(true) + @session.stubs(:headers).returns({"location" => ["www.google.com"]}) + @session.stubs(:status).returns(200) + @session.expects(:get) + assert_equal 200, @session.follow_redirect! + end + + def test_get_via_redirect + path = "/somepath"; args = {:id => '1'} + + @session.expects(:get).with(path,args) + + redirects = [true, true, false] + @session.stubs(:redirect?).returns(lambda { redirects.shift }) + @session.expects(:follow_redirect!).times(2) + + @session.stubs(:status).returns(200) + assert_equal 200, @session.get_via_redirect(path, args) + end + + def test_post_via_redirect + path = "/somepath"; args = {:id => '1'} + + @session.expects(:post).with(path,args) + + redirects = [true, true, false] + @session.stubs(:redirect?).returns(lambda { redirects.shift }) + @session.expects(:follow_redirect!).times(2) + + @session.stubs(:status).returns(200) + assert_equal 200, @session.post_via_redirect(path, args) + end + + def test_url_for_with_controller + options = {:action => 'show'} + mock_controller = mock() + mock_controller.expects(:url_for).with(options).returns('/show') + @session.stubs(:controller).returns(mock_controller) + assert_equal '/show', @session.url_for(options) + end + + def test_url_for_without_controller + options = {:action => 'show'} + mock_rewriter = mock() + mock_rewriter.expects(:rewrite).with(options).returns('/show') + @session.stubs(:generic_url_rewriter).returns(mock_rewriter) + @session.stubs(:controller).returns(nil) + assert_equal '/show', @session.url_for(options) + end + + def test_redirect_bool_with_status_in_300s + @session.stubs(:status).returns 301 + assert @session.redirect? + end + + def test_redirect_bool_with_status_in_200s + @session.stubs(:status).returns 200 + assert !@session.redirect? + end + + def test_get + path = "/index"; params = "blah"; headers = {:location => 'blah'} + @session.expects(:process).with(:get,path,params,headers) + @session.get(path,params,headers) + end + + def test_post + path = "/index"; params = "blah"; headers = {:location => 'blah'} + @session.expects(:process).with(:post,path,params,headers) + @session.post(path,params,headers) + end + + def test_put + path = "/index"; params = "blah"; headers = {:location => 'blah'} + @session.expects(:process).with(:put,path,params,headers) + @session.put(path,params,headers) + end + + def test_delete + path = "/index"; params = "blah"; headers = {:location => 'blah'} + @session.expects(:process).with(:delete,path,params,headers) + @session.delete(path,params,headers) + end + + def test_head + path = "/index"; params = "blah"; headers = {:location => 'blah'} + @session.expects(:process).with(:head,path,params,headers) + @session.head(path,params,headers) + end + + def test_xml_http_request + path = "/index"; params = "blah"; headers = {:location => 'blah'} + headers_after_xhr = headers.merge( + "X-Requested-With" => "XMLHttpRequest", + "Accept" => "text/javascript, text/html, application/xml, text/xml, */*" + ) + @session.expects(:post).with(path,params,headers_after_xhr) + @session.xml_http_request(path,params,headers) + end +end + +# TODO +# class MockCGITest < Test::Unit::TestCase +# end + +rescue LoadError + $stderr.puts "Skipping integration tests. `gem install mocha` and try again." +end