2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2011-11-22 03:24:05 -05:00
|
|
|
|
|
|
|
module ShowExceptions
|
2011-11-22 05:34:13 -05:00
|
|
|
class ShowExceptionsController < ActionController::Base
|
2011-12-16 03:29:37 -05:00
|
|
|
use ActionDispatch::ShowExceptions, ActionDispatch::PublicExceptions.new("#{FIXTURE_LOAD_PATH}/public")
|
2011-12-01 14:46:18 -05:00
|
|
|
use ActionDispatch::DebugExceptions
|
2011-11-22 03:24:05 -05:00
|
|
|
|
2012-12-07 14:46:06 -05:00
|
|
|
before_action only: :another_boom do
|
2011-12-15 13:43:49 -05:00
|
|
|
request.env["action_dispatch.show_detailed_exceptions"] = true
|
|
|
|
end
|
|
|
|
|
2011-11-22 03:24:05 -05:00
|
|
|
def boom
|
2016-08-06 12:54:50 -04:00
|
|
|
raise "boom!"
|
2011-11-22 03:24:05 -05:00
|
|
|
end
|
2011-12-15 13:43:49 -05:00
|
|
|
|
|
|
|
def another_boom
|
2016-08-06 12:54:50 -04:00
|
|
|
raise "boom!"
|
2011-12-15 13:43:49 -05:00
|
|
|
end
|
2011-12-16 04:38:17 -05:00
|
|
|
|
|
|
|
def show_detailed_exceptions?
|
|
|
|
request.local?
|
|
|
|
end
|
2011-11-22 03:24:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class ShowExceptionsTest < ActionDispatch::IntegrationTest
|
2016-08-06 12:54:50 -04:00
|
|
|
test "show error page from a remote ip" do
|
2011-11-22 03:24:05 -05:00
|
|
|
@app = ShowExceptionsController.action(:boom)
|
2016-08-06 12:54:50 -04:00
|
|
|
self.remote_addr = "208.77.188.166"
|
|
|
|
get "/"
|
2011-11-22 03:24:05 -05:00
|
|
|
assert_equal "500 error fixture\n", body
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "show diagnostics from a local ip if show_detailed_exceptions? is set to request.local?" do
|
2011-11-22 03:24:05 -05:00
|
|
|
@app = ShowExceptionsController.action(:boom)
|
2016-08-06 12:54:50 -04:00
|
|
|
["127.0.0.1", "127.0.0.127", "127.12.1.1", "::1", "0:0:0:0:0:0:0:1", "0:0:0:0:0:0:0:1%0"].each do |ip_address|
|
2011-11-22 03:24:05 -05:00
|
|
|
self.remote_addr = ip_address
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/"
|
2011-11-23 10:10:34 -05:00
|
|
|
assert_match(/boom/, body)
|
2011-11-22 03:24:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "show diagnostics from a remote ip when env is already set" do
|
2011-12-15 13:43:49 -05:00
|
|
|
@app = ShowExceptionsController.action(:another_boom)
|
2016-08-06 12:54:50 -04:00
|
|
|
self.remote_addr = "208.77.188.166"
|
|
|
|
get "/"
|
2011-11-23 10:10:34 -05:00
|
|
|
assert_match(/boom/, body)
|
2011-11-22 03:24:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-30 14:17:08 -04:00
|
|
|
class ShowExceptionsOverriddenController < ShowExceptionsController
|
2011-11-22 03:24:05 -05:00
|
|
|
private
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def show_detailed_exceptions?
|
|
|
|
params["detailed"] == "1"
|
|
|
|
end
|
2011-11-22 03:24:05 -05:00
|
|
|
end
|
|
|
|
|
2013-03-30 14:17:08 -04:00
|
|
|
class ShowExceptionsOverriddenTest < ActionDispatch::IntegrationTest
|
2016-08-06 12:54:50 -04:00
|
|
|
test "show error page" do
|
2013-03-30 14:17:08 -04:00
|
|
|
@app = ShowExceptionsOverriddenController.action(:boom)
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/", params: { "detailed" => "0" }
|
2011-11-22 03:24:05 -05:00
|
|
|
assert_equal "500 error fixture\n", body
|
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
test "show diagnostics message" do
|
2013-03-30 14:17:08 -04:00
|
|
|
@app = ShowExceptionsOverriddenController.action(:boom)
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/", params: { "detailed" => "1" }
|
2011-11-23 10:10:34 -05:00
|
|
|
assert_match(/boom/, body)
|
2011-11-22 03:24:05 -05:00
|
|
|
end
|
|
|
|
end
|
2012-06-11 15:58:24 -04:00
|
|
|
|
|
|
|
class ShowExceptionsFormatsTest < ActionDispatch::IntegrationTest
|
|
|
|
def test_render_json_exception
|
2013-03-30 14:17:08 -04:00
|
|
|
@app = ShowExceptionsOverriddenController.action(:boom)
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/", headers: { "HTTP_ACCEPT" => "application/json" }
|
2012-06-11 15:58:24 -04:00
|
|
|
assert_response :internal_server_error
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "application/json", response.content_type.to_s
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_equal({ status: 500, error: "Internal Server Error" }.to_json, response.body)
|
2012-06-11 15:58:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_xml_exception
|
2013-03-30 14:17:08 -04:00
|
|
|
@app = ShowExceptionsOverriddenController.action(:boom)
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/", headers: { "HTTP_ACCEPT" => "application/xml" }
|
2012-06-11 15:58:24 -04:00
|
|
|
assert_response :internal_server_error
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "application/xml", response.content_type.to_s
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_equal({ status: 500, error: "Internal Server Error" }.to_xml, response.body)
|
2012-06-11 15:58:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_fallback_exception
|
2013-03-30 14:17:08 -04:00
|
|
|
@app = ShowExceptionsOverriddenController.action(:boom)
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/", headers: { "HTTP_ACCEPT" => "text/csv" }
|
2012-06-11 15:58:24 -04:00
|
|
|
assert_response :internal_server_error
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "text/html", response.content_type.to_s
|
2012-06-11 15:58:24 -04:00
|
|
|
end
|
|
|
|
end
|
2012-07-06 01:39:15 -04:00
|
|
|
|
|
|
|
class ShowFailsafeExceptionsTest < ActionDispatch::IntegrationTest
|
|
|
|
def test_render_failsafe_exception
|
2013-03-30 14:17:08 -04:00
|
|
|
@app = ShowExceptionsOverriddenController.action(:boom)
|
2012-07-06 01:39:15 -04:00
|
|
|
@exceptions_app = @app.instance_variable_get(:@exceptions_app)
|
|
|
|
@app.instance_variable_set(:@exceptions_app, nil)
|
|
|
|
$stderr = StringIO.new
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/", headers: { "HTTP_ACCEPT" => "text/json" }
|
2012-07-06 01:39:15 -04:00
|
|
|
assert_response :internal_server_error
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "text/plain", response.content_type.to_s
|
2012-11-04 16:50:56 -05:00
|
|
|
ensure
|
2012-07-06 01:39:15 -04:00
|
|
|
@app.instance_variable_set(:@exceptions_app, @exceptions_app)
|
|
|
|
$stderr = STDERR
|
|
|
|
end
|
|
|
|
end
|
2011-11-22 03:24:05 -05:00
|
|
|
end
|