rack-jet_router 1.1.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGES.md +52 -0
- data/MIT-LICENSE +1 -1
- data/README.md +207 -52
- data/bench/Gemfile +13 -0
- data/bench/Rakefile.rb +11 -0
- data/bench/bench.rb +367 -0
- data/lib/rack/jet_router.rb +432 -208
- data/rack-jet_router.gemspec +21 -18
- data/test/builder_test.rb +631 -0
- data/test/misc_test.rb +23 -0
- data/test/router_test.rb +597 -0
- data/test/run_all.rb +6 -0
- data/test/{test_helper.rb → shared.rb} +8 -3
- metadata +36 -42
- data/Rakefile +0 -58
- data/test/rack/jet_router_test.rb +0 -670
@@ -1,670 +0,0 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
|
3
|
-
###
|
4
|
-
### $Release: 1.1.1 $
|
5
|
-
### $Copyright: copyright(c) 2015 kuwata-lab.com all rights reserved $
|
6
|
-
### $License: MIT License $
|
7
|
-
###
|
8
|
-
|
9
|
-
require_relative '../test_helper'
|
10
|
-
|
11
|
-
|
12
|
-
describe Rack::JetRouter do
|
13
|
-
|
14
|
-
welcome_app = proc {|env| [200, {}, ["welcome_app"]]}
|
15
|
-
#
|
16
|
-
book_list_api = proc {|env| [200, {}, ["book_list_api"]]}
|
17
|
-
book_create_api = proc {|env| [200, {}, ["book_create_api"]]}
|
18
|
-
book_new_api = proc {|env| [200, {}, ["book_new_api"]]}
|
19
|
-
book_show_api = proc {|env| [200, {}, ["book_show_api"]]}
|
20
|
-
book_update_api = proc {|env| [200, {}, ["book_update_api"]]}
|
21
|
-
book_delete_api = proc {|env| [200, {}, ["book_delete_api"]]}
|
22
|
-
book_edit_api = proc {|env| [200, {}, ["book_edit_api"]]}
|
23
|
-
#
|
24
|
-
comment_create_api = proc {|env| [200, {}, ["comment_create_api"]]}
|
25
|
-
comment_update_api = proc {|env| [200, {}, ["comment_update_api"]]}
|
26
|
-
#
|
27
|
-
admin_book_list_app = proc {|env| [200, {}, ["admin_book_list_app"]]}
|
28
|
-
admin_book_create_app = proc {|env| [200, {}, ["admin_book_create_app"]]}
|
29
|
-
admin_book_new_app = proc {|env| [200, {}, ["admin_book_new_app"]]}
|
30
|
-
admin_book_show_app = proc {|env| [200, {}, ["admin_book_show_app"]]}
|
31
|
-
admin_book_update_app = proc {|env| [200, {}, ["admin_book_update_app"]]}
|
32
|
-
admin_book_delete_app = proc {|env| [200, {}, ["admin_book_delete_app"]]}
|
33
|
-
admin_book_edit_app = proc {|env| [200, {}, ["admin_book_edit_app"]]}
|
34
|
-
#
|
35
|
-
whole_urlpath_mapping = [
|
36
|
-
['/' , welcome_app],
|
37
|
-
['/index.html' , welcome_app],
|
38
|
-
['/api' , [
|
39
|
-
['/books' , [
|
40
|
-
['' , book_list_api],
|
41
|
-
['/new' , book_new_api],
|
42
|
-
['/:id' , book_show_api],
|
43
|
-
['/:id/edit' , book_edit_api],
|
44
|
-
]],
|
45
|
-
['/books/:book_id/comments', [
|
46
|
-
['' , comment_create_api],
|
47
|
-
['/:comment_id' , comment_update_api],
|
48
|
-
]],
|
49
|
-
]],
|
50
|
-
['/admin' , [
|
51
|
-
['/books' , [
|
52
|
-
['' , {:GET=>admin_book_list_app, :POST=>admin_book_create_app}],
|
53
|
-
['/:id' , {:GET=>admin_book_show_app, :PUT=>admin_book_update_app, :DELETE=>admin_book_delete_app}],
|
54
|
-
]],
|
55
|
-
]],
|
56
|
-
]
|
57
|
-
#
|
58
|
-
jet_router = Rack::JetRouter.new(whole_urlpath_mapping)
|
59
|
-
#
|
60
|
-
def new_env(req_method, req_path, opts={})
|
61
|
-
opts[:method] = req_method.to_s
|
62
|
-
env = ::Rack::MockRequest.env_for(req_path, opts)
|
63
|
-
return env
|
64
|
-
end
|
65
|
-
|
66
|
-
|
67
|
-
describe '#range_of_urlpath_param()' do
|
68
|
-
|
69
|
-
it "[!syrdh] returns Range object when urlpath_pattern contains just one param." do
|
70
|
-
jet_router.instance_exec(self) do |_|
|
71
|
-
r1 = range_of_urlpath_param('/books/:id')
|
72
|
-
_.ok {r1} == (7..-1)
|
73
|
-
_.ok {'/books/123'[r1]} == '123'
|
74
|
-
r2 = range_of_urlpath_param('/books/:id.html')
|
75
|
-
_.ok {r2} == (7..-6)
|
76
|
-
_.ok {'/books/4567.html'[r2]} == '4567'
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
it "[!skh4z] returns nil when urlpath_pattern contains more than two params." do
|
81
|
-
jet_router.instance_exec(self) do |_|
|
82
|
-
_.ok {range_of_urlpath_param('/books/:book_id/comments/:comment_id')} == nil
|
83
|
-
_.ok {range_of_urlpath_param('/books/:id(:format)')} == nil
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
it "[!acj5b] returns nil when urlpath_pattern contains no params." do
|
88
|
-
jet_router.instance_exec(self) do |_|
|
89
|
-
_.ok {range_of_urlpath_param('/books')} == nil
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
end
|
94
|
-
|
95
|
-
|
96
|
-
describe '#compile_urlpath_pattern()' do
|
97
|
-
|
98
|
-
it "[!joozm] escapes metachars with backslash in text part." do
|
99
|
-
jet_router.instance_exec(self) do |_|
|
100
|
-
_.ok {compile_urlpath_pattern('/foo.html')} == ['/foo\.html', nil]
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
it "[!rpezs] converts '/books/:id' into '/books/([^./]+)'." do
|
105
|
-
jet_router.instance_exec(self) do |_|
|
106
|
-
_.ok {compile_urlpath_pattern('/books/:id')} == ['/books/([^./]+)', ['id']]
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
it "[!4dcsa] converts '/index(.:format)' into '/index(?:\.([^./]+))?'." do
|
111
|
-
jet_router.instance_exec(self) do |_|
|
112
|
-
_.ok {compile_urlpath_pattern('/index(.:format)')} == ['/index(?:\.([^./]+))?', ['format']]
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
it "[!1d5ya] rethrns compiled string and nil when no urlpath parameters nor parens." do
|
117
|
-
jet_router.instance_exec(self) do |_|
|
118
|
-
_.ok {compile_urlpath_pattern('/index')} == ['/index', nil]
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
it "[!of1zq] returns compiled string and urlpath param names when urlpath param or parens exist." do
|
123
|
-
jet_router.instance_exec(self) do |_|
|
124
|
-
_.ok {compile_urlpath_pattern('/books/:id')} == ['/books/([^./]+)', ['id']]
|
125
|
-
_.ok {compile_urlpath_pattern('/books/:id(.:format)')} == ['/books/([^./]+)(?:\.([^./]+))?', ['id', 'format']]
|
126
|
-
_.ok {compile_urlpath_pattern('/index(.html)')} == ['/index(?:\.html)?', []]
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
end
|
131
|
-
|
132
|
-
|
133
|
-
describe '#compile_mapping()' do
|
134
|
-
|
135
|
-
it "[!xzo7k] returns regexp, hash, and array." do
|
136
|
-
mapping = [
|
137
|
-
['/', welcome_app],
|
138
|
-
['/books/:id' , book_show_api],
|
139
|
-
]
|
140
|
-
expected = '
|
141
|
-
\A
|
142
|
-
/books/[^./]+(\z)
|
143
|
-
\z
|
144
|
-
'.gsub(/\s+/, '')
|
145
|
-
jet_router.instance_exec(self) do |_|
|
146
|
-
rexp, dict, list = compile_mapping(mapping)
|
147
|
-
_.ok {rexp} == Regexp.new(expected)
|
148
|
-
_.ok {dict} == {
|
149
|
-
'/' => welcome_app,
|
150
|
-
}
|
151
|
-
_.ok {list} == [
|
152
|
-
[%r'\A/books/([^./]+)\z', ['id'], book_show_api, (7..-1)],
|
153
|
-
]
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
it "[!gfxgr] deletes unnecessary grouping." do
|
158
|
-
mapping = [
|
159
|
-
['/' , welcome_app],
|
160
|
-
['/api/books' , book_list_api],
|
161
|
-
]
|
162
|
-
expected = '
|
163
|
-
\A
|
164
|
-
\z
|
165
|
-
'.gsub(/\s+/, '')
|
166
|
-
jet_router.instance_exec(self) do |_|
|
167
|
-
rexp, dict, list = compile_mapping(mapping)
|
168
|
-
_.ok {rexp} == Regexp.new(expected)
|
169
|
-
_.ok {dict} == {
|
170
|
-
'/' => welcome_app,
|
171
|
-
'/api/books' => book_list_api,
|
172
|
-
}
|
173
|
-
_.ok {list} == [
|
174
|
-
]
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
it "[!pv2au] deletes unnecessary urlpath regexp." do
|
179
|
-
mapping = [
|
180
|
-
['/' , welcome_app],
|
181
|
-
['/api', [
|
182
|
-
['/books', [
|
183
|
-
['' , book_list_api],
|
184
|
-
['/new' , book_new_api],
|
185
|
-
]],
|
186
|
-
['/books2', [
|
187
|
-
['/:id' , book_show_api],
|
188
|
-
]],
|
189
|
-
]],
|
190
|
-
]
|
191
|
-
expected = '
|
192
|
-
\A
|
193
|
-
(?:
|
194
|
-
/api
|
195
|
-
(?:
|
196
|
-
/books2
|
197
|
-
/[^./]+(\z)
|
198
|
-
)
|
199
|
-
)
|
200
|
-
\z
|
201
|
-
'.gsub(/\s+/, '')
|
202
|
-
jet_router.instance_exec(self) do |_|
|
203
|
-
rexp, dict, list = compile_mapping(mapping)
|
204
|
-
_.ok {rexp} == Regexp.new(expected)
|
205
|
-
_.ok {dict} == {
|
206
|
-
'/' => welcome_app,
|
207
|
-
'/api/books' => book_list_api,
|
208
|
-
'/api/books/new' => book_new_api,
|
209
|
-
}
|
210
|
-
_.ok {list} == [
|
211
|
-
[%r'\A/api/books2/([^./]+)\z', ['id'], book_show_api, (12..-1)],
|
212
|
-
]
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
it "[!bh9lo] deletes unnecessary grouping which contains only an element." do
|
217
|
-
mapping = [
|
218
|
-
['/api', [
|
219
|
-
['/books', [
|
220
|
-
['/:id' , book_show_api],
|
221
|
-
]],
|
222
|
-
]],
|
223
|
-
]
|
224
|
-
expected = '
|
225
|
-
\A
|
226
|
-
(?:
|
227
|
-
/api
|
228
|
-
(?:
|
229
|
-
/books
|
230
|
-
/[^./]+(\z)
|
231
|
-
)
|
232
|
-
)
|
233
|
-
\z
|
234
|
-
'.gsub(/\s+/, '')
|
235
|
-
jet_router.instance_exec(self) do |_|
|
236
|
-
rexp, dict, list = compile_mapping(mapping)
|
237
|
-
_.ok {rexp} == Regexp.new(expected)
|
238
|
-
_.ok {dict} == {}
|
239
|
-
_.ok {list} == [
|
240
|
-
[%r'\A/api/books/([^./]+)\z', ['id'], book_show_api, (11..-1)],
|
241
|
-
]
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
it "[!l63vu] handles urlpath pattern as fixed when no urlpath params." do
|
246
|
-
mapping = [
|
247
|
-
['/api/books' , book_list_api],
|
248
|
-
]
|
249
|
-
expected = '
|
250
|
-
\A
|
251
|
-
\z
|
252
|
-
'.gsub(/\s+/, '')
|
253
|
-
jet_router.instance_exec(self) do |_|
|
254
|
-
rexp, dict, list = compile_mapping(mapping)
|
255
|
-
_.ok {rexp} == Regexp.new(expected)
|
256
|
-
_.ok {dict} == {
|
257
|
-
'/api/books' => book_list_api,
|
258
|
-
}
|
259
|
-
_.ok {list} == [
|
260
|
-
]
|
261
|
-
end
|
262
|
-
end
|
263
|
-
|
264
|
-
it "[!vfytw] handles urlpath pattern as variable when urlpath param exists." do
|
265
|
-
mapping = [
|
266
|
-
['/api/books/:id' , book_show_api],
|
267
|
-
]
|
268
|
-
expected = '
|
269
|
-
\A
|
270
|
-
/api/books/[^./]+(\z)
|
271
|
-
\z
|
272
|
-
'.gsub(/\s+/, '')
|
273
|
-
jet_router.instance_exec(self) do |_|
|
274
|
-
rexp, dict, list = compile_mapping(mapping)
|
275
|
-
_.ok {rexp} == Regexp.new(expected)
|
276
|
-
_.ok {dict} == {
|
277
|
-
}
|
278
|
-
_.ok {list} == [
|
279
|
-
[%r'\A/api/books/([^./]+)\z', ['id'], book_show_api, (11..-1)],
|
280
|
-
]
|
281
|
-
end
|
282
|
-
end
|
283
|
-
|
284
|
-
it "[!2ktpf] handles end-point." do
|
285
|
-
mapping = [
|
286
|
-
['/' , welcome_app],
|
287
|
-
['/api/books' , book_list_api],
|
288
|
-
['/api/books/:id' , book_show_api],
|
289
|
-
]
|
290
|
-
expected = '
|
291
|
-
\A
|
292
|
-
/api/books/[^./]+(\z)
|
293
|
-
\z
|
294
|
-
'.gsub(/\s+/, '')
|
295
|
-
jet_router.instance_exec(self) do |_|
|
296
|
-
rexp, dict, list = compile_mapping(mapping)
|
297
|
-
_.ok {rexp} == Regexp.new(expected)
|
298
|
-
_.ok {dict} == {
|
299
|
-
'/' => welcome_app,
|
300
|
-
'/api/books' => book_list_api,
|
301
|
-
}
|
302
|
-
_.ok {list} == [
|
303
|
-
[%r'\A/api/books/([^./]+)\z', ['id'], book_show_api, (11..-1)],
|
304
|
-
]
|
305
|
-
end
|
306
|
-
end
|
307
|
-
|
308
|
-
it "[!ospaf] accepts nested mapping." do
|
309
|
-
mapping = [
|
310
|
-
['/admin', [
|
311
|
-
['/api', [
|
312
|
-
['/books', [
|
313
|
-
['', book_list_api],
|
314
|
-
['/:id', book_show_api],
|
315
|
-
]],
|
316
|
-
]],
|
317
|
-
]],
|
318
|
-
]
|
319
|
-
expected = '
|
320
|
-
\A
|
321
|
-
(?:
|
322
|
-
/admin
|
323
|
-
(?:
|
324
|
-
/api
|
325
|
-
(?:
|
326
|
-
/books
|
327
|
-
/[^./]+(\z)
|
328
|
-
)
|
329
|
-
)
|
330
|
-
)
|
331
|
-
\z
|
332
|
-
'.gsub(/\s+/, '')
|
333
|
-
jet_router.instance_exec(self) do |_|
|
334
|
-
rexp, dict, list = compile_mapping(mapping)
|
335
|
-
_.ok {rexp} == Regexp.new(expected)
|
336
|
-
_.ok {dict} == {
|
337
|
-
'/admin/api/books' => book_list_api,
|
338
|
-
}
|
339
|
-
_.ok {list} == [
|
340
|
-
[%r'\A/admin/api/books/([^./]+)\z', ['id'], book_show_api, (17..-1)],
|
341
|
-
]
|
342
|
-
end
|
343
|
-
end
|
344
|
-
|
345
|
-
describe "[!guhdc] if mapping dict is specified..." do
|
346
|
-
|
347
|
-
it "[!r7cmk] converts keys into string." do
|
348
|
-
mapping = [
|
349
|
-
['/books', {:GET=>book_list_api, :POST=>book_create_api}]
|
350
|
-
]
|
351
|
-
Rack::JetRouter.new([]).instance_exec(self) do |_|
|
352
|
-
rexp, dict, list = compile_mapping(mapping)
|
353
|
-
_.ok {dict['/books']} == {'GET'=>book_list_api, 'POST'=>book_create_api}
|
354
|
-
end
|
355
|
-
end
|
356
|
-
|
357
|
-
it "[!z9kww] allows 'ANY' as request method." do
|
358
|
-
mapping = [
|
359
|
-
['/books', {'ANY'=>book_list_api, 'POST'=>book_create_api}]
|
360
|
-
]
|
361
|
-
Rack::JetRouter.new([]).instance_exec(self) do |_|
|
362
|
-
rexp, dict, list = compile_mapping(mapping)
|
363
|
-
_.ok {dict['/books']} == {'ANY'=>book_list_api, 'POST'=>book_create_api}
|
364
|
-
end
|
365
|
-
end
|
366
|
-
|
367
|
-
it "[!k7sme] raises error when unknown request method specified." do
|
368
|
-
mapping = [
|
369
|
-
['/books', {"UNLOCK"=>book_list_api}]
|
370
|
-
]
|
371
|
-
Rack::JetRouter.new([]).instance_exec(self) do |_|
|
372
|
-
pr = proc { compile_mapping(mapping) }
|
373
|
-
_.ok {pr}.raise?(ArgumentError, '"UNLOCK": unknown request method.')
|
374
|
-
end
|
375
|
-
end
|
376
|
-
|
377
|
-
end
|
378
|
-
|
379
|
-
end
|
380
|
-
|
381
|
-
|
382
|
-
describe '#error_not_found()' do
|
383
|
-
|
384
|
-
it "[!mlruv] returns 404 response." do
|
385
|
-
expected = [404, {"Content-Type"=>"text/plain"}, ["404 Not Found"]]
|
386
|
-
env = new_env('GET', '/xxx')
|
387
|
-
jet_router.instance_exec(self) do |_|
|
388
|
-
_.ok {error_not_found(env)} == expected
|
389
|
-
end
|
390
|
-
end
|
391
|
-
|
392
|
-
end
|
393
|
-
|
394
|
-
|
395
|
-
describe '#error_not_allowed()' do
|
396
|
-
|
397
|
-
it "[!mjigf] returns 405 response." do
|
398
|
-
expected = [405, {"Content-Type"=>"text/plain"}, ["405 Method Not Allowed"]]
|
399
|
-
env = new_env('POST', '/')
|
400
|
-
jet_router.instance_exec(self) do |_|
|
401
|
-
_.ok {error_not_allowed(env)} == expected
|
402
|
-
end
|
403
|
-
end
|
404
|
-
|
405
|
-
end
|
406
|
-
|
407
|
-
|
408
|
-
describe '#initialize()' do
|
409
|
-
|
410
|
-
it "[!u2ff4] compiles urlpath mapping." do
|
411
|
-
jet_router.instance_exec(self) do |_|
|
412
|
-
expected = '
|
413
|
-
\A
|
414
|
-
(?:
|
415
|
-
/api
|
416
|
-
(?:
|
417
|
-
/books
|
418
|
-
(?:/[^./]+(\z)|/[^./]+/edit(\z))
|
419
|
-
|
|
420
|
-
/books/[^./]+/comments
|
421
|
-
(?:(\z)|/[^./]+(\z))
|
422
|
-
)
|
423
|
-
|
|
424
|
-
/admin
|
425
|
-
(?:/books
|
426
|
-
/[^./]+(\z)
|
427
|
-
)
|
428
|
-
)
|
429
|
-
\z
|
430
|
-
'.gsub(/\s+/, '')
|
431
|
-
_.ok {@urlpath_rexp} == Regexp.new(expected)
|
432
|
-
_.ok {@fixed_urlpath_dict} == {
|
433
|
-
'/' => welcome_app,
|
434
|
-
'/index.html' => welcome_app,
|
435
|
-
'/api/books' => book_list_api,
|
436
|
-
'/api/books/new' => book_new_api,
|
437
|
-
'/admin/books' => {
|
438
|
-
'GET'=>admin_book_list_app,
|
439
|
-
'POST'=>admin_book_create_app,
|
440
|
-
},
|
441
|
-
}
|
442
|
-
_.ok {@variable_urlpath_list} == [
|
443
|
-
[%r'\A/api/books/([^./]+)\z', ['id'], book_show_api, (11..-1)],
|
444
|
-
[%r'\A/api/books/([^./]+)/edit\z', ['id'], book_edit_api, (11..-6)],
|
445
|
-
[%r'\A/api/books/([^./]+)/comments\z', ['book_id'], comment_create_api, (11..-10)],
|
446
|
-
[%r'\A/api/books/([^./]+)/comments/([^./]+)\z', ['book_id', 'comment_id'], comment_update_api, nil],
|
447
|
-
[%r'\A/admin/books/([^./]+)\z', ['id'], {'GET' => admin_book_show_app,
|
448
|
-
'PUT' => admin_book_update_app,
|
449
|
-
'DELETE' => admin_book_delete_app}, (13..-1)],
|
450
|
-
]
|
451
|
-
end
|
452
|
-
end
|
453
|
-
|
454
|
-
end
|
455
|
-
|
456
|
-
|
457
|
-
describe '#lookup()' do
|
458
|
-
|
459
|
-
it "[!ijqws] returns mapped object and urlpath parameter values when urlpath found." do
|
460
|
-
ret = jet_router.lookup('/api/books/123')
|
461
|
-
ok {ret} == [book_show_api, {"id"=>"123"}]
|
462
|
-
end
|
463
|
-
|
464
|
-
it "[!vpdzn] returns nil when urlpath not found." do
|
465
|
-
ok {jet_router.lookup('/api')} == nil
|
466
|
-
ok {jet_router.lookup('/api/book')} == nil
|
467
|
-
ok {jet_router.lookup('/api/books/')} == nil
|
468
|
-
end
|
469
|
-
|
470
|
-
it "[!24khb] finds in fixed urlpaths at first." do
|
471
|
-
ok {jet_router.lookup('/')} == [welcome_app, nil]
|
472
|
-
ok {jet_router.lookup('/api/books')} == [book_list_api, nil]
|
473
|
-
dict = {'GET'=>admin_book_list_app, 'POST'=>admin_book_create_app}
|
474
|
-
ok {jet_router.lookup('/admin/books')} == [dict, nil]
|
475
|
-
end
|
476
|
-
|
477
|
-
it "[!iwyzd] urlpath param value is nil when found in fixed urlpaths." do
|
478
|
-
obj, vars = jet_router.lookup('/')
|
479
|
-
ok {vars} == nil
|
480
|
-
obj, vars = jet_router.lookup('/api/books')
|
481
|
-
ok {vars} == nil
|
482
|
-
end
|
483
|
-
|
484
|
-
it "[!upacd] finds in variable urlpath cache if it is enabled." do
|
485
|
-
mapping = [
|
486
|
-
['/api/books/:id', book_show_api],
|
487
|
-
]
|
488
|
-
r = Rack::JetRouter.new(mapping, urlpath_cache_size: 3)
|
489
|
-
pair = r.lookup('/api/books/123')
|
490
|
-
ok {pair} == [book_show_api, {"id"=>"123"}]
|
491
|
-
r.instance_exec(self) do |_|
|
492
|
-
_.ok {@variable_urlpath_cache} == {'/api/books/123'=>pair}
|
493
|
-
#
|
494
|
-
@variable_urlpath_cache['/api/books/999'] = [book_list_api, {"ID"=>"111"}]
|
495
|
-
end
|
496
|
-
pair = r.lookup('/api/books/999')
|
497
|
-
ok {pair} == [book_list_api, {"ID"=>"111"}]
|
498
|
-
end
|
499
|
-
|
500
|
-
it "[!84inr] caches result when variable urlpath cache enabled." do
|
501
|
-
mapping = [
|
502
|
-
['/books/:id', book_show_api],
|
503
|
-
]
|
504
|
-
r = Rack::JetRouter.new(mapping, urlpath_cache_size: 3)
|
505
|
-
#
|
506
|
-
pair1 = r.lookup('/books/1'); ok {pair1} == [book_show_api, {"id"=>"1"}]
|
507
|
-
pair2 = r.lookup('/books/2'); ok {pair2} == [book_show_api, {"id"=>"2"}]
|
508
|
-
pair3 = r.lookup('/books/3'); ok {pair3} == [book_show_api, {"id"=>"3"}]
|
509
|
-
r.instance_exec(self) do |_|
|
510
|
-
_.ok {@variable_urlpath_cache} == {
|
511
|
-
'/books/1'=>pair1,
|
512
|
-
'/books/2'=>pair2,
|
513
|
-
'/books/3'=>pair3,
|
514
|
-
}
|
515
|
-
end
|
516
|
-
#
|
517
|
-
pair4 = r.lookup('/books/4'); ok {pair4} == [book_show_api, {"id"=>"4"}]
|
518
|
-
r.instance_exec(self) do |_|
|
519
|
-
_.ok {@variable_urlpath_cache} == {
|
520
|
-
'/books/2'=>pair2,
|
521
|
-
'/books/3'=>pair3,
|
522
|
-
'/books/4'=>pair4,
|
523
|
-
}
|
524
|
-
end
|
525
|
-
end
|
526
|
-
|
527
|
-
it "[!1zx7t] variable urlpath cache is based on LRU." do
|
528
|
-
mapping = [
|
529
|
-
['/books/:id', book_show_api],
|
530
|
-
]
|
531
|
-
r = Rack::JetRouter.new(mapping, urlpath_cache_size: 3)
|
532
|
-
#
|
533
|
-
pair1 = r.lookup('/books/1')
|
534
|
-
pair2 = r.lookup('/books/2')
|
535
|
-
pair3 = r.lookup('/books/3')
|
536
|
-
pair4 = r.lookup('/books/4')
|
537
|
-
r.instance_exec(self) do |_|
|
538
|
-
_.ok {@variable_urlpath_cache} == {
|
539
|
-
'/books/2'=>pair2,
|
540
|
-
'/books/3'=>pair3,
|
541
|
-
'/books/4'=>pair4,
|
542
|
-
}
|
543
|
-
end
|
544
|
-
#
|
545
|
-
ok {r.lookup('/books/3')} == pair3
|
546
|
-
r.instance_exec(self) do |_|
|
547
|
-
_.ok {@variable_urlpath_cache} == {
|
548
|
-
'/books/2'=>pair2,
|
549
|
-
'/books/4'=>pair4,
|
550
|
-
'/books/3'=>pair3,
|
551
|
-
}
|
552
|
-
end
|
553
|
-
#
|
554
|
-
ok {r.lookup('/books/1')} == pair1
|
555
|
-
r.instance_exec(self) do |_|
|
556
|
-
_.ok {@variable_urlpath_cache} == {
|
557
|
-
'/books/4'=>pair4,
|
558
|
-
'/books/3'=>pair3,
|
559
|
-
'/books/1'=>pair1,
|
560
|
-
}
|
561
|
-
end
|
562
|
-
end
|
563
|
-
|
564
|
-
end
|
565
|
-
|
566
|
-
|
567
|
-
describe '#call()' do
|
568
|
-
|
569
|
-
it "[!hse47] invokes app mapped to request urlpath." do
|
570
|
-
ok {jet_router.call(new_env(:GET, '/api/books/123'))} == [200, {}, ["book_show_api"]]
|
571
|
-
ok {jet_router.call(new_env(:PUT, '/admin/books/123'))} == [200, {}, ["admin_book_update_app"]]
|
572
|
-
end
|
573
|
-
|
574
|
-
it "[!fpw8x] finds mapped app according to env['PATH_INFO']." do
|
575
|
-
ok {jet_router.call(new_env(:GET, '/api/books'))} == [200, {}, ["book_list_api"]]
|
576
|
-
ok {jet_router.call(new_env(:GET, '/api/books/123'))} == [200, {}, ["book_show_api"]]
|
577
|
-
end
|
578
|
-
|
579
|
-
it "[!wxt2g] guesses correct urlpath and redirects to it automaticaly when request path not found." do
|
580
|
-
headers = {"Content-Type"=>"text/plain", "Location"=>"/api/books"}
|
581
|
-
content = "Redirect to /api/books"
|
582
|
-
ok {jet_router.call(new_env(:GET, '/api/books/'))} == [301, headers, [content]]
|
583
|
-
#
|
584
|
-
headers = {"Content-Type"=>"text/plain", "Location"=>"/api/books/78"}
|
585
|
-
content = "Redirect to /api/books/78"
|
586
|
-
ok {jet_router.call(new_env(:GET, '/api/books/78/'))} == [301, headers, [content]]
|
587
|
-
end
|
588
|
-
|
589
|
-
it "[!3vsua] doesn't redict automatically when request path is '/'." do
|
590
|
-
r = Rack::JetRouter.new([['/api/books', book_list_api]])
|
591
|
-
ok {r.call(new_env(:GET, '/'))} == [404, {"Content-Type"=>"text/plain"}, ["404 Not Found"]]
|
592
|
-
end
|
593
|
-
|
594
|
-
it "[!30x0k] returns 404 when request urlpath not found." do
|
595
|
-
expected = [404, {"Content-Type"=>"text/plain"}, ["404 Not Found"]]
|
596
|
-
ok {jet_router.call(new_env(:GET, '/xxx'))} == expected
|
597
|
-
ok {jet_router.call(new_env(:GET, '/api/book'))} == expected
|
598
|
-
end
|
599
|
-
|
600
|
-
describe "[!gclbs] if mapped object is a Hash..." do
|
601
|
-
|
602
|
-
it "[!p1fzn] invokes app mapped to request method." do
|
603
|
-
ok {jet_router.call(new_env(:GET, '/admin/books'))} == [200, {}, ["admin_book_list_app"]]
|
604
|
-
ok {jet_router.call(new_env(:POST, '/admin/books'))} == [200, {}, ["admin_book_create_app"]]
|
605
|
-
ok {jet_router.call(new_env(:GET, '/admin/books/123'))} == [200, {}, ["admin_book_show_app"]]
|
606
|
-
ok {jet_router.call(new_env(:PUT, '/admin/books/123'))} == [200, {}, ["admin_book_update_app"]]
|
607
|
-
ok {jet_router.call(new_env(:DELETE, '/admin/books/123'))} == [200, {}, ["admin_book_delete_app"]]
|
608
|
-
end
|
609
|
-
|
610
|
-
it "[!5m64a] returns 405 when request method is not allowed." do
|
611
|
-
expected = [405, {"Content-Type"=>"text/plain"}, ["405 Method Not Allowed"]]
|
612
|
-
ok {jet_router.call(new_env(:PUT, '/admin/books'))} == expected
|
613
|
-
ok {jet_router.call(new_env(:FOOBAR, '/admin/books'))} == expected
|
614
|
-
end
|
615
|
-
|
616
|
-
it "[!ys1e2] uses GET method when HEAD is not mapped." do
|
617
|
-
ok {jet_router.call(new_env(:HEAD, '/admin/books'))} == [200, {}, ["admin_book_list_app"]]
|
618
|
-
ok {jet_router.call(new_env(:HEAD, '/admin/books/123'))} == [200, {}, ["admin_book_show_app"]]
|
619
|
-
end
|
620
|
-
|
621
|
-
it "[!2hx6j] try ANY method when request method is not mapped." do
|
622
|
-
mapping = [
|
623
|
-
['/admin/books', {:ANY=>admin_book_list_app}]
|
624
|
-
]
|
625
|
-
r = Rack::JetRouter.new(mapping)
|
626
|
-
expected = [200, {}, ["admin_book_list_app"]]
|
627
|
-
ok {r.call(new_env(:GET, '/admin/books'))} == expected
|
628
|
-
ok {r.call(new_env(:POST, '/admin/books'))} == expected
|
629
|
-
ok {r.call(new_env(:PUT, '/admin/books'))} == expected
|
630
|
-
ok {r.call(new_env(:DELETE, '/admin/books'))} == expected
|
631
|
-
end
|
632
|
-
|
633
|
-
end
|
634
|
-
|
635
|
-
it "[!2c32f] stores urlpath parameters as env['rack.urlpath_params']." do
|
636
|
-
env = new_env(:GET, '/api/books')
|
637
|
-
jet_router.call(env)
|
638
|
-
ok {env['rack.urlpath_params']} == nil
|
639
|
-
env = new_env(:GET, '/api/books/123')
|
640
|
-
jet_router.call(env)
|
641
|
-
ok {env['rack.urlpath_params']} == {"id"=>"123"}
|
642
|
-
env = new_env(:GET, '/api/books/123/comments/999')
|
643
|
-
jet_router.call(env)
|
644
|
-
ok {env['rack.urlpath_params']} == {"book_id"=>"123", "comment_id"=>"999"}
|
645
|
-
#
|
646
|
-
env = new_env(:GET, '/admin/books')
|
647
|
-
jet_router.call(env)
|
648
|
-
ok {env['rack.urlpath_params']} == nil
|
649
|
-
env = new_env(:GET, '/admin/books/123')
|
650
|
-
jet_router.call(env)
|
651
|
-
ok {env['rack.urlpath_params']} == {"id"=>"123"}
|
652
|
-
end
|
653
|
-
|
654
|
-
end
|
655
|
-
|
656
|
-
|
657
|
-
describe 'REQUEST_METHODS' do
|
658
|
-
|
659
|
-
it "[!haggu] contains available request methods." do
|
660
|
-
Rack::JetRouter::REQUEST_METHODS.each do |k, v|
|
661
|
-
ok {k}.is_a?(String)
|
662
|
-
ok {v}.is_a?(Symbol)
|
663
|
-
ok {v.to_s} == k
|
664
|
-
end
|
665
|
-
end
|
666
|
-
|
667
|
-
end
|
668
|
-
|
669
|
-
|
670
|
-
end
|