-
Notifications
You must be signed in to change notification settings - Fork 434
/
middleware_spec.rb
301 lines (246 loc) · 9.63 KB
/
middleware_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
require "spec_helper"
module BetterErrors
describe Middleware do
let(:app) { Middleware.new(->env { ":)" }) }
let(:exception) { RuntimeError.new("oh no :(") }
it "passes non-error responses through" do
expect(app.call({})).to eq(":)")
end
it "calls the internal methods" do
expect(app).to receive :internal_call
app.call("PATH_INFO" => "/__better_errors/1/preform_awesomness")
end
it "calls the internal methods on any subfolder path" do
expect(app).to receive :internal_call
app.call("PATH_INFO" => "/any_sub/folder/path/__better_errors/1/preform_awesomness")
end
it "shows the error page" do
expect(app).to receive :show_error_page
app.call("PATH_INFO" => "/__better_errors/")
end
it "shows the error page on any subfolder path" do
expect(app).to receive :show_error_page
app.call("PATH_INFO" => "/any_sub/folder/path/__better_errors/")
end
it "doesn't show the error page to a non-local address" do
expect(app).not_to receive :better_errors_call
app.call("REMOTE_ADDR" => "1.2.3.4")
end
it "shows to a whitelisted IP" do
BetterErrors::Middleware.allow_ip! '77.55.33.11'
expect(app).to receive :better_errors_call
app.call("REMOTE_ADDR" => "77.55.33.11")
end
it "shows to a whitelisted IPAddr" do
BetterErrors::Middleware.allow_ip! IPAddr.new('77.55.33.0/24')
expect(app).to receive :better_errors_call
app.call("REMOTE_ADDR" => "77.55.33.11")
end
it "respects the X-Forwarded-For header" do
expect(app).not_to receive :better_errors_call
app.call(
"REMOTE_ADDR" => "127.0.0.1",
"HTTP_X_FORWARDED_FOR" => "1.2.3.4",
)
end
it "doesn't blow up when given a blank REMOTE_ADDR" do
expect { app.call("REMOTE_ADDR" => " ") }.to_not raise_error
end
it "doesn't blow up when given an IP address with a zone index" do
expect { app.call("REMOTE_ADDR" => "0:0:0:0:0:0:0:1%0" ) }.to_not raise_error
end
context "when requesting the /__better_errors manually" do
let(:app) { Middleware.new(->env { ":)" }) }
it "shows that no errors have been recorded" do
status, headers, body = app.call("PATH_INFO" => "/__better_errors")
expect(body.join).to match /No errors have been recorded yet./
end
it 'does not attempt to use ActionDispatch::ExceptionWrapper with a nil exception' do
ad_ew = double("ActionDispatch::ExceptionWrapper")
stub_const('ActionDispatch::ExceptionWrapper', ad_ew)
expect(ad_ew).to_not receive :new
status, headers, body = app.call("PATH_INFO" => "/__better_errors")
end
it "shows that no errors have been recorded on any subfolder path" do
status, headers, body = app.call("PATH_INFO" => "/any_sub/folder/path/__better_errors")
expect(body.join).to match /No errors have been recorded yet./
end
end
context "when handling an error" do
let(:app) { Middleware.new(->env { raise exception }) }
it "returns status 500" do
status, headers, body = app.call({})
expect(status).to eq(500)
end
if Exception.new.respond_to?(:cause)
context "cause" do
class OtherException < Exception
def initialize(message)
super(message)
end
end
it "shows Original Exception if it responds_to and has an cause" do
app = Middleware.new(->env {
begin
raise "Original Exception"
rescue
raise OtherException.new("Other Exception")
end
})
status, _, body = app.call({})
expect(status).to eq(500)
expect(body.join).not_to match(/\n> Other Exception\n/)
expect(body.join).to match(/\n> Original Exception\n/)
end
end
else
context "original_exception" do
class OriginalExceptionException < Exception
attr_reader :original_exception
def initialize(message, original_exception = nil)
super(message)
@original_exception = original_exception
end
end
it "shows Original Exception if it responds_to and has an original_exception" do
app = Middleware.new(->env {
raise OriginalExceptionException.new("Other Exception", Exception.new("Original Exception"))
})
status, _, body = app.call({})
expect(status).to eq(500)
expect(body.join).not_to match(/Other Exception/)
expect(body.join).to match(/Original Exception/)
end
it "won't crash if the exception responds_to but doesn't have an original_exception" do
app = Middleware.new(->env {
raise OriginalExceptionException.new("Other Exception")
})
status, _, body = app.call({})
expect(status).to eq(500)
expect(body.join).to match(/Other Exception/)
end
end
end
it "returns ExceptionWrapper's status_code" do
ad_ew = double("ActionDispatch::ExceptionWrapper")
allow(ad_ew).to receive('new').with({}, exception) { double("ExceptionWrapper", status_code: 404) }
stub_const('ActionDispatch::ExceptionWrapper', ad_ew)
status, headers, body = app.call({})
expect(status).to eq(404)
end
it "returns UTF-8 error pages" do
status, headers, body = app.call({})
expect(headers["Content-Type"]).to match /charset=utf-8/
end
it "returns text pages by default" do
status, headers, body = app.call({})
expect(headers["Content-Type"]).to match /text\/plain/
end
it "returns HTML pages by default" do
# Chrome's 'Accept' header looks similar this.
status, headers, body = app.call("HTTP_ACCEPT" => "text/html,application/xhtml+xml;q=0.9,*/*")
expect(headers["Content-Type"]).to match /text\/html/
end
context 'the logger' do
let(:logger) { double('logger', fatal: nil) }
before do
allow(BetterErrors).to receive(:logger).and_return(logger)
end
it "receives the exception as a fatal message" do
expect(logger).to receive(:fatal).with(/RuntimeError/)
app.call({})
end
context 'when Rails is being used' do
before do
skip("Rails not included in this run") unless defined? Rails
end
it "receives the exception without filtered backtrace frames" do
expect(logger).to receive(:fatal) do |message|
expect(message).to_not match(/rspec-core/)
end
app.call({})
end
end
context 'when Rails is not being used' do
before do
skip("Rails is included in this run") if defined? Rails
end
it "receives the exception with all backtrace frames" do
expect(logger).to receive(:fatal) do |message|
expect(message).to match(/rspec-core/)
end
app.call({})
end
end
end
end
context "requesting the variables for a specific frame" do
let(:env) { {} }
let(:result) {
app.call(
"PATH_INFO" => "/__better_errors/#{id}/#{method}",
# This is a POST request, and this is the body of the request.
"rack.input" => StringIO.new('{"index": 0}'),
)
}
let(:status) { result[0] }
let(:headers) { result[1] }
let(:body) { result[2].join }
let(:json_body) { JSON.parse(body) }
let(:id) { 'abcdefg' }
let(:method) { 'variables' }
context 'when no errors have been recorded' do
it 'returns a JSON error' do
expect(json_body).to match(
'error' => 'No exception information available',
'explanation' => /application has been restarted/,
)
end
context 'when Middleman is in use' do
let!(:middleman) { class_double("Middleman").as_stubbed_const }
it 'returns a JSON error' do
expect(json_body['explanation'])
.to match(/Middleman reloads all dependencies/)
end
end
context 'when Shotgun is in use' do
let!(:shotgun) { class_double("Shotgun").as_stubbed_const }
it 'returns a JSON error' do
expect(json_body['explanation'])
.to match(/The shotgun gem/)
end
context 'when Hanami is also in use' do
let!(:hanami) { class_double("Hanami").as_stubbed_const }
it 'returns a JSON error' do
expect(json_body['explanation'])
.to match(/--no-code-reloading/)
end
end
end
end
context 'when an error has been recorded' do
let(:error_page) { ErrorPage.new(exception, env) }
before do
app.instance_variable_set('@error_page', error_page)
end
context 'but it does not match the request' do
it 'returns a JSON error' do
expect(json_body).to match(
'error' => 'Session expired',
'explanation' => /no longer available in memory/,
)
end
end
context 'and it matches the request', :focus do
let(:id) { error_page.id }
it 'returns a JSON error' do
expect(error_page).to receive(:do_variables).and_return(html: "<content>")
expect(json_body).to match(
'html' => '<content>',
)
end
end
end
end
end
end