-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcustom_spec.rb
303 lines (217 loc) · 5.63 KB
/
custom_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
302
303
# frozen_string_literal: true
module CustomTest
## Example of custom controller
class CustomController < Flame::Controller
def index; end
def foo
'This is foo'
end
def hello(name = 'world')
"Hello, #{name}!"
end
get ':?key/document',
def document(key = '')
return 'I have no document for you.' if key.empty?
"Here is your #{key} document."
end
# def page(*path_parts)
# path_parts.join '/'
# end
def error
raise 'Test'
end
def syntax_error
ERB.new('<% % %>').result(binding)
end
def merge_query_parameter(_id)
path_to :merge_query_parameter, params.merge(lang: 'ru')
end
private
def execute(action)
@action = action
return halt redirect :foo if request.path.include? '/old_foo'
super
{ a: 1 }
end
def not_found
response.header['Custom-Header'] = 'Hello from not_found'
halt redirect :foo if request.path.include? 'redirecting'
super
end
def default_body
result = "Some page about #{status} code"
result += "; exception is #{@exception.class}" if status == 500
result
end
def server_error(exception)
@exception = exception
super
end
end
## Mount example controller to app
class Application < Flame::Application
mount :custom
# puts router.routes
end
end
describe CustomTest do
include Rack::Test::Methods
subject { last_response }
let(:middlewares) { [] }
let(:app) do
builder = Rack::Builder.new
middlewares.each { |middleware| builder.use middleware }
builder.run CustomTest::Application.new
end
describe 'foo' do
before { get '/custom/foo' }
describe 'last_response' do
it { is_expected.to be_ok }
describe 'body' do
subject { super().body }
it { is_expected.to eq 'This is foo' }
end
end
end
describe 'hello with world' do
before { get '/custom/hello' }
describe 'last_response' do
it { is_expected.to be_ok }
describe 'body' do
subject { super().body }
it { is_expected.to eq 'Hello, world!' }
end
end
end
describe 'hello with name' do
before { get '/custom/hello/Alex' }
describe 'last_response' do
it { is_expected.to be_ok }
describe 'body' do
subject { super().body }
it { is_expected.to eq 'Hello, Alex!' }
end
end
end
describe 'document' do
context 'without key' do
before { get '/custom/document' }
describe 'last_response' do
it { is_expected.to be_ok }
describe 'body' do
subject { super().body }
it { is_expected.to eq 'I have no document for you.' }
end
end
end
context 'with a key' do
before { get '/custom/service_agreement/document' }
describe 'last_response' do
it { is_expected.to be_ok }
describe 'body' do
subject { super().body }
it { is_expected.to eq 'Here is your service_agreement document.' }
end
end
end
end
describe 'custom 404' do
before { get '/custom/foo/404' }
describe 'last_response' do
it { is_expected.to be_not_found }
describe 'body' do
subject { super().body }
it { is_expected.to eq 'Some page about 404 code' }
end
end
end
describe 'execute custom code for `not_found`' do
before { get '/custom/404' }
describe 'last_response' do
it { is_expected.to be_not_found }
describe 'Custom-Header' do
subject { super().headers['Custom-Header'] }
it { is_expected.to eq 'Hello from not_found' }
end
end
end
describe 'custom 500' do
shared_examples 'custom 500' do
describe 'last_response' do
it { is_expected.to be_server_error }
describe 'body' do
subject { super().body }
let(:expected_body) do
"Some page about 500 code; exception is #{exception}"
end
it { is_expected.to eq expected_body }
end
end
end
before do
hide_const 'BetterErrors' if hide_better_errors
end
let(:hide_better_errors) { true }
context 'with regular error' do
before { get '/custom/error' }
let(:exception) { RuntimeError }
it_behaves_like 'custom 500'
end
context 'with syntax error' do
before { get '/custom/syntax_error' }
let(:exception) { SyntaxError }
it_behaves_like 'custom 500'
end
## https://github.com/BetterErrors/better_errors/issues/454
context 'when there is `BetterErrors`' do
subject(:make_request) { get '/custom/error' }
let(:hide_better_errors) { false }
let(:middlewares) { [BetterErrors::Middleware] }
it { expect { make_request }.to raise_error 'Test' }
end
end
describe 'status and headers for HEAD request' do
before { head '/custom/foo' }
describe 'last_response' do
it { is_expected.to be_ok }
describe 'body' do
subject { super().body }
it { is_expected.to be_empty }
end
end
end
describe 'redirect with halt to `foo` from `not_found`' do
before { get '/custom/redirecting' }
describe 'last_response' do
it { is_expected.to be_redirect }
describe 'body' do
subject { super().body }
it { is_expected.to eq 'Some page about 302 code' }
end
end
end
describe 'merge query parameter' do
subject { super().body }
before { get path }
let(:path_without) { '/custom/merge_query_parameter/2?foo=bar' }
shared_examples 'correct path' do
it { is_expected.to eq "#{path_without}&lang=ru" }
end
context 'when does not exist' do
let(:path) { path_without }
it_behaves_like 'correct path'
end
context 'when exists' do
let(:path) { "#{path_without}&lang=en" }
it_behaves_like 'correct path'
end
end
describe 'execute `not_found` through `execute`' do
before { get '/custom/old_foo' }
it { is_expected.to be_redirect }
describe 'location' do
subject { super().location }
it { is_expected.to eq '/custom/foo' }
end
end
end