From 6bf2ee09f74af37a50fd28af58b6f33a99ff891b Mon Sep 17 00:00:00 2001 From: Hartley McGuire Date: Wed, 16 Aug 2023 15:58:51 -0400 Subject: [PATCH] Add test coverage for ShowExceptions failsafe This adds additional test coverage to ShowExceptions, since one of the possible responses it creates was not previously tested. Because of the previous [addition][1] of Rack::Lint, this also demonstrates that the Content-Type header needed to be fixed. [1]: 339dda4a82356d173b62dab144870790618e40c6 --- .../action_dispatch/middleware/show_exceptions.rb | 2 +- actionpack/test/dispatch/show_exceptions_test.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb index b35b0b3cb66..07d9a98885e 100644 --- a/actionpack/lib/action_dispatch/middleware/show_exceptions.rb +++ b/actionpack/lib/action_dispatch/middleware/show_exceptions.rb @@ -54,7 +54,7 @@ module ActionDispatch rescue Exception => failsafe_error $stderr.puts "Error during failsafe response: #{failsafe_error}\n #{failsafe_error.backtrace * "\n "}" - [500, { "Content-Type" => "text/plain; charset=utf-8" }, + [500, { Rack::CONTENT_TYPE => "text/plain; charset=utf-8" }, ["500 Internal Server Error\n" \ "If you are the administrator of this website, then please read this web " \ "application's log file and/or the web server's log file to find out what " \ diff --git a/actionpack/test/dispatch/show_exceptions_test.rb b/actionpack/test/dispatch/show_exceptions_test.rb index 29bc8015e06..7115f6a93a8 100644 --- a/actionpack/test/dispatch/show_exceptions_test.rb +++ b/actionpack/test/dispatch/show_exceptions_test.rb @@ -135,6 +135,18 @@ class ShowExceptionsTest < ActionDispatch::IntegrationTest assert_equal("{\"status\":400,\"error\":\"Bad Request\"}", body) end + test "failsafe prevents raising if exceptions_app raises" do + old_stderr, $stderr = $stderr, StringIO.new + @app = build_app(->(_) { raise }) + + get "/" + + assert_response 500 + assert_match(/500 Internal Server Error/, body) + ensure + $stderr = old_stderr + end + private def build_app(exceptions_app = nil) exceptions_app ||= ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public")