Skip to content

Commit c6d6b28

Browse files
committed
refactor show exceptions tests
1 parent a9e8cf7 commit c6d6b28

File tree

2 files changed

+78
-42
lines changed

2 files changed

+78
-42
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require 'abstract_unit'
2+
3+
module ShowExceptions
4+
class ShowExceptionsController < ActionController::Metal
5+
use ActionDispatch::ShowExceptions
6+
7+
def boom
8+
raise 'boom!'
9+
end
10+
end
11+
12+
class ShowExceptionsTest < ActionDispatch::IntegrationTest
13+
test 'show error page from a remote ip' do
14+
@app = ShowExceptionsController.action(:boom)
15+
self.remote_addr = '208.77.188.166'
16+
get '/'
17+
assert_equal "500 error fixture\n", body
18+
end
19+
20+
test 'show diagnostics from a local ip' do
21+
@app = ShowExceptionsController.action(:boom)
22+
['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|
23+
self.remote_addr = ip_address
24+
get '/'
25+
assert_match /boom/, body
26+
end
27+
end
28+
29+
test 'show diagnostics from a remote ip when consider_all_requests_local is true' do
30+
Rails.stubs(:application).returns stub(:config => stub(:consider_all_requests_local => true))
31+
@app = ShowExceptionsController.action(:boom)
32+
self.remote_addr = '208.77.188.166'
33+
get '/'
34+
assert_match /boom/, body
35+
end
36+
end
37+
38+
class ShowExceptionsOverridenController < ShowExceptionsController
39+
private
40+
41+
def show_detailed_exceptions?
42+
params['detailed'] == '1'
43+
end
44+
end
45+
46+
class ShowExceptionsOverridenTest < ActionDispatch::IntegrationTest
47+
test 'show error page' do
48+
@app = ShowExceptionsOverridenController.action(:boom)
49+
get '/', {'detailed' => '0'}
50+
assert_equal "500 error fixture\n", body
51+
end
52+
53+
test 'show diagnostics message' do
54+
@app = ShowExceptionsOverridenController.action(:boom)
55+
get '/', {'detailed' => '1'}
56+
assert_match /boom/, body
57+
end
58+
end
59+
end

actionpack/test/dispatch/show_exceptions_test.rb

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
class ShowExceptionsTest < ActionDispatch::IntegrationTest
44

55
class Boomer
6-
def initialize(show_exceptions = false)
7-
@show_exceptions = show_exceptions
6+
def initialize(detailed = false)
7+
@detailed = detailed
88
end
99

1010
def call(env)
11-
env['action_dispatch.show_exceptions'] = @show_exceptions
11+
env['action_dispatch.show_detailed_exceptions'] = @detailed
1212
req = ActionDispatch::Request.new(env)
1313
case req.path
1414
when "/not_found"
@@ -32,9 +32,8 @@ def call(env)
3232
ProductionApp = ActionDispatch::ShowExceptions.new(Boomer.new(false))
3333
DevelopmentApp = ActionDispatch::ShowExceptions.new(Boomer.new(true))
3434

35-
test "rescue in public from a remote ip" do
35+
test "rescue with error page when show_exceptions is false" do
3636
@app = ProductionApp
37-
self.remote_addr = '208.77.188.166'
3837

3938
get "/", {}, {'action_dispatch.show_exceptions' => true}
4039
assert_response 500
@@ -49,32 +48,28 @@ def call(env)
4948
assert_equal "", body
5049
end
5150

52-
test "rescue locally from a local request" do
53-
@app = ProductionApp
54-
['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|
55-
self.remote_addr = ip_address
51+
test "rescue with diagnostics message when show_exceptions is true" do
52+
@app = DevelopmentApp
5653

57-
get "/", {}, {'action_dispatch.show_exceptions' => true}
58-
assert_response 500
59-
assert_match(/puke/, body)
54+
get "/", {}, {'action_dispatch.show_exceptions' => true}
55+
assert_response 500
56+
assert_match(/puke/, body)
6057

61-
get "/not_found", {}, {'action_dispatch.show_exceptions' => true}
62-
assert_response 404
63-
assert_match(/#{ActionController::UnknownAction.name}/, body)
58+
get "/not_found", {}, {'action_dispatch.show_exceptions' => true}
59+
assert_response 404
60+
assert_match(/#{ActionController::UnknownAction.name}/, body)
6461

65-
get "/method_not_allowed", {}, {'action_dispatch.show_exceptions' => true}
66-
assert_response 405
67-
assert_match(/ActionController::MethodNotAllowed/, body)
68-
end
62+
get "/method_not_allowed", {}, {'action_dispatch.show_exceptions' => true}
63+
assert_response 405
64+
assert_match(/ActionController::MethodNotAllowed/, body)
6965
end
7066

71-
test "localize public rescue message" do
67+
test "localize rescue error page" do
7268
# Change locale
7369
old_locale, I18n.locale = I18n.locale, :da
7470

7571
begin
7672
@app = ProductionApp
77-
self.remote_addr = '208.77.188.166'
7873

7974
get "/", {}, {'action_dispatch.show_exceptions' => true}
8075
assert_response 500
@@ -88,23 +83,6 @@ def call(env)
8883
end
8984
end
9085

91-
test "always rescue locally in development mode" do
92-
@app = DevelopmentApp
93-
self.remote_addr = '208.77.188.166'
94-
95-
get "/", {}, {'action_dispatch.show_exceptions' => true}
96-
assert_response 500
97-
assert_match(/puke/, body)
98-
99-
get "/not_found", {}, {'action_dispatch.show_exceptions' => true}
100-
assert_response 404
101-
assert_match(/#{ActionController::UnknownAction.name}/, body)
102-
103-
get "/method_not_allowed", {}, {'action_dispatch.show_exceptions' => true}
104-
assert_response 405
105-
assert_match(/ActionController::MethodNotAllowed/, body)
106-
end
107-
10886
test "does not show filtered parameters" do
10987
@app = DevelopmentApp
11088

@@ -114,16 +92,15 @@ def call(env)
11492
assert_match("&quot;foo&quot;=&gt;&quot;[FILTERED]&quot;", body)
11593
end
11694

117-
test "show registered original exception for wrapped exceptions when consider_all_requests_local is false" do
95+
test "show registered original exception for wrapped exceptions when show_exceptions is false" do
11896
@app = ProductionApp
119-
self.remote_addr = '208.77.188.166'
12097

12198
get "/not_found_original_exception", {}, {'action_dispatch.show_exceptions' => true}
12299
assert_response 404
123100
assert_match(/404 error/, body)
124101
end
125102

126-
test "show registered original exception for wrapped exceptions when consider_all_requests_local is true" do
103+
test "show registered original exception for wrapped exceptions when show_exceptions is true" do
127104
@app = DevelopmentApp
128105

129106
get "/not_found_original_exception", {}, {'action_dispatch.show_exceptions' => true}
@@ -132,7 +109,7 @@ def call(env)
132109
end
133110

134111
test "show the controller name in the diagnostics template when controller name is present" do
135-
@app = ProductionApp
112+
@app = DevelopmentApp
136113
get("/runtime_error", {}, {
137114
'action_dispatch.show_exceptions' => true,
138115
'action_dispatch.request.parameters' => {

0 commit comments

Comments
 (0)