roda 3.18.0 → 3.19.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +24 -0
- data/README.rdoc +7 -9
- data/doc/conventions.rdoc +10 -10
- data/doc/release_notes/3.19.0.txt +229 -0
- data/lib/roda.rb +88 -45
- data/lib/roda/plugins/assets.rb +11 -4
- data/lib/roda/plugins/delay_build.rb +3 -30
- data/lib/roda/plugins/empty_root.rb +1 -1
- data/lib/roda/plugins/hash_routes.rb +455 -0
- data/lib/roda/plugins/match_hook.rb +69 -0
- data/lib/roda/plugins/multi_route.rb +4 -0
- data/lib/roda/plugins/multi_view.rb +4 -0
- data/lib/roda/plugins/optimized_string_matchers.rb +1 -1
- data/lib/roda/plugins/sessions.rb +63 -16
- data/lib/roda/plugins/static_routing.rb +7 -40
- data/lib/roda/version.rb +1 -1
- data/spec/define_roda_method_spec.rb +3 -0
- data/spec/freeze_spec.rb +10 -1
- data/spec/integration_spec.rb +1 -1
- data/spec/plugin/assets_spec.rb +16 -0
- data/spec/plugin/delay_build_spec.rb +2 -3
- data/spec/plugin/hash_routes_spec.rb +535 -0
- data/spec/plugin/match_hook_spec.rb +79 -0
- data/spec/plugin/middleware_spec.rb +1 -0
- data/spec/plugin/sessions_spec.rb +363 -320
- metadata +8 -2
@@ -0,0 +1,535 @@
|
|
1
|
+
require_relative "../spec_helper"
|
2
|
+
|
3
|
+
describe "hash_routes plugin - hash_routes DSL" do
|
4
|
+
before do
|
5
|
+
app(:bare) do
|
6
|
+
plugin :hash_routes
|
7
|
+
|
8
|
+
hash_routes "" do
|
9
|
+
on "a" do |r|
|
10
|
+
r.is "" do
|
11
|
+
"a0"
|
12
|
+
end
|
13
|
+
|
14
|
+
r.is "a" do
|
15
|
+
"a1"
|
16
|
+
end
|
17
|
+
|
18
|
+
r.hash_branches
|
19
|
+
|
20
|
+
"a2"
|
21
|
+
end
|
22
|
+
|
23
|
+
on "b" do |r|
|
24
|
+
r.is "" do
|
25
|
+
"b0"
|
26
|
+
end
|
27
|
+
|
28
|
+
r.is "a" do
|
29
|
+
"b1"
|
30
|
+
end
|
31
|
+
|
32
|
+
"b2"
|
33
|
+
end
|
34
|
+
|
35
|
+
is "c" do |r|
|
36
|
+
"c#{r.request_method}"
|
37
|
+
end
|
38
|
+
|
39
|
+
get 'd' do
|
40
|
+
'dg'
|
41
|
+
end
|
42
|
+
|
43
|
+
post 'e' do
|
44
|
+
'ep'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
hash_routes "/a" do |hr|
|
49
|
+
hr.dispatch_from(:p, "r")
|
50
|
+
|
51
|
+
hr.dispatch_from(:p, "s") do |r|
|
52
|
+
r.is "a" do
|
53
|
+
"psa1"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
hr.on "b" do |r|
|
58
|
+
r.is "" do
|
59
|
+
"ab0"
|
60
|
+
end
|
61
|
+
|
62
|
+
r.is "a" do
|
63
|
+
"ab1"
|
64
|
+
end
|
65
|
+
|
66
|
+
r.hash_branches
|
67
|
+
|
68
|
+
"ab2"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
phr = hash_routes(:p)
|
73
|
+
|
74
|
+
phr.is true do
|
75
|
+
"pi"
|
76
|
+
end
|
77
|
+
|
78
|
+
phr.on "x" do |r|
|
79
|
+
r.is do
|
80
|
+
'px'
|
81
|
+
end
|
82
|
+
|
83
|
+
'pnx'
|
84
|
+
end
|
85
|
+
|
86
|
+
route do |r|
|
87
|
+
r.hash_routes
|
88
|
+
|
89
|
+
r.on 'p' do
|
90
|
+
r.hash_routes(:p)
|
91
|
+
|
92
|
+
r.hash_routes("")
|
93
|
+
|
94
|
+
"p"
|
95
|
+
end
|
96
|
+
|
97
|
+
"n"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
it "adds support for routing via r.hash_routes" do
|
103
|
+
body.must_equal 'n'
|
104
|
+
body('/a').must_equal 'a2'
|
105
|
+
body('/a/').must_equal 'a0'
|
106
|
+
body('/a/a').must_equal 'a1'
|
107
|
+
body('/a/b').must_equal 'ab2'
|
108
|
+
body('/a/b/').must_equal 'ab0'
|
109
|
+
body('/a/b/a').must_equal 'ab1'
|
110
|
+
body('/b').must_equal 'b2'
|
111
|
+
body('/b/').must_equal 'b0'
|
112
|
+
body('/b/a').must_equal 'b1'
|
113
|
+
body('/c').must_equal 'cGET'
|
114
|
+
body('/c', 'REQUEST_METHOD'=>'POST').must_equal 'cPOST'
|
115
|
+
body('/c/').must_equal 'n'
|
116
|
+
body('/d').must_equal 'dg'
|
117
|
+
body('/d', 'REQUEST_METHOD'=>'POST').must_equal ''
|
118
|
+
body('/d/').must_equal 'n'
|
119
|
+
body('/e').must_equal ''
|
120
|
+
body('/e', 'REQUEST_METHOD'=>'POST').must_equal 'ep'
|
121
|
+
body('/e/').must_equal 'n'
|
122
|
+
body('/p').must_equal 'pi'
|
123
|
+
body('/p/x').must_equal 'px'
|
124
|
+
body('/p/x/1').must_equal 'pnx'
|
125
|
+
|
126
|
+
body('/p/a').must_equal 'a2'
|
127
|
+
body('/p/a/').must_equal 'a0'
|
128
|
+
body('/p/a/a').must_equal 'a1'
|
129
|
+
body('/p/a/b').must_equal 'a2'
|
130
|
+
body('/p/a/b/').must_equal 'a2'
|
131
|
+
body('/p/a/b/a').must_equal 'a2'
|
132
|
+
body('/p/b').must_equal 'b2'
|
133
|
+
body('/p/b/').must_equal 'b0'
|
134
|
+
body('/p/b/a').must_equal 'b1'
|
135
|
+
body('/p/c').must_equal 'cGET'
|
136
|
+
body('/p/c', 'REQUEST_METHOD'=>'POST').must_equal 'cPOST'
|
137
|
+
body('/p/c/').must_equal 'p'
|
138
|
+
body('/p/d').must_equal 'dg'
|
139
|
+
body('/p/d', 'REQUEST_METHOD'=>'POST').must_equal ''
|
140
|
+
body('/p/d/').must_equal 'p'
|
141
|
+
body('/p/e').must_equal ''
|
142
|
+
body('/p/e', 'REQUEST_METHOD'=>'POST').must_equal 'ep'
|
143
|
+
body('/p/e/').must_equal 'p'
|
144
|
+
body('/p/p').must_equal 'p'
|
145
|
+
body('/p/p/x').must_equal 'p'
|
146
|
+
body('/p/p/x/1').must_equal 'p'
|
147
|
+
|
148
|
+
body('/p/r/b').must_equal 'ab2'
|
149
|
+
body('/p/r/b/').must_equal 'ab0'
|
150
|
+
body('/p/r/b/a').must_equal 'ab1'
|
151
|
+
|
152
|
+
body('/p/s/a').must_equal 'psa1'
|
153
|
+
body('/p/s/b').must_equal 'ab2'
|
154
|
+
body('/p/s/b/').must_equal 'ab0'
|
155
|
+
body('/p/s/b/a').must_equal 'ab1'
|
156
|
+
end
|
157
|
+
|
158
|
+
it "works when freezing the app" do
|
159
|
+
app.freeze
|
160
|
+
body.must_equal 'n'
|
161
|
+
body('/a').must_equal 'a2'
|
162
|
+
body('/a/').must_equal 'a0'
|
163
|
+
proc{app.hash_branch("foo"){}}.must_raise
|
164
|
+
end
|
165
|
+
|
166
|
+
it "works when subclassing the app" do
|
167
|
+
old_app = app
|
168
|
+
@app = Class.new(app)
|
169
|
+
@app.route(&old_app.route_block)
|
170
|
+
body.must_equal 'n'
|
171
|
+
body('/a').must_equal 'a2'
|
172
|
+
body('/a/').must_equal 'a0'
|
173
|
+
body('/p/x').must_equal 'px'
|
174
|
+
end
|
175
|
+
|
176
|
+
it "handles loading the plugin multiple times correctly" do
|
177
|
+
app.plugin :hash_routes
|
178
|
+
body.must_equal 'n'
|
179
|
+
body('/a').must_equal 'a2'
|
180
|
+
body('/a/').must_equal 'a0'
|
181
|
+
body('/p/x').must_equal 'px'
|
182
|
+
end
|
183
|
+
|
184
|
+
it "r.hash_routes with verb handles loading the same route more than once" do
|
185
|
+
app.hash_routes "" do
|
186
|
+
get 'd' do
|
187
|
+
'dg'
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
body('/d').must_equal 'dg'
|
192
|
+
body('/d', 'REQUEST_METHOD'=>'POST').must_equal ''
|
193
|
+
body('/d/').must_equal 'n'
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
describe "hash_routes plugin - hash_branch" do
|
198
|
+
before do
|
199
|
+
app(:bare) do
|
200
|
+
plugin :hash_routes
|
201
|
+
|
202
|
+
hash_branch("a") do |r|
|
203
|
+
r.is "" do
|
204
|
+
"a0"
|
205
|
+
end
|
206
|
+
|
207
|
+
r.is "a" do
|
208
|
+
"a1"
|
209
|
+
end
|
210
|
+
|
211
|
+
r.hash_branches
|
212
|
+
|
213
|
+
"a2"
|
214
|
+
end
|
215
|
+
|
216
|
+
hash_branch("/a", "b") do |r|
|
217
|
+
r.is "" do
|
218
|
+
"ab0"
|
219
|
+
end
|
220
|
+
|
221
|
+
r.is "a" do
|
222
|
+
"ab1"
|
223
|
+
end
|
224
|
+
|
225
|
+
r.hash_branches
|
226
|
+
|
227
|
+
"ab2"
|
228
|
+
end
|
229
|
+
|
230
|
+
hash_branch("", "b") do |r|
|
231
|
+
r.is "" do
|
232
|
+
"b0"
|
233
|
+
end
|
234
|
+
|
235
|
+
r.is "a" do
|
236
|
+
"b1"
|
237
|
+
end
|
238
|
+
|
239
|
+
"b2"
|
240
|
+
end
|
241
|
+
|
242
|
+
hash_branch(:p, "x") do |r|
|
243
|
+
r.is do
|
244
|
+
'px'
|
245
|
+
end
|
246
|
+
|
247
|
+
'pnx'
|
248
|
+
end
|
249
|
+
|
250
|
+
route do |r|
|
251
|
+
r.hash_branches
|
252
|
+
|
253
|
+
r.on 'p' do
|
254
|
+
r.hash_branches(:p)
|
255
|
+
|
256
|
+
r.hash_branches("")
|
257
|
+
|
258
|
+
"p"
|
259
|
+
end
|
260
|
+
|
261
|
+
"n"
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
it "adds support for routing via r.hash_branches" do
|
267
|
+
body.must_equal 'n'
|
268
|
+
body('/a').must_equal 'a2'
|
269
|
+
body('/a/').must_equal 'a0'
|
270
|
+
body('/a/a').must_equal 'a1'
|
271
|
+
body('/a/b').must_equal 'ab2'
|
272
|
+
body('/a/b/').must_equal 'ab0'
|
273
|
+
body('/a/b/a').must_equal 'ab1'
|
274
|
+
body('/b').must_equal 'b2'
|
275
|
+
body('/b/').must_equal 'b0'
|
276
|
+
body('/b/a').must_equal 'b1'
|
277
|
+
body('/p').must_equal 'p'
|
278
|
+
body('/p/x').must_equal 'px'
|
279
|
+
body('/p/x/1').must_equal 'pnx'
|
280
|
+
|
281
|
+
body('/p/a').must_equal 'a2'
|
282
|
+
body('/p/a/').must_equal 'a0'
|
283
|
+
body('/p/a/a').must_equal 'a1'
|
284
|
+
body('/p/a/b').must_equal 'a2'
|
285
|
+
body('/p/a/b/').must_equal 'a2'
|
286
|
+
body('/p/a/b/a').must_equal 'a2'
|
287
|
+
body('/p/b').must_equal 'b2'
|
288
|
+
body('/p/b/').must_equal 'b0'
|
289
|
+
body('/p/b/a').must_equal 'b1'
|
290
|
+
body('/p/p').must_equal 'p'
|
291
|
+
body('/p/p/x').must_equal 'p'
|
292
|
+
body('/p/p/x/1').must_equal 'p'
|
293
|
+
end
|
294
|
+
|
295
|
+
it "works when freezing the app" do
|
296
|
+
app.freeze
|
297
|
+
body.must_equal 'n'
|
298
|
+
body('/a').must_equal 'a2'
|
299
|
+
body('/a/').must_equal 'a0'
|
300
|
+
proc{app.hash_branch("foo"){}}.must_raise
|
301
|
+
end
|
302
|
+
|
303
|
+
it "works when subclassing the app" do
|
304
|
+
old_app = app
|
305
|
+
@app = Class.new(app)
|
306
|
+
@app.route(&old_app.route_block)
|
307
|
+
body.must_equal 'n'
|
308
|
+
body('/a').must_equal 'a2'
|
309
|
+
body('/a/').must_equal 'a0'
|
310
|
+
body('/p/x').must_equal 'px'
|
311
|
+
end
|
312
|
+
|
313
|
+
it "handles loading the plugin multiple times correctly" do
|
314
|
+
app.plugin :hash_routes
|
315
|
+
body.must_equal 'n'
|
316
|
+
body('/a').must_equal 'a2'
|
317
|
+
body('/a/').must_equal 'a0'
|
318
|
+
body('/p/x').must_equal 'px'
|
319
|
+
end
|
320
|
+
|
321
|
+
it "r.hash_branch handles loading the same route more than once" do
|
322
|
+
app.hash_branch(:p, "x") do |r|
|
323
|
+
'px'
|
324
|
+
end
|
325
|
+
|
326
|
+
body('/p').must_equal 'p'
|
327
|
+
body('/p/x').must_equal 'px'
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
describe "hash_routes plugin - hash_path" do
|
332
|
+
before do
|
333
|
+
app(:bare) do
|
334
|
+
plugin :hash_routes
|
335
|
+
|
336
|
+
hash_path("/a") do |r|
|
337
|
+
r.get{"a"}
|
338
|
+
r.post{"ap"}
|
339
|
+
end
|
340
|
+
|
341
|
+
hash_path("", "/b") do |_|
|
342
|
+
"b"
|
343
|
+
end
|
344
|
+
|
345
|
+
hash_path("/c", "/b") do |_|
|
346
|
+
"cb"
|
347
|
+
end
|
348
|
+
|
349
|
+
hash_path(:p, "/x") do |_|
|
350
|
+
'px'
|
351
|
+
end
|
352
|
+
|
353
|
+
route do |r|
|
354
|
+
r.hash_paths
|
355
|
+
|
356
|
+
r.on 'p' do
|
357
|
+
r.hash_paths(:p)
|
358
|
+
|
359
|
+
r.hash_paths("")
|
360
|
+
|
361
|
+
"p"
|
362
|
+
end
|
363
|
+
|
364
|
+
r.on "c" do
|
365
|
+
r.hash_paths
|
366
|
+
|
367
|
+
"c"
|
368
|
+
end
|
369
|
+
|
370
|
+
"n"
|
371
|
+
end
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
it "adds support for routing via r.hash_paths" do
|
376
|
+
body.must_equal 'n'
|
377
|
+
body('/a').must_equal 'a'
|
378
|
+
body('/a', 'REQUEST_METHOD'=>'POST').must_equal 'ap'
|
379
|
+
body('/a/').must_equal 'n'
|
380
|
+
body('/b').must_equal 'b'
|
381
|
+
body('/b/').must_equal 'n'
|
382
|
+
body('/c').must_equal 'c'
|
383
|
+
body('/c/').must_equal 'c'
|
384
|
+
body('/c/b').must_equal 'cb'
|
385
|
+
body('/c/b/').must_equal 'c'
|
386
|
+
body('/p').must_equal 'p'
|
387
|
+
body('/p/x').must_equal 'px'
|
388
|
+
body('/p/x/1').must_equal 'p'
|
389
|
+
end
|
390
|
+
|
391
|
+
it "works when freezing the app" do
|
392
|
+
app.freeze
|
393
|
+
body.must_equal 'n'
|
394
|
+
body('/a').must_equal 'a'
|
395
|
+
body('/a/').must_equal 'n'
|
396
|
+
body('/p/x').must_equal 'px'
|
397
|
+
proc{app.hash_path("foo"){}}.must_raise
|
398
|
+
end
|
399
|
+
|
400
|
+
it "works when subclassing the app" do
|
401
|
+
old_app = app
|
402
|
+
@app = Class.new(app)
|
403
|
+
@app.route(&old_app.route_block)
|
404
|
+
body.must_equal 'n'
|
405
|
+
body('/a').must_equal 'a'
|
406
|
+
body('/a/').must_equal 'n'
|
407
|
+
body('/p/x').must_equal 'px'
|
408
|
+
end
|
409
|
+
|
410
|
+
it "handles loading the plugin multiple times correctly" do
|
411
|
+
app.plugin :hash_routes
|
412
|
+
body.must_equal 'n'
|
413
|
+
body('/a').must_equal 'a'
|
414
|
+
body('/a/').must_equal 'n'
|
415
|
+
body('/p/x').must_equal 'px'
|
416
|
+
end
|
417
|
+
|
418
|
+
it "r.hash_path handles loading the same route more than once" do
|
419
|
+
app.hash_path(:p, "x") do |r|
|
420
|
+
'px'
|
421
|
+
end
|
422
|
+
|
423
|
+
body('/p').must_equal 'p'
|
424
|
+
body('/p/x').must_equal 'px'
|
425
|
+
end
|
426
|
+
end
|
427
|
+
|
428
|
+
describe "hash_routes plugin" do
|
429
|
+
it "should work with route_block_args" do
|
430
|
+
app(:bare) do
|
431
|
+
plugin :hash_routes
|
432
|
+
plugin :route_block_args do
|
433
|
+
[request, response]
|
434
|
+
end
|
435
|
+
|
436
|
+
hash_branch 'a' do |r, res|
|
437
|
+
r.hash_paths
|
438
|
+
|
439
|
+
res.write('a')
|
440
|
+
end
|
441
|
+
|
442
|
+
hash_path '/a', '/b' do |r, res|
|
443
|
+
res.write('b')
|
444
|
+
end
|
445
|
+
|
446
|
+
route do |r|
|
447
|
+
r.hash_branches
|
448
|
+
|
449
|
+
'n'
|
450
|
+
end
|
451
|
+
end
|
452
|
+
|
453
|
+
body.must_equal 'n'
|
454
|
+
body('/a').must_equal 'a'
|
455
|
+
body('/a/').must_equal 'a'
|
456
|
+
body('/a/b').must_equal 'b'
|
457
|
+
body('/a/b/').must_equal 'a'
|
458
|
+
end
|
459
|
+
|
460
|
+
it "should have r.hash_routes dispatch to both hash_paths and hash_branches" do
|
461
|
+
app(:bare) do
|
462
|
+
plugin :hash_routes
|
463
|
+
plugin :route_block_args do
|
464
|
+
[request, response]
|
465
|
+
end
|
466
|
+
|
467
|
+
hash_branch 'a' do |r|
|
468
|
+
r.root do
|
469
|
+
'ar'
|
470
|
+
end
|
471
|
+
|
472
|
+
'ab'
|
473
|
+
end
|
474
|
+
|
475
|
+
hash_path '/a' do |_|
|
476
|
+
'ap'
|
477
|
+
end
|
478
|
+
|
479
|
+
hash_branch 'b' do |_|
|
480
|
+
'b'
|
481
|
+
end
|
482
|
+
|
483
|
+
hash_path '/c' do |_|
|
484
|
+
'c'
|
485
|
+
end
|
486
|
+
|
487
|
+
route do |r|
|
488
|
+
r.hash_routes
|
489
|
+
|
490
|
+
'n'
|
491
|
+
end
|
492
|
+
end
|
493
|
+
|
494
|
+
body.must_equal 'n'
|
495
|
+
body('/a').must_equal 'ap'
|
496
|
+
body('/a/').must_equal 'ar'
|
497
|
+
body('/a/b').must_equal 'ab'
|
498
|
+
body('/a').must_equal 'ap'
|
499
|
+
body('/b').must_equal 'b'
|
500
|
+
body('/b/').must_equal 'b'
|
501
|
+
body('/c').must_equal 'c'
|
502
|
+
body('/c/').must_equal 'n'
|
503
|
+
end
|
504
|
+
end
|
505
|
+
|
506
|
+
begin
|
507
|
+
require 'tilt/erb'
|
508
|
+
rescue LoadError
|
509
|
+
warn "tilt not installed, skipping hash_routes plugin views test"
|
510
|
+
else
|
511
|
+
describe "hash routes plugin" do
|
512
|
+
it "supports easy rendering of multiple views by name" do
|
513
|
+
app(:bare) do
|
514
|
+
plugin :render, :views=>'spec/views', :layout=>'layout-yield'
|
515
|
+
plugin :hash_routes
|
516
|
+
|
517
|
+
hash_routes '/d' do
|
518
|
+
view true, 'a'
|
519
|
+
view '', 'b'
|
520
|
+
views %w'c'
|
521
|
+
end
|
522
|
+
|
523
|
+
route do |r|
|
524
|
+
r.on 'd' do
|
525
|
+
r.hash_routes
|
526
|
+
end
|
527
|
+
end
|
528
|
+
end
|
529
|
+
|
530
|
+
body('/d').gsub(/\s+/, '').must_equal "HeaderaFooter"
|
531
|
+
body('/d/').gsub(/\s+/, '').must_equal "HeaderbFooter"
|
532
|
+
body('/d/c').gsub(/\s+/, '').must_equal "HeadercFooter"
|
533
|
+
end
|
534
|
+
end
|
535
|
+
end
|