From a6f49d9b786bfcc049a99f843de8e4d838de1179 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 23 Sep 2007 21:56:22 +0000 Subject: Introduce ActionController::Base.rescue_from to declare exception-handling methods. Cleaner style than the case-heavy rescue_action_in_public. Closes #9449. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7597 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/test/controller/rescue_test.rb | 48 ++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 10 deletions(-) (limited to 'actionpack/test/controller') diff --git a/actionpack/test/controller/rescue_test.rb b/actionpack/test/controller/rescue_test.rb index 6756e226c3..f0d0526a36 100644 --- a/actionpack/test/controller/rescue_test.rb +++ b/actionpack/test/controller/rescue_test.rb @@ -3,6 +3,15 @@ require File.dirname(__FILE__) + '/../abstract_unit' uses_mocha 'rescue' do class RescueController < ActionController::Base + class NotAuthorized < StandardError + end + + class RecordInvalid < StandardError + end + + rescue_from NotAuthorized, :with => :deny_access + rescue_from RecordInvalid, :with => :show_errors + def raises render :text => 'already rendered' raise "don't panic!" @@ -15,10 +24,27 @@ class RescueController < ActionController::Base def not_implemented raise ActionController::NotImplemented.new(:get, :put) end + + def not_authorized + raise NotAuthorized + end + + def record_invalid + raise RecordInvalid + end - def missing_template; end -end + def missing_template + end + + protected + def deny_access + head :forbidden + end + def show_errors(exception) + head :unprocessable_entity + end +end class RescueTest < Test::Unit::TestCase FIXTURE_PUBLIC = "#{File.dirname(__FILE__)}/../fixtures".freeze @@ -38,7 +64,6 @@ class RescueTest < Test::Unit::TestCase end end - def test_rescue_action_locally_if_all_requests_local @controller.expects(:local_request?).never @controller.expects(:rescue_action_locally).with(@exception) @@ -69,7 +94,6 @@ class RescueTest < Test::Unit::TestCase end end - def test_rescue_action_in_public_with_error_file with_rails_root FIXTURE_PUBLIC do with_all_requests_local false do @@ -93,7 +117,6 @@ class RescueTest < Test::Unit::TestCase assert_equal ' ', @response.body end - def test_rescue_unknown_action_in_public_with_error_file with_rails_root FIXTURE_PUBLIC do with_all_requests_local false do @@ -117,7 +140,6 @@ class RescueTest < Test::Unit::TestCase assert_equal ' ', @response.body end - def test_rescue_missing_template_in_public with_rails_root FIXTURE_PUBLIC do with_all_requests_local true do @@ -129,7 +151,6 @@ class RescueTest < Test::Unit::TestCase assert @response.body.include?('missing_template'), "Response should include the template name." end - def test_rescue_action_locally get :raises assert_response :internal_server_error @@ -138,7 +159,6 @@ class RescueTest < Test::Unit::TestCase assert @response.body.include?("don't panic"), "Response should include exception message." end - def test_local_request_when_remote_addr_is_localhost @controller.expects(:request).returns(@request).at_least_once with_remote_addr '127.0.0.1' do @@ -153,7 +173,6 @@ class RescueTest < Test::Unit::TestCase end end - def test_rescue_responses responses = ActionController::Base.rescue_responses @@ -182,7 +201,6 @@ class RescueTest < Test::Unit::TestCase assert_equal 'template_error', templates[ActionView::TemplateError.name] end - def test_clean_backtrace with_rails_root nil do # No action if RAILS_ROOT isn't set. @@ -217,6 +235,16 @@ class RescueTest < Test::Unit::TestCase assert_equal "GET, HEAD, PUT", @response.headers['Allow'] end + def test_rescue_handler + get :not_authorized + assert_response :forbidden + end + + def test_rescue_handler_with_argument + @controller.expects(:show_errors).once.with { |e| e.is_a?(Exception) } + get :record_invalid + end + protected def with_all_requests_local(local = true) old_local, ActionController::Base.consider_all_requests_local = -- cgit v1.2.3