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
|
|
|
|
|
2014-07-28 11:22:11 -04:00
|
|
|
# We're obliged to implement this (even though it doesn't actually
|
|
|
|
# get called here) to properly comply with the Rack SPEC
|
2011-12-14 11:03:35 -05:00
|
|
|
def each
|
|
|
|
end
|
|
|
|
|
|
|
|
def close
|
|
|
|
@closed = true
|
2011-12-01 15:15:42 -05:00
|
|
|
end
|
|
|
|
|
2014-10-23 12:53:43 -04:00
|
|
|
def method_that_raises
|
|
|
|
raise StandardError.new 'error in framework'
|
|
|
|
end
|
|
|
|
|
2011-12-01 15:15:42 -05:00
|
|
|
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)
|
2014-11-24 11:54:03 -05:00
|
|
|
when "/missing_template"
|
|
|
|
raise ActionView::MissingTemplate.new(%w(foo), 'foo/index', %w(foo), false, 'mailer')
|
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
|
2014-10-23 12:53:43 -04:00
|
|
|
when "/framework_raises"
|
|
|
|
method_that_raises
|
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
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/", headers: { 'action_dispatch.show_exceptions' => true }
|
2011-12-01 15:15:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'skip diagnosis if not showing exceptions' do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
assert_raise RuntimeError do
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/", headers: { 'action_dispatch.show_exceptions' => false }
|
2011-12-01 15:15:42 -05:00
|
|
|
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
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/pass", headers: { 'action_dispatch.show_exceptions' => true }
|
2011-12-03 05:38:25 -05:00
|
|
|
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
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/pass", headers: { 'action_dispatch.show_exceptions' => true }
|
2011-12-14 11:03:35 -05:00
|
|
|
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
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/pass", headers: { 'action_dispatch.show_exceptions' => true }
|
2013-01-05 07:59:00 -05:00
|
|
|
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
|
|
|
|
|
2014-11-24 11:54:03 -05:00
|
|
|
test 'displays request and response info when a RoutingError occurs' do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/pass", headers: { 'action_dispatch.show_exceptions' => true }
|
2014-11-24 11:54:03 -05:00
|
|
|
|
|
|
|
assert_select 'h2', /Request/
|
|
|
|
assert_select 'h2', /Response/
|
|
|
|
end
|
|
|
|
|
2011-12-01 15:15:42 -05:00
|
|
|
test "rescue with diagnostics message" do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/", headers: { 'action_dispatch.show_exceptions' => true }
|
2011-12-01 15:15:42 -05:00
|
|
|
assert_response 500
|
|
|
|
assert_match(/puke/, body)
|
|
|
|
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/not_found", headers: { 'action_dispatch.show_exceptions' => true }
|
2011-12-01 15:15:42 -05:00
|
|
|
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
|
|
|
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/method_not_allowed", headers: { 'action_dispatch.show_exceptions' => true }
|
2011-12-01 15:15:42 -05:00
|
|
|
assert_response 405
|
|
|
|
assert_match(/ActionController::MethodNotAllowed/, body)
|
2012-05-20 05:04:12 -04:00
|
|
|
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/unknown_http_method", headers: { 'action_dispatch.show_exceptions' => true }
|
2013-04-22 09:09:41 -04:00
|
|
|
assert_response 405
|
|
|
|
assert_match(/ActionController::UnknownHttpMethod/, body)
|
|
|
|
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/bad_request", headers: { 'action_dispatch.show_exceptions' => true }
|
2012-05-20 05:04:12 -04:00
|
|
|
assert_response 400
|
|
|
|
assert_match(/ActionController::BadRequest/, body)
|
2013-01-15 19:07:13 -05:00
|
|
|
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/parameter_missing", headers: { 'action_dispatch.show_exceptions' => true }
|
2013-01-15 19:07:13 -05:00
|
|
|
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'}
|
|
|
|
|
2015-01-04 04:35:06 -05:00
|
|
|
get "/", headers: xhr_request_env
|
2013-08-21 08:42:04 -04:00
|
|
|
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)
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal "text/plain", response.content_type
|
2014-04-14 09:45:40 -04:00
|
|
|
assert_match(/RuntimeError\npuke/, body)
|
2013-08-21 08:42:04 -04:00
|
|
|
|
2015-01-04 04:35:06 -05:00
|
|
|
get "/not_found", headers: xhr_request_env
|
2013-08-21 08:42:04 -04:00
|
|
|
assert_response 404
|
|
|
|
assert_no_match(/<body>/, body)
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal "text/plain", response.content_type
|
2013-08-21 08:42:04 -04:00
|
|
|
assert_match(/#{AbstractController::ActionNotFound.name}/, body)
|
|
|
|
|
2015-01-04 04:35:06 -05:00
|
|
|
get "/method_not_allowed", headers: xhr_request_env
|
2013-08-21 08:42:04 -04:00
|
|
|
assert_response 405
|
|
|
|
assert_no_match(/<body>/, body)
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal "text/plain", response.content_type
|
2013-08-21 08:42:04 -04:00
|
|
|
assert_match(/ActionController::MethodNotAllowed/, body)
|
|
|
|
|
2015-01-04 04:35:06 -05:00
|
|
|
get "/unknown_http_method", headers: xhr_request_env
|
2013-08-21 08:42:04 -04:00
|
|
|
assert_response 405
|
|
|
|
assert_no_match(/<body>/, body)
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal "text/plain", response.content_type
|
2013-08-21 08:42:04 -04:00
|
|
|
assert_match(/ActionController::UnknownHttpMethod/, body)
|
|
|
|
|
2015-01-04 04:35:06 -05:00
|
|
|
get "/bad_request", headers: xhr_request_env
|
2013-08-21 08:42:04 -04:00
|
|
|
assert_response 400
|
|
|
|
assert_no_match(/<body>/, body)
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal "text/plain", response.content_type
|
2013-08-21 08:42:04 -04:00
|
|
|
assert_match(/ActionController::BadRequest/, body)
|
|
|
|
|
2015-01-04 04:35:06 -05:00
|
|
|
get "/parameter_missing", headers: xhr_request_env
|
2013-08-21 08:42:04 -04:00
|
|
|
assert_response 400
|
|
|
|
assert_no_match(/<body>/, body)
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal "text/plain", response.content_type
|
2013-08-21 08:42:04 -04:00
|
|
|
assert_match(/ActionController::ParameterMissing/, body)
|
|
|
|
end
|
|
|
|
|
2011-12-01 15:15:42 -05:00
|
|
|
test "does not show filtered parameters" do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/", params: { "foo"=>"bar" }, headers: { 'action_dispatch.show_exceptions' => true,
|
|
|
|
'action_dispatch.parameter_filter' => [:foo] }
|
2011-12-01 15:15:42 -05:00
|
|
|
assert_response 500
|
|
|
|
assert_match(""foo"=>"[FILTERED]"", body)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "show registered original exception for wrapped exceptions" do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/not_found_original_exception", headers: { 'action_dispatch.show_exceptions' => true }
|
2011-12-01 15:15:42 -05:00
|
|
|
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
|
|
|
|
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/missing_keys", headers: { 'action_dispatch.show_exceptions' => true }
|
2012-08-01 16:33:15 -04:00
|
|
|
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
|
2015-01-04 04:35:06 -05:00
|
|
|
get("/runtime_error", headers: {
|
2011-12-01 15:15:42 -05:00
|
|
|
'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
|
|
|
|
|
2014-12-01 17:45:33 -05:00
|
|
|
test "show formatted params" do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
|
|
|
|
params = {
|
|
|
|
'id' => 'unknown',
|
|
|
|
'someparam' => {
|
|
|
|
'foo' => 'bar',
|
|
|
|
'abc' => 'goo'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-04 04:35:06 -05:00
|
|
|
get("/runtime_error", headers: {
|
2014-12-01 17:45:33 -05:00
|
|
|
'action_dispatch.show_exceptions' => true,
|
|
|
|
'action_dispatch.request.parameters' => {
|
|
|
|
'action' => 'show',
|
|
|
|
'controller' => 'featured_tile'
|
|
|
|
}.merge(params)
|
|
|
|
})
|
|
|
|
assert_response 500
|
|
|
|
|
|
|
|
assert_includes(body, CGI.escapeHTML(PP.pp(params, "", 200)))
|
|
|
|
end
|
|
|
|
|
2011-12-01 15:15:42 -05:00
|
|
|
test "sets the HTTP charset parameter" do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/", headers: { 'action_dispatch.show_exceptions' => true }
|
2011-12-01 15:15:42 -05:00
|
|
|
assert_equal "text/html; charset=utf-8", response.headers["Content-Type"]
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'uses logger from env' do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
output = StringIO.new
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/", headers: { 'action_dispatch.show_exceptions' => true, 'action_dispatch.logger' => Logger.new(output) }
|
2011-12-01 15:15:42 -05:00
|
|
|
assert_match(/puke/, output.rewind && output.read)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'uses backtrace cleaner from env' do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
cleaner = stub(:clean => ['passed backtrace cleaner'])
|
2015-01-29 09:19:41 -05:00
|
|
|
get "/", headers: { 'action_dispatch.show_exceptions' => true, 'action_dispatch.backtrace_cleaner' => cleaner }
|
2011-12-01 15:15:42 -05:00
|
|
|
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}
|
|
|
|
|
2015-01-04 04:35:06 -05:00
|
|
|
get "/", headers: 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
|
|
|
|
|
2015-01-29 09:19:41 -05:00
|
|
|
get '/original_syntax_error', headers: { 'action_dispatch.backtrace_cleaner' => ActiveSupport::BacktraceCleaner.new }
|
2014-01-30 09:33:49 -05:00
|
|
|
|
|
|
|
assert_response 500
|
|
|
|
assert_select '#Application-Trace' do
|
2015-02-27 08:25:43 -05:00
|
|
|
assert_select 'pre code', /syntax error, unexpected/
|
2014-01-30 09:33:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-24 11:54:03 -05:00
|
|
|
test 'display backtrace on template missing errors' do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
|
2015-01-04 04:35:06 -05:00
|
|
|
get "/missing_template"
|
2014-11-24 11:54:03 -05:00
|
|
|
|
|
|
|
assert_select "header h1", /Template is missing/
|
|
|
|
|
|
|
|
assert_select "#container h2", /^Missing template/
|
|
|
|
|
|
|
|
assert_select '#Application-Trace'
|
|
|
|
assert_select '#Framework-Trace'
|
|
|
|
assert_select '#Full-Trace'
|
|
|
|
|
|
|
|
assert_select 'h2', /Request/
|
|
|
|
end
|
|
|
|
|
2014-01-30 09:33:49 -05:00
|
|
|
test 'display backtrace when error type is SyntaxError wrapped by ActionView::Template::Error' do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
|
2015-01-29 09:19:41 -05:00
|
|
|
get '/syntax_error_into_view', headers: { 'action_dispatch.backtrace_cleaner' => ActiveSupport::BacktraceCleaner.new }
|
2014-01-30 09:33:49 -05:00
|
|
|
|
|
|
|
assert_response 500
|
|
|
|
assert_select '#Application-Trace' do
|
2015-02-27 08:25:43 -05:00
|
|
|
assert_select 'pre code', /syntax error, unexpected/
|
2014-01-30 09:33:49 -05:00
|
|
|
end
|
|
|
|
end
|
2014-10-23 12:53:43 -04:00
|
|
|
|
|
|
|
test 'debug exceptions app shows user code that caused the error in source view' do
|
|
|
|
@app = DevelopmentApp
|
|
|
|
Rails.stubs(:root).returns(Pathname.new('.'))
|
|
|
|
cleaner = ActiveSupport::BacktraceCleaner.new.tap do |bc|
|
|
|
|
bc.add_silencer { |line| line =~ /method_that_raises/ }
|
|
|
|
bc.add_silencer { |line| line !~ %r{test/dispatch/debug_exceptions_test.rb} }
|
|
|
|
end
|
|
|
|
|
2015-01-29 09:19:41 -05:00
|
|
|
get '/framework_raises', headers: { 'action_dispatch.backtrace_cleaner' => cleaner }
|
2014-10-23 12:53:43 -04:00
|
|
|
|
|
|
|
# Assert correct error
|
|
|
|
assert_response 500
|
|
|
|
assert_select 'h2', /error in framework/
|
|
|
|
|
|
|
|
# assert source view line is the call to method_that_raises
|
|
|
|
assert_select 'div.source:not(.hidden)' do
|
|
|
|
assert_select 'pre .line.active', /method_that_raises/
|
|
|
|
end
|
|
|
|
|
|
|
|
# assert first source view (hidden) that throws the error
|
|
|
|
assert_select 'div.source:first' do
|
|
|
|
assert_select 'pre .line.active', /raise StandardError\.new/
|
|
|
|
end
|
|
|
|
|
|
|
|
# assert application trace refers to line that calls method_that_raises is first
|
|
|
|
assert_select '#Application-Trace' do
|
|
|
|
assert_select 'pre code a:first', %r{test/dispatch/debug_exceptions_test\.rb:\d+:in `call}
|
|
|
|
end
|
|
|
|
|
|
|
|
# assert framework trace that that threw the error is first
|
|
|
|
assert_select '#Framework-Trace' do
|
|
|
|
assert_select 'pre code a:first', /method_that_raises/
|
|
|
|
end
|
|
|
|
end
|
2011-12-01 15:15:42 -05:00
|
|
|
end
|