Permalink
Browse files

Ensure rescue_from handlers are respected inside tests. [#835 state:r…

…esolved]

Note : If you're not using rescue_from, you should overrider rescue_action_without_handler() method
and not rescue_action(). Afterwards, you can set request.remote_addr to a non "0.0.0.0" value for testing the
overridden behavior.
  • Loading branch information...
1 parent 4918e6d commit 5e3517ea7b9fbd460f772bffc9212d882011f2bc @lifo lifo committed Oct 4, 2008
@@ -112,24 +112,7 @@ def rescue_from(*klasses, &block)
protected
# Exception handler called when the performance of an action raises an exception.
def rescue_action(exception)
- if handler_for_rescue(exception)
- rescue_action_with_handler(exception)
- else
- log_error(exception) if logger
- erase_results if performed?
-
- # Let the exception alter the response if it wants.
- # For example, MethodNotAllowed sets the Allow header.
- if exception.respond_to?(:handle_response!)
- exception.handle_response!(response)
- end
-
- if consider_all_requests_local || local_request?
- rescue_action_locally(exception)
- else
- rescue_action_in_public(exception)
- end
- end
+ rescue_action_with_handler(exception) || rescue_action_without_handler(exception)
end
# Overwrite to implement custom logging of errors. By default logs as fatal.
@@ -197,6 +180,23 @@ def rescue_action_with_handler(exception)
end
end
+ def rescue_action_without_handler(exception)
+ log_error(exception) if logger
+ erase_results if performed?
+
+ # Let the exception alter the response if it wants.
+ # For example, MethodNotAllowed sets the Allow header.
+ if exception.respond_to?(:handle_response!)
+ exception.handle_response!(response)
+ end
+
+ if consider_all_requests_local || local_request?
+ rescue_action_locally(exception)
+ else
+ rescue_action_in_public(exception)
+ end
+ end
+
private
def perform_action_with_rescue #:nodoc:
perform_action_without_rescue
@@ -84,7 +84,7 @@ class TestCase < ActiveSupport::TestCase
module RaiseActionExceptions
attr_accessor :exception
- def rescue_action(e)
+ def rescue_action_without_handler(e)
self.exception = e
if request.remote_addr == "0.0.0.0"
@@ -75,7 +75,7 @@ def raises
def method_not_allowed
raise ActionController::MethodNotAllowed.new(:get, :head, :put)
end
-
+
def not_implemented
raise ActionController::NotImplemented.new(:get, :put)
end
@@ -107,7 +107,7 @@ def record_invalid
def record_invalid_raise_as_string
raise RecordInvalidToRescueAsString
end
-
+
def bad_gateway
raise BadGateway
end
@@ -135,18 +135,19 @@ def show_errors(exception)
end
end
-class RescueTest < Test::Unit::TestCase
+class RescueControllerTest < ActionController::TestCase
FIXTURE_PUBLIC = "#{File.dirname(__FILE__)}/../fixtures".freeze
- def setup
- @controller = RescueController.new
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
+ setup :set_all_requests_local
+ setup :populate_exception_object
+ def set_all_requests_local
RescueController.consider_all_requests_local = true
@request.remote_addr = '1.2.3.4'
@request.host = 'example.com'
+ end
+ def populate_exception_object
begin
raise 'foo'
rescue => @exception
@@ -307,7 +308,7 @@ def test_clean_backtrace
assert_nil @controller.send(:clean_backtrace, Exception.new)
end
end
-
+
def test_not_implemented
with_all_requests_local false do
with_rails_public_path(".") do
@@ -463,14 +464,7 @@ def raise_grandchild_exception
end
end
-class ExceptionInheritanceRescueTest < Test::Unit::TestCase
-
- def setup
- @controller = ExceptionInheritanceRescueController.new
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
- end
-
+class ExceptionInheritanceRescueControllerTest < ActionController::TestCase
def test_bottom_first
get :raise_grandchild_exception
assert_response :no_content
@@ -500,14 +494,7 @@ def raise_second_exception_in_child_controller
end
end
-class ControllerInheritanceRescueControllerTest < Test::Unit::TestCase
-
- def setup
- @controller = ControllerInheritanceRescueController.new
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
- end
-
+class ControllerInheritanceRescueControllerTest < ActionController::TestCase
def test_first_exception_in_child_controller
get :raise_first_exception_in_child_controller
assert_response :gone

0 comments on commit 5e3517e

Please sign in to comment.