mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
refactor show exceptions tests
This commit is contained in:
59
actionpack/test/controller/show_exceptions_test.rb
Normal file
59
actionpack/test/controller/show_exceptions_test.rb
Normal file
@@ -0,0 +1,59 @@
|
||||
require 'abstract_unit'
|
||||
|
||||
module ShowExceptions
|
||||
class ShowExceptionsController < ActionController::Metal
|
||||
use ActionDispatch::ShowExceptions
|
||||
|
||||
def boom
|
||||
raise 'boom!'
|
||||
end
|
||||
end
|
||||
|
||||
class ShowExceptionsTest < ActionDispatch::IntegrationTest
|
||||
test 'show error page from a remote ip' do
|
||||
@app = ShowExceptionsController.action(:boom)
|
||||
self.remote_addr = '208.77.188.166'
|
||||
get '/'
|
||||
assert_equal "500 error fixture\n", body
|
||||
end
|
||||
|
||||
test 'show diagnostics from a local ip' do
|
||||
@app = ShowExceptionsController.action(:boom)
|
||||
['127.0.0.1', '127.0.0.127', '::1', '0:0:0:0:0:0:0:1', '0:0:0:0:0:0:0:1%0'].each do |ip_address|
|
||||
self.remote_addr = ip_address
|
||||
get '/'
|
||||
assert_match /boom/, body
|
||||
end
|
||||
end
|
||||
|
||||
test 'show diagnostics from a remote ip when consider_all_requests_local is true' do
|
||||
Rails.stubs(:application).returns stub(:config => stub(:consider_all_requests_local => true))
|
||||
@app = ShowExceptionsController.action(:boom)
|
||||
self.remote_addr = '208.77.188.166'
|
||||
get '/'
|
||||
assert_match /boom/, body
|
||||
end
|
||||
end
|
||||
|
||||
class ShowExceptionsOverridenController < ShowExceptionsController
|
||||
private
|
||||
|
||||
def show_detailed_exceptions?
|
||||
params['detailed'] == '1'
|
||||
end
|
||||
end
|
||||
|
||||
class ShowExceptionsOverridenTest < ActionDispatch::IntegrationTest
|
||||
test 'show error page' do
|
||||
@app = ShowExceptionsOverridenController.action(:boom)
|
||||
get '/', {'detailed' => '0'}
|
||||
assert_equal "500 error fixture\n", body
|
||||
end
|
||||
|
||||
test 'show diagnostics message' do
|
||||
@app = ShowExceptionsOverridenController.action(:boom)
|
||||
get '/', {'detailed' => '1'}
|
||||
assert_match /boom/, body
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,12 +3,12 @@ require 'abstract_unit'
|
||||
class ShowExceptionsTest < ActionDispatch::IntegrationTest
|
||||
|
||||
class Boomer
|
||||
def initialize(show_exceptions = false)
|
||||
@show_exceptions = show_exceptions
|
||||
def initialize(detailed = false)
|
||||
@detailed = detailed
|
||||
end
|
||||
|
||||
def call(env)
|
||||
env['action_dispatch.show_exceptions'] = @show_exceptions
|
||||
env['action_dispatch.show_detailed_exceptions'] = @detailed
|
||||
req = ActionDispatch::Request.new(env)
|
||||
case req.path
|
||||
when "/not_found"
|
||||
@@ -32,9 +32,8 @@ class ShowExceptionsTest < ActionDispatch::IntegrationTest
|
||||
ProductionApp = ActionDispatch::ShowExceptions.new(Boomer.new(false))
|
||||
DevelopmentApp = ActionDispatch::ShowExceptions.new(Boomer.new(true))
|
||||
|
||||
test "rescue in public from a remote ip" do
|
||||
test "rescue with error page when show_exceptions is false" do
|
||||
@app = ProductionApp
|
||||
self.remote_addr = '208.77.188.166'
|
||||
|
||||
get "/", {}, {'action_dispatch.show_exceptions' => true}
|
||||
assert_response 500
|
||||
@@ -49,48 +48,8 @@ class ShowExceptionsTest < ActionDispatch::IntegrationTest
|
||||
assert_equal "", body
|
||||
end
|
||||
|
||||
test "rescue locally from a local request" do
|
||||
@app = ProductionApp
|
||||
['127.0.0.1', '127.0.0.127', '::1', '0:0:0:0:0:0:0:1', '0:0:0:0:0:0:0:1%0'].each do |ip_address|
|
||||
self.remote_addr = ip_address
|
||||
|
||||
get "/", {}, {'action_dispatch.show_exceptions' => true}
|
||||
assert_response 500
|
||||
assert_match(/puke/, body)
|
||||
|
||||
get "/not_found", {}, {'action_dispatch.show_exceptions' => true}
|
||||
assert_response 404
|
||||
assert_match(/#{ActionController::UnknownAction.name}/, body)
|
||||
|
||||
get "/method_not_allowed", {}, {'action_dispatch.show_exceptions' => true}
|
||||
assert_response 405
|
||||
assert_match(/ActionController::MethodNotAllowed/, body)
|
||||
end
|
||||
end
|
||||
|
||||
test "localize public rescue message" do
|
||||
# Change locale
|
||||
old_locale, I18n.locale = I18n.locale, :da
|
||||
|
||||
begin
|
||||
@app = ProductionApp
|
||||
self.remote_addr = '208.77.188.166'
|
||||
|
||||
get "/", {}, {'action_dispatch.show_exceptions' => true}
|
||||
assert_response 500
|
||||
assert_equal "500 localized error fixture\n", body
|
||||
|
||||
get "/not_found", {}, {'action_dispatch.show_exceptions' => true}
|
||||
assert_response 404
|
||||
assert_equal "404 error fixture\n", body
|
||||
ensure
|
||||
I18n.locale = old_locale
|
||||
end
|
||||
end
|
||||
|
||||
test "always rescue locally in development mode" do
|
||||
test "rescue with diagnostics message when show_exceptions is true" do
|
||||
@app = DevelopmentApp
|
||||
self.remote_addr = '208.77.188.166'
|
||||
|
||||
get "/", {}, {'action_dispatch.show_exceptions' => true}
|
||||
assert_response 500
|
||||
@@ -105,6 +64,25 @@ class ShowExceptionsTest < ActionDispatch::IntegrationTest
|
||||
assert_match(/ActionController::MethodNotAllowed/, body)
|
||||
end
|
||||
|
||||
test "localize rescue error page" do
|
||||
# Change locale
|
||||
old_locale, I18n.locale = I18n.locale, :da
|
||||
|
||||
begin
|
||||
@app = ProductionApp
|
||||
|
||||
get "/", {}, {'action_dispatch.show_exceptions' => true}
|
||||
assert_response 500
|
||||
assert_equal "500 localized error fixture\n", body
|
||||
|
||||
get "/not_found", {}, {'action_dispatch.show_exceptions' => true}
|
||||
assert_response 404
|
||||
assert_equal "404 error fixture\n", body
|
||||
ensure
|
||||
I18n.locale = old_locale
|
||||
end
|
||||
end
|
||||
|
||||
test "does not show filtered parameters" do
|
||||
@app = DevelopmentApp
|
||||
|
||||
@@ -114,16 +92,15 @@ class ShowExceptionsTest < ActionDispatch::IntegrationTest
|
||||
assert_match(""foo"=>"[FILTERED]"", body)
|
||||
end
|
||||
|
||||
test "show registered original exception for wrapped exceptions when consider_all_requests_local is false" do
|
||||
test "show registered original exception for wrapped exceptions when show_exceptions is false" do
|
||||
@app = ProductionApp
|
||||
self.remote_addr = '208.77.188.166'
|
||||
|
||||
get "/not_found_original_exception", {}, {'action_dispatch.show_exceptions' => true}
|
||||
assert_response 404
|
||||
assert_match(/404 error/, body)
|
||||
end
|
||||
|
||||
test "show registered original exception for wrapped exceptions when consider_all_requests_local is true" do
|
||||
test "show registered original exception for wrapped exceptions when show_exceptions is true" do
|
||||
@app = DevelopmentApp
|
||||
|
||||
get "/not_found_original_exception", {}, {'action_dispatch.show_exceptions' => true}
|
||||
@@ -132,7 +109,7 @@ class ShowExceptionsTest < ActionDispatch::IntegrationTest
|
||||
end
|
||||
|
||||
test "show the controller name in the diagnostics template when controller name is present" do
|
||||
@app = ProductionApp
|
||||
@app = DevelopmentApp
|
||||
get("/runtime_error", {}, {
|
||||
'action_dispatch.show_exceptions' => true,
|
||||
'action_dispatch.request.parameters' => {
|
||||
|
||||
Reference in New Issue
Block a user