-
Notifications
You must be signed in to change notification settings - Fork 4
/
rack-rescue_spec.rb
151 lines (123 loc) · 4.4 KB
/
rack-rescue_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "RackRescue" do
class RRUnknownException < RuntimeError; end
after do
Rack::Rescue.error_handlers.clear
end
def safe_endpoint(msg = "OK")
lambda{|e| Rack::Response.new(msg)}
end
def raise_endpoint(exception, msg)
lambda{|e| raise exception, msg }
end
def build_stack(endpoint = safe_endpoint)
Rack::Builder.new do
use Rack::Rescue
run endpoint
end
end
before do
@env = Rack::MockRequest.env_for("/")
end
it "should do nothing if there is no downstream exception" do
result = build_stack.call(@env)
result.body.map.join.should == "OK"
end
it "should render the error with the defautl text error handler if it's an unkonwn exception" do
@env['HTTP_ACCEPT'] = "text/plain"
bad_app = raise_endpoint(RRUnknownException, "Unknown Brew")
stack = build_stack(bad_app)
r = stack.call(@env)
r[0].should == 500
body = r[2].body.map.join
body.should include("Error")
body.should include("Unknown Brew")
end
describe "A known exception" do
it "should render the template for the error" do
bad_app = raise_endpoint(Pancake::Errors::NotFound, "Action Not Found")
stack = build_stack(bad_app)
@env['HTTP_ACCEPT'] = 'text/plain'
r = stack.call(@env)
body = r[2].body.map.join
body.should include("Pancake::Errors::NotFound")
end
it "should set the correct status code" do
bad_app = raise_endpoint(Pancake::Errors::NotFound, "Action Not Found")
stack = build_stack(bad_app)
@env['HTTP_ACCEPT'] = "text/plain"
r = stack.call(@env)
status = r[0]
status.should == 404
body = r[2].body.map.join
body.should include("Action Not Found")
end
end
it "should provide a list of default formats" do
Rack::Rescue.default_formats.should == Pancake::MimeTypes.groups.keys.map(&:to_sym)
rr = Rack::Rescue.new SUCCESS_APP
rr.formats.should == Pancake::MimeTypes.groups.keys.map(&:to_sym)
end
it "should let me overwrite the formats avaiable" do
rr = Rack::Rescue.new(SUCCESS_APP, :formats => [:text, :html])
rr.formats.should == [:text, :html]
end
it "should inspect the rack env and negotiate the content" do
env = Rack::MockRequest.env_for("/")
env['HTTP_ACCEPT'] = "text/xml"
rr = Rack::Rescue.new(raise_endpoint(RuntimeError, "Boo"))
result = rr.call(env)
result[0].should == 500
result[2].body.to_s.should include("XML Error Template")
end
it "should inspect the extension of the " do
env = Rack::MockRequest.env_for("/")
env['HTTP_ACCEPT'] = "text/xml"
rr = Rack::Rescue.new(raise_endpoint(RuntimeError, "Boo"))
result = rr.call(env)
result[0].should == 500
result[2].body.to_s.should include("XML Error Template")
end
it "should wrap the error in a layout if there's one present" do
endpoint = raise_endpoint(RuntimeError, "Bad Error")
layout_dir = File.join(File.expand_path(File.dirname(__FILE__)), 'rack', 'rescue', 'fixtures', 'layouts')
File.exists?(File.join(layout_dir, "error.html.erb")).should be_true
app = Rack::Builder.new do
use Wrapt do |wrapt|
wrapt.layout_dirs << layout_dir
end
use Rack::Rescue
run endpoint
end
env = Rack::MockRequest.env_for("/foo.html")
r = app.call(env)
r[0].should == 500
body = r[2].body.to_s
body.should include("<h1>Error Layout</h1>")
end
it "should allow me to add error handlers and they should execute on each exception" do
layout_dir = File.join(File.expand_path(File.dirname(__FILE__)), 'rack', 'rescue', 'fixtures', 'layouts')
Rack::Rescue.add_handler do |exception, env, options|
options[:format].should == :html
options[:layout].should == 'error'
end
Rack::Rescue.add_handler do |exception, env, options|
options[:format] = :xml
options[:layout] = 'custom_error_layout'
end
Rack::Rescue.add_handler do |exception, env, options|
options[:format].should == :xml
options[:layout].should == 'custom_error_layout'
end
app = Rack::Builder.new do
use Wrapt do |wrapt|
wrapt.layout_dirs << layout_dir
end
use Rack::Rescue
the_app = lambda{|e| raise "Halp"}
run the_app
end
result = app.call(Rack::MockRequest.env_for("/"))
result[2].body.join.should include("Custom Error Layout")
end
end