2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2009-05-03 00:02:22 -04:00
|
|
|
|
2010-09-24 20:15:52 -04:00
|
|
|
class ShowExceptionsTest < ActionDispatch::IntegrationTest
|
2011-11-21 12:13:54 -05:00
|
|
|
class Boomer
|
|
|
|
def call(env)
|
|
|
|
req = ActionDispatch::Request.new(env)
|
|
|
|
case req.path
|
|
|
|
when "/not_found"
|
2012-01-14 15:25:11 -05:00
|
|
|
raise AbstractController::ActionNotFound
|
2019-03-25 16:51:22 -04:00
|
|
|
when "/invalid_mimetype"
|
|
|
|
raise Mime::Type::InvalidMimeType
|
2015-07-06 13:12:00 -04:00
|
|
|
when "/bad_params", "/bad_params.json"
|
2015-11-03 09:54:34 -05:00
|
|
|
begin
|
|
|
|
raise StandardError.new
|
|
|
|
rescue
|
2016-06-19 21:09:41 -04:00
|
|
|
raise ActionDispatch::Http::Parameters::ParseError
|
2015-11-03 09:54:34 -05:00
|
|
|
end
|
2011-11-21 12:13:54 -05:00
|
|
|
when "/method_not_allowed"
|
2016-08-06 12:54:50 -04:00
|
|
|
raise ActionController::MethodNotAllowed, "PUT"
|
2013-04-22 09:09:41 -04:00
|
|
|
when "/unknown_http_method"
|
|
|
|
raise ActionController::UnknownHttpMethod
|
2011-11-21 12:13:54 -05:00
|
|
|
when "/not_found_original_exception"
|
2015-11-03 09:54:34 -05:00
|
|
|
begin
|
|
|
|
raise AbstractController::ActionNotFound.new
|
|
|
|
rescue
|
2016-08-06 12:54:50 -04:00
|
|
|
raise ActionView::Template::Error.new("template")
|
2015-11-03 09:54:34 -05:00
|
|
|
end
|
2011-11-21 12:13:54 -05:00
|
|
|
else
|
|
|
|
raise "puke!"
|
|
|
|
end
|
2009-05-03 00:02:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-22 16:00:38 -04:00
|
|
|
ProductionApp = ActionDispatch::ShowExceptions.new(Boomer.new, ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public"))
|
2009-05-03 00:02:22 -04:00
|
|
|
|
2011-12-16 03:45:14 -05:00
|
|
|
test "skip exceptions app if not showing exceptions" do
|
2011-12-01 15:15:42 -05:00
|
|
|
@app = ProductionApp
|
|
|
|
assert_raise RuntimeError do
|
2018-11-26 15:21:11 -05:00
|
|
|
get "/", env: { "action_dispatch.show_exceptions" => false }
|
2011-12-01 15:15:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "rescue with error page" do
|
2009-09-26 21:51:05 -04:00
|
|
|
@app = ProductionApp
|
2009-05-03 00:02:22 -04:00
|
|
|
|
2018-11-26 15:21:11 -05:00
|
|
|
get "/", env: { "action_dispatch.show_exceptions" => true }
|
2009-05-03 00:02:22 -04:00
|
|
|
assert_response 500
|
|
|
|
assert_equal "500 error fixture\n", body
|
2014-07-13 12:58:20 -04:00
|
|
|
|
2018-11-26 15:21:11 -05:00
|
|
|
get "/bad_params", env: { "action_dispatch.show_exceptions" => true }
|
2013-03-21 14:23:46 -04:00
|
|
|
assert_response 400
|
|
|
|
assert_equal "400 error fixture\n", body
|
2009-05-03 00:02:22 -04:00
|
|
|
|
2018-11-26 15:21:11 -05:00
|
|
|
get "/not_found", env: { "action_dispatch.show_exceptions" => true }
|
2009-05-03 00:02:22 -04:00
|
|
|
assert_response 404
|
|
|
|
assert_equal "404 error fixture\n", body
|
|
|
|
|
2018-11-26 15:21:11 -05:00
|
|
|
get "/method_not_allowed", env: { "action_dispatch.show_exceptions" => true }
|
2009-05-03 00:02:22 -04:00
|
|
|
assert_response 405
|
|
|
|
assert_equal "", body
|
2013-04-22 09:09:41 -04:00
|
|
|
|
2018-11-26 15:21:11 -05:00
|
|
|
get "/unknown_http_method", env: { "action_dispatch.show_exceptions" => true }
|
2013-04-22 09:09:41 -04:00
|
|
|
assert_response 405
|
|
|
|
assert_equal "", body
|
2019-03-25 16:51:22 -04:00
|
|
|
|
|
|
|
get "/invalid_mimetype", headers: { "Accept" => "text/html,*", "action_dispatch.show_exceptions" => true }
|
|
|
|
assert_response 406
|
|
|
|
assert_equal "", body
|
2009-05-03 00:02:22 -04:00
|
|
|
end
|
|
|
|
|
2011-11-22 03:24:05 -05:00
|
|
|
test "localize rescue error page" do
|
2009-09-19 14:22:09 -04:00
|
|
|
old_locale, I18n.locale = I18n.locale, :da
|
2009-05-03 00:02:22 -04:00
|
|
|
|
|
|
|
begin
|
2009-09-26 21:51:05 -04:00
|
|
|
@app = ProductionApp
|
2009-05-03 00:02:22 -04:00
|
|
|
|
2018-11-26 15:21:11 -05:00
|
|
|
get "/", env: { "action_dispatch.show_exceptions" => true }
|
2009-05-03 00:02:22 -04:00
|
|
|
assert_response 500
|
|
|
|
assert_equal "500 localized error fixture\n", body
|
|
|
|
|
2018-11-26 15:21:11 -05:00
|
|
|
get "/not_found", env: { "action_dispatch.show_exceptions" => true }
|
2009-05-03 00:02:22 -04:00
|
|
|
assert_response 404
|
|
|
|
assert_equal "404 error fixture\n", body
|
|
|
|
ensure
|
|
|
|
I18n.locale = old_locale
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-03 09:30:59 -04:00
|
|
|
test "sets the HTTP charset parameter" do
|
2011-12-01 15:15:42 -05:00
|
|
|
@app = ProductionApp
|
2011-05-03 09:30:59 -04:00
|
|
|
|
2018-11-26 15:21:11 -05:00
|
|
|
get "/", env: { "action_dispatch.show_exceptions" => true }
|
2011-05-03 09:30:59 -04:00
|
|
|
assert_equal "text/html; charset=utf-8", response.headers["Content-Type"]
|
|
|
|
end
|
2011-11-24 14:37:48 -05:00
|
|
|
|
2011-12-01 15:15:42 -05:00
|
|
|
test "show registered original exception for wrapped exceptions" do
|
2011-11-24 14:37:48 -05:00
|
|
|
@app = ProductionApp
|
2011-11-28 11:25:37 -05:00
|
|
|
|
2018-11-26 15:21:11 -05:00
|
|
|
get "/not_found_original_exception", env: { "action_dispatch.show_exceptions" => true }
|
2011-12-01 15:15:42 -05:00
|
|
|
assert_response 404
|
|
|
|
assert_match(/404 error/, body)
|
2011-11-28 11:25:37 -05:00
|
|
|
end
|
2011-12-16 03:45:14 -05:00
|
|
|
|
|
|
|
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"]
|
2014-07-13 12:58:20 -04:00
|
|
|
assert_equal "/not_found_original_exception", env["action_dispatch.original_path"]
|
2015-10-25 04:57:50 -04:00
|
|
|
[404, { "Content-Type" => "text/plain" }, ["YOU FAILED"]]
|
2011-12-16 03:45:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@app = ActionDispatch::ShowExceptions.new(Boomer.new, exceptions_app)
|
2018-11-26 15:21:11 -05:00
|
|
|
get "/not_found_original_exception", env: { "action_dispatch.show_exceptions" => true }
|
2011-12-16 03:45:14 -05:00
|
|
|
assert_response 404
|
2015-10-25 04:57:50 -04:00
|
|
|
assert_equal "YOU FAILED", body
|
2011-12-16 03:45:14 -05:00
|
|
|
end
|
2011-12-16 03:59:36 -05:00
|
|
|
|
|
|
|
test "returns an empty response if custom exceptions app returns X-Cascade pass" do
|
|
|
|
exceptions_app = lambda do |env|
|
|
|
|
[404, { "X-Cascade" => "pass" }, []]
|
|
|
|
end
|
|
|
|
|
|
|
|
@app = ActionDispatch::ShowExceptions.new(Boomer.new, exceptions_app)
|
2018-11-26 15:21:11 -05:00
|
|
|
get "/method_not_allowed", env: { "action_dispatch.show_exceptions" => true }
|
2011-12-16 03:59:36 -05:00
|
|
|
assert_response 405
|
|
|
|
assert_equal "", body
|
|
|
|
end
|
2015-07-06 13:12:00 -04:00
|
|
|
|
|
|
|
test "bad params exception is returned in the correct format" do
|
|
|
|
@app = ProductionApp
|
|
|
|
|
2018-11-26 15:21:11 -05:00
|
|
|
get "/bad_params", env: { "action_dispatch.show_exceptions" => true }
|
2015-07-06 13:12:00 -04:00
|
|
|
assert_equal "text/html; charset=utf-8", response.headers["Content-Type"]
|
|
|
|
assert_response 400
|
|
|
|
assert_match(/400 error/, body)
|
|
|
|
|
2018-11-26 15:21:11 -05:00
|
|
|
get "/bad_params.json", env: { "action_dispatch.show_exceptions" => true }
|
2015-07-06 13:12:00 -04:00
|
|
|
assert_equal "application/json; charset=utf-8", response.headers["Content-Type"]
|
|
|
|
assert_response 400
|
|
|
|
assert_equal("{\"status\":400,\"error\":\"Bad Request\"}", body)
|
|
|
|
end
|
2009-05-03 00:02:22 -04:00
|
|
|
end
|