2011-12-01 15:15:42 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
|
|
|
class DebugExceptionsTest < ActionDispatch::IntegrationTest
|
|
|
|
|
|
|
|
class Boomer
|
2011-12-14 11:03:35 -05:00
|
|
|
attr_accessor :closed
|
|
|
|
|
2011-12-01 15:15:42 -05:00
|
|
|
def initialize(detailed = false)
|
|
|
|
@detailed = detailed
|
2011-12-14 11:03:35 -05:00
|
|
|
@closed = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def each
|
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
@closed = true
|
2011-12-01 15:15:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
env['action_dispatch.show_detailed_exceptions'] = @detailed
|
|
|
|
req = ActionDispatch::Request.new(env)
|
|
|
|
case req.path
|
2011-12-03 05:38:25 -05:00
|
|
|
when "/pass"
|
2011-12-14 11:03:35 -05:00
|
|
|
[404, { "X-Cascade" => "pass" }, self]
|
2011-12-01 15:15:42 -05:00
|
|
|
when "/not_found"
|
2012-01-14 15:25:11 -05:00
|
|
|
raise AbstractController::ActionNotFound
|
2011-12-01 15:15:42 -05:00
|
|
|
when "/runtime_error"
|
|
|
|
raise RuntimeError
|
|
|
|
when "/method_not_allowed"
|
|
|
|
raise ActionController::MethodNotAllowed
|
2013-04-22 09:09:41 -04:00
|
|
|
when "/unknown_http_method"
|
|
|
|
raise ActionController::UnknownHttpMethod
|
2011-12-01 15:15:42 -05:00
|
|
|
when "/not_implemented"
|
|
|
|
raise ActionController::NotImplemented
|
|
|
|
when "/unprocessable_entity"
|
|
|
|
raise ActionController::InvalidAuthenticityToken
|
|
|
|
when "/not_found_original_exception"
|
2012-01-20 11:13:29 -05:00
|
|
|
raise ActionView::Template::Error.new('template', AbstractController::ActionNotFound.new)
|
2012-05-20 05:04:12 -04:00
|
|
|
when "/bad_request"
|
|
|
|
raise ActionController::BadRequest
|
2012-08-01 16:33:15 -04:00
|
|
|
when "/missing_keys"
|
|
|
|
raise ActionController::UrlGenerationError, "No route matches"
|
2013-01-15 19:07:13 -05:00
|
|
|
when "/parameter_missing"
|
|
|
|
raise ActionController::ParameterMissing, :missing_param_key
|
2014-01-30 09:33:49 -05:00
|
|
|
when "/original_syntax_error"
|
|
|
|
eval 'broke_syntax =' # `eval` need for raise native SyntaxError at runtime
|
|
|
|
when "/syntax_error_into_view"
|
|
|
|
begin
|
|
|
|
eval 'broke_syntax ='
|
|
|
|
rescue Exception => e
|
|
|
|
template = ActionView::Template.new(File.read(__FILE__),
|
|
|
|
__FILE__,
|
|
|
|
ActionView::Template::Handlers::Raw.new,
|
|
|
|
{})
|
|
|
|
raise ActionView::Template::Error.new(template, e)
|
|
|
|
end
|
|
|
|
|
2011-12-01 15:15:42 -05:00
|
|
|
else
|
|
|
|
raise "puke!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-05 07:59:00 -05:00
|
|
|
def setup
|
|
|
|
app = ActiveSupport::OrderedOptions.new
|
|
|
|
app.config = ActiveSupport::OrderedOptions.new
|
|
|
|
app.config.assets = ActiveSupport::OrderedOptions.new
|
|
|
|
app.config.assets.prefix = '/sprockets'
|
|
|
|
Rails.stubs(:application).returns(app)
|
|
|
|
end
|
|
|
|
|
|
|
|
RoutesApp = Struct.new(:routes).new(SharedTestRoutes)
|
|
|
|
ProductionApp = ActionDispatch::DebugExceptions.new(Boomer.new(false), RoutesApp)
|
|
|
|
DevelopmentApp = ActionDispatch::DebugExceptions.new(Boomer.new(true), RoutesApp)
|
2011-12-01 15:15:42 -05:00
|
|
|
|
|
|
|
test 'skip diagnosis if not showing detailed exceptions' do
|
|
|
|
@app = ProductionApp
|
|
|
|
assert_raise RuntimeError do
|
|
|
|
get "/", {}, {'action_dispatch.show_exceptions' => true}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'skip diagnosis if not showing exceptions' do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
assert_raise RuntimeError do
|
|
|
|
get "/", {}, {'action_dispatch.show_exceptions' => false}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-03 05:38:25 -05:00
|
|
|
test 'raise an exception on cascade pass' do
|
|
|
|
@app = ProductionApp
|
|
|
|
assert_raise ActionController::RoutingError do
|
|
|
|
get "/pass", {}, {'action_dispatch.show_exceptions' => true}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-14 11:03:35 -05:00
|
|
|
test 'closes the response body on cascade pass' do
|
|
|
|
boomer = Boomer.new(false)
|
|
|
|
@app = ActionDispatch::DebugExceptions.new(boomer)
|
|
|
|
assert_raise ActionController::RoutingError do
|
|
|
|
get "/pass", {}, {'action_dispatch.show_exceptions' => true}
|
|
|
|
end
|
|
|
|
assert boomer.closed, "Expected to close the response body"
|
|
|
|
end
|
|
|
|
|
2013-01-05 07:59:00 -05:00
|
|
|
test 'displays routes in a table when a RoutingError occurs' do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
get "/pass", {}, {'action_dispatch.show_exceptions' => true}
|
|
|
|
routing_table = body[/route_table.*<.table>/m]
|
|
|
|
assert_match '/:controller(/:action)(.:format)', routing_table
|
|
|
|
assert_match ':controller#:action', routing_table
|
|
|
|
assert_no_match '<|>', routing_table, "there should not be escaped html in the output"
|
|
|
|
end
|
|
|
|
|
2011-12-01 15:15:42 -05:00
|
|
|
test "rescue with diagnostics message" do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
|
|
|
|
get "/", {}, {'action_dispatch.show_exceptions' => true}
|
|
|
|
assert_response 500
|
|
|
|
assert_match(/puke/, body)
|
|
|
|
|
|
|
|
get "/not_found", {}, {'action_dispatch.show_exceptions' => true}
|
|
|
|
assert_response 404
|
2012-01-14 15:25:11 -05:00
|
|
|
assert_match(/#{AbstractController::ActionNotFound.name}/, body)
|
2011-12-01 15:15:42 -05:00
|
|
|
|
|
|
|
get "/method_not_allowed", {}, {'action_dispatch.show_exceptions' => true}
|
|
|
|
assert_response 405
|
|
|
|
assert_match(/ActionController::MethodNotAllowed/, body)
|
2012-05-20 05:04:12 -04:00
|
|
|
|
2013-04-22 09:09:41 -04:00
|
|
|
get "/unknown_http_method", {}, {'action_dispatch.show_exceptions' => true}
|
|
|
|
assert_response 405
|
|
|
|
assert_match(/ActionController::UnknownHttpMethod/, body)
|
|
|
|
|
2012-05-20 05:04:12 -04:00
|
|
|
get "/bad_request", {}, {'action_dispatch.show_exceptions' => true}
|
|
|
|
assert_response 400
|
|
|
|
assert_match(/ActionController::BadRequest/, body)
|
2013-01-15 19:07:13 -05:00
|
|
|
|
|
|
|
get "/parameter_missing", {}, {'action_dispatch.show_exceptions' => true}
|
|
|
|
assert_response 400
|
|
|
|
assert_match(/ActionController::ParameterMissing/, body)
|
2011-12-01 15:15:42 -05:00
|
|
|
end
|
|
|
|
|
2013-08-21 08:42:04 -04:00
|
|
|
test "rescue with text error for xhr request" do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
xhr_request_env = {'action_dispatch.show_exceptions' => true, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'}
|
|
|
|
|
|
|
|
get "/", {}, xhr_request_env
|
|
|
|
assert_response 500
|
2014-04-14 09:45:40 -04:00
|
|
|
assert_no_match(/<header>/, body)
|
2013-08-21 08:42:04 -04:00
|
|
|
assert_no_match(/<body>/, body)
|
|
|
|
assert_equal response.content_type, "text/plain"
|
2014-04-14 09:45:40 -04:00
|
|
|
assert_match(/RuntimeError\npuke/, body)
|
2013-08-21 08:42:04 -04:00
|
|
|
|
|
|
|
get "/not_found", {}, xhr_request_env
|
|
|
|
assert_response 404
|
|
|
|
assert_no_match(/<body>/, body)
|
|
|
|
assert_equal response.content_type, "text/plain"
|
|
|
|
assert_match(/#{AbstractController::ActionNotFound.name}/, body)
|
|
|
|
|
|
|
|
get "/method_not_allowed", {}, xhr_request_env
|
|
|
|
assert_response 405
|
|
|
|
assert_no_match(/<body>/, body)
|
|
|
|
assert_equal response.content_type, "text/plain"
|
|
|
|
assert_match(/ActionController::MethodNotAllowed/, body)
|
|
|
|
|
|
|
|
get "/unknown_http_method", {}, xhr_request_env
|
|
|
|
assert_response 405
|
|
|
|
assert_no_match(/<body>/, body)
|
|
|
|
assert_equal response.content_type, "text/plain"
|
|
|
|
assert_match(/ActionController::UnknownHttpMethod/, body)
|
|
|
|
|
|
|
|
get "/bad_request", {}, xhr_request_env
|
|
|
|
assert_response 400
|
|
|
|
assert_no_match(/<body>/, body)
|
|
|
|
assert_equal response.content_type, "text/plain"
|
|
|
|
assert_match(/ActionController::BadRequest/, body)
|
|
|
|
|
|
|
|
get "/parameter_missing", {}, xhr_request_env
|
|
|
|
assert_response 400
|
|
|
|
assert_no_match(/<body>/, body)
|
|
|
|
assert_equal response.content_type, "text/plain"
|
|
|
|
assert_match(/ActionController::ParameterMissing/, body)
|
|
|
|
end
|
|
|
|
|
2011-12-01 15:15:42 -05:00
|
|
|
test "does not show filtered parameters" do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
|
|
|
|
get "/", {"foo"=>"bar"}, {'action_dispatch.show_exceptions' => true,
|
|
|
|
'action_dispatch.parameter_filter' => [:foo]}
|
|
|
|
assert_response 500
|
|
|
|
assert_match(""foo"=>"[FILTERED]"", body)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "show registered original exception for wrapped exceptions" do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
|
|
|
|
get "/not_found_original_exception", {}, {'action_dispatch.show_exceptions' => true}
|
|
|
|
assert_response 404
|
|
|
|
assert_match(/AbstractController::ActionNotFound/, body)
|
|
|
|
end
|
|
|
|
|
2012-08-01 16:33:15 -04:00
|
|
|
test "named urls missing keys raise 500 level error" do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
|
|
|
|
get "/missing_keys", {}, {'action_dispatch.show_exceptions' => true}
|
|
|
|
assert_response 500
|
|
|
|
|
|
|
|
assert_match(/ActionController::UrlGenerationError/, body)
|
|
|
|
end
|
|
|
|
|
2011-12-01 15:15:42 -05:00
|
|
|
test "show the controller name in the diagnostics template when controller name is present" do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
get("/runtime_error", {}, {
|
|
|
|
'action_dispatch.show_exceptions' => true,
|
|
|
|
'action_dispatch.request.parameters' => {
|
|
|
|
'action' => 'show',
|
|
|
|
'id' => 'unknown',
|
|
|
|
'controller' => 'featured_tile'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert_response 500
|
2012-12-31 16:48:10 -05:00
|
|
|
assert_match(/RuntimeError\n\s+in FeaturedTileController/, body)
|
2011-12-01 15:15:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "sets the HTTP charset parameter" do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
|
|
|
|
get "/", {}, {'action_dispatch.show_exceptions' => true}
|
|
|
|
assert_equal "text/html; charset=utf-8", response.headers["Content-Type"]
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'uses logger from env' do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
output = StringIO.new
|
|
|
|
get "/", {}, {'action_dispatch.show_exceptions' => true, 'action_dispatch.logger' => Logger.new(output)}
|
|
|
|
assert_match(/puke/, output.rewind && output.read)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'uses backtrace cleaner from env' do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
cleaner = stub(:clean => ['passed backtrace cleaner'])
|
|
|
|
get "/", {}, {'action_dispatch.show_exceptions' => true, 'action_dispatch.backtrace_cleaner' => cleaner}
|
|
|
|
assert_match(/passed backtrace cleaner/, body)
|
|
|
|
end
|
2011-12-13 14:32:39 -05:00
|
|
|
|
|
|
|
test 'logs exception backtrace when all lines silenced' do
|
|
|
|
output = StringIO.new
|
|
|
|
backtrace_cleaner = ActiveSupport::BacktraceCleaner.new
|
|
|
|
backtrace_cleaner.add_silencer { true }
|
|
|
|
|
|
|
|
env = {'action_dispatch.show_exceptions' => true,
|
|
|
|
'action_dispatch.logger' => Logger.new(output),
|
|
|
|
'action_dispatch.backtrace_cleaner' => backtrace_cleaner}
|
|
|
|
|
|
|
|
get "/", {}, env
|
2011-12-16 00:00:20 -05:00
|
|
|
assert_operator((output.rewind && output.read).lines.count, :>, 10)
|
2011-12-13 14:32:39 -05:00
|
|
|
end
|
2014-01-30 09:33:49 -05:00
|
|
|
|
|
|
|
test 'display backtrace when error type is SyntaxError' do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
|
|
|
|
get '/original_syntax_error', {}, {'action_dispatch.backtrace_cleaner' => ActiveSupport::BacktraceCleaner.new}
|
|
|
|
|
|
|
|
assert_response 500
|
|
|
|
assert_select '#Application-Trace' do
|
|
|
|
assert_select 'pre code', /\(eval\):1: syntax error, unexpected/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'display backtrace when error type is SyntaxError wrapped by ActionView::Template::Error' do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
|
|
|
|
get '/syntax_error_into_view', {}, {'action_dispatch.backtrace_cleaner' => ActiveSupport::BacktraceCleaner.new}
|
|
|
|
|
|
|
|
assert_response 500
|
|
|
|
assert_select '#Application-Trace' do
|
|
|
|
assert_select 'pre code', /\(eval\):1: syntax error, unexpected/
|
|
|
|
end
|
|
|
|
end
|
2011-12-01 15:15:42 -05:00
|
|
|
end
|