From 7dd1c751f90858cbdfaebeafed5fdf1ef400ae8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 16 Dec 2011 09:45:14 +0100 Subject: Improve the specs on exceptions app. --- .../lib/action_dispatch/middleware/show_exceptions.rb | 7 ++++++- actionpack/test/dispatch/show_exceptions_test.rb | 15 ++++++++++++++- railties/test/application/middleware/exceptions_test.rb | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb index 4cfcb0a7db..5323048d90 100644 --- a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb +++ b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb @@ -4,7 +4,12 @@ require 'active_support/deprecation' module ActionDispatch # This middleware rescues any exception returned by the application - # and wraps them in a format for the end user. + # and calls a rack application that will wrap it in a format for the end user. + # + # The rack application should be passed as parameter on initialization + # of ShowExceptions. Everytime there is an exception, ShowExceptions will + # store the exception in env["action_dispatch.exception"], rewrite the + # PATH_INFO to the exception status code and call the rack app. class ShowExceptions FAILSAFE_RESPONSE = [500, {'Content-Type' => 'text/html'}, ["

500 Internal Server Error

" << diff --git a/actionpack/test/dispatch/show_exceptions_test.rb b/actionpack/test/dispatch/show_exceptions_test.rb index d3c99e4f31..0ebe281ada 100644 --- a/actionpack/test/dispatch/show_exceptions_test.rb +++ b/actionpack/test/dispatch/show_exceptions_test.rb @@ -20,7 +20,7 @@ class ShowExceptionsTest < ActionDispatch::IntegrationTest ProductionApp = ActionDispatch::ShowExceptions.new(Boomer.new, ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public")) - test 'skip diagnosis if not showing exceptions' do + test "skip exceptions app if not showing exceptions" do @app = ProductionApp assert_raise RuntimeError do get "/", {}, {'action_dispatch.show_exceptions' => false} @@ -75,4 +75,17 @@ class ShowExceptionsTest < ActionDispatch::IntegrationTest assert_response 404 assert_match(/404 error/, body) end + + test "calls custom exceptions app" do + exceptions_app = lambda do |env| + assert_kind_of AbstractController::ActionNotFound, env["action_dispatch.exception"] + assert_equal "/404", env["PATH_INFO"] + [404, { "Content-Type" => "text/plain" }, ["YOU FAILED BRO"]] + end + + @app = ActionDispatch::ShowExceptions.new(Boomer.new, exceptions_app) + get "/not_found_original_exception", {}, {'action_dispatch.show_exceptions' => true} + assert_response 404 + assert_equal "YOU FAILED BRO", body + end end diff --git a/railties/test/application/middleware/exceptions_test.rb b/railties/test/application/middleware/exceptions_test.rb index 912903adb7..6819e3e2e2 100644 --- a/railties/test/application/middleware/exceptions_test.rb +++ b/railties/test/application/middleware/exceptions_test.rb @@ -48,7 +48,7 @@ module ApplicationTests test "uses custom exceptions app" do add_to_config <<-RUBY config.exceptions_app = lambda do |env| - ["404", { "Content-Type" => "text/plain" }, ["YOU FAILED BRO"]] + [404, { "Content-Type" => "text/plain" }, ["YOU FAILED BRO"]] end RUBY -- cgit v1.2.3