forked from scriptoLLC/take-five
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.ts
530 lines (486 loc) · 12.4 KB
/
router_test.ts
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// Copyright 2016-2021 the take-five authors. All rights reserved
// Based on wayfarer originally by Yoshua Wyuts
// Apache 2.0 license
import {
assert,
assertEquals,
assertThrows
} from 'https://deno.land/std@0.101.0/testing/asserts.ts'
import { Router } from './router.ts'
Deno.test({
name: 'should match a path',
fn(): Promise<void> {
return new Promise((resolve) => {
var r = new Router<void>()
r.on('/', function () {
assert(true, 'called')
resolve()
})
r.emit('/')
})
}
})
Deno.test({
name: 'should type returns',
fn(): Promise<void> {
return new Promise((resolve) => {
var r = new Router<Promise<string>>()
r.on('/', function () {
return new Promise((resolve1) => {
assert(true, 'called')
resolve1('hello')
resolve()
})
})
r.emit('/')
})
}
})
Deno.test({
name: 'should match a nested path',
fn(): Promise<void> {
return new Promise((resolve) => {
var r = new Router<void>()
r.on('/foo/bar', function () {
assert(true, 'called')
resolve()
})
r.emit('/foo/bar')
})
}
})
Deno.test({
name: 'should match a default path',
fn(): Promise<void> {
return new Promise((resolve) => {
var r = new Router<void>('/404')
r.on('/404', function () {
assert(true, 'default')
resolve()
})
r.emit('/nope')
})
}
})
Deno.test({
name: 'should allow passing of extra values',
fn (): Promise<void> {
return new Promise((resolve) => {
var foo = {}
var bar = {}
var r = new Router<void>()
r.on('/foo', function (_params, arg1, arg2) {
assertEquals(arg1, foo, 'arg1 was passed')
assertEquals(arg2, bar, 'arg2 was passed')
resolve()
})
r.emit('/foo', foo, bar)
})
}
})
Deno.test({
name: '.emit() should match paths',
fn(): Promise<void> {
let _catch = 0
return new Promise((resolve) => {
var r = new Router<void>()
r.on('/foo/bar', function () {
++_catch
assert(true, 'bar called')
if (_catch === 2) resolve()
})
r.on('/foo/baz', function () {
++_catch
assert(true, 'baz called')
if (_catch === 2) resolve()
})
r.emit('/foo/bar')
r.emit('/foo/baz')
})
}
})
Deno.test({
name: '.match() should match paths',
fn(): Promise<void> {
return new Promise((resolve) => {
var r = new Router<void>()
r.on('/foo/bar', function () {
assert(false, 'should not call callback')
})
r.on('/foo/baz', () => {})
var bar = r.match('/foo/bar')
assertEquals(bar.route, '/foo/bar', '/foo/bar route exists')
var baz = r.match('/foo/baz')
assertEquals(baz.route, '/foo/baz', '/foo/baz route exists')
setTimeout(() => resolve(), 1)
})
}
})
Deno.test({
name: '.emit() should match partials',
fn(): Promise<void> {
return new Promise((resolve) => {
var r = new Router<void>()
r.on('/:user', function (param) {
assertEquals(param.user, 'tobi', 'param matched')
resolve()
})
r.emit('/tobi')
})
}
})
Deno.test({
name: '.match() should match partials',
fn(): void {
var r = new Router<void>()
r.on('/:user', () => {})
var toby = r.match('/tobi')
assertEquals(toby.params.user, 'tobi')
}
})
Deno.test({
name: '.emit() should match paths before partials',
fn(): Promise<void> {
return new Promise((resolve) => {
var r = new Router<void>()
r.on('/foo', function () {
assert(true, 'called')
resolve()
})
r.on('/:user', () => {})
r.emit('/foo')
})
}
})
Deno.test({
name: '.emit() should allow path overriding',
fn(): Promise<void> {
return new Promise((resolve) => {
var r = new Router<void>()
r.on('/:user', function () {
assert(false, 'wrong callback called')
})
r.on('/:user', function () {
assert(true, 'called')
resolve()
})
r.emit('/foo')
})
}
})
Deno.test({
name: '.emit() should match nested partials',
fn(): Promise<void> {
return new Promise((resolve) => {
var r = new Router<void>()
r.on('/:user/:name', function (param) {
assertEquals(param.user, 'tobi', 'param matched')
assertEquals(param.name, 'baz', 'param matched')
resolve()
})
r.emit('/tobi/baz')
})
}
})
Deno.test({
name: '.emit() should parse encoded params',
fn(): Promise<void> {
return new Promise((resolve) => {
var r = new Router<void>()
r.on('/:channel', function (param) {
assertEquals(param.channel, '#choo', 'param matched')
resolve()
})
r.emit('/%23choo')
})
}
})
Deno.test({
name: '.emit() should throw if no matches are found',
fn(): void {
var r1 = new Router<void>()
assertThrows(r1.emit.bind(r1, '/whoops'))
}
})
Deno.test({
name: '.emit() should return values',
fn(): void {
var r1 = new Router<string>()
r1.on('/foo', function () {
return 'hello'
})
assertEquals(r1.emit('foo'), 'hello', 'returns value')
}
})
Deno.test({
name: '.emit() mount subrouters',
fn(): Promise<void> {
return new Promise((resolve) => {
let _catch = 0
var r4 = new Router<void>()
var r3 = new Router<void>()
r4.on('/kidlette', function () {
++_catch
assert(true, 'nested 2 levels')
if (_catch === 4) resolve()
})
r3.on('/mom', r4)
r3.emit('/mom/kidlette')
var r1 = new Router<void>()
var r2 = new Router<void>()
r2.on('/', function () {
++_catch
assert(true, 'nested 1 level')
//if (_catch === 4) resolve()
resolve()
})
r1.on('/home', r2)
r1.emit('/home')
var r5 = new Router<void>()
var r6 = new Router<void>()
r6.on('/child', function (param) {
++_catch
assertEquals(typeof param, 'object', 'param is passed')
assertEquals(param.parent, 'hello', 'nested 2 levels with params')
if (_catch === 4) resolve()
})
r5.on('/:parent', r6)
r5.emit('/hello/child')
var r7 = new Router<void>()
var r8 = new Router<void>()
var r9 = new Router<void>()
r9.on('/bar', function () {
++_catch
assert(true, 'nested 3 levels')
if (_catch === 4) resolve()
})
r8.on('/bin', r9)
r7.on('/foo', r8)
r7.emit('/foo/bin/bar')
})
}
})
Deno.test({
name: '.emit() should match nested partials of subrouters',
fn(): Promise<void> {
return new Promise((resolve) => {
var r1 = new Router<void>()
var r2 = new Router<void>()
var r3 = new Router<void>()
r3.on('/:grandchild', function (param) {
assertEquals(param.parent, 'bin', 'nested 3 levels with params')
assertEquals(param.child, 'bar', 'nested 3 levels with params')
assertEquals(param.grandchild, 'baz', 'nested 3 levels with parmas')
resolve()
})
r2.on('/:child', r3)
r1.on('/foo/:parent', r2)
r1.emit('/foo/bin/bar/baz')
})
}
})
Deno.test({
name: '.match() should return nested partials of subrouters',
fn(): void {
var r1 = new Router<void>()
var r2 = new Router<void>()
var r3 = new Router<void>()
r3.on('/:grandchild', () => {})
r2.on('/:child', r3)
r1.on('/foo/:parent', r2)
var matched = r1.match('/foo/bin/bar/baz')
assertEquals(matched.params.parent, 'bin')
assertEquals(matched.params.child, 'bar')
assertEquals(matched.params.grandchild, 'baz')
}
})
Deno.test({
name: '.match() returns a handler of a route',
fn(): Promise<void> {
return new Promise((resolve) => {
var r = new Router<void>()
r.on('/:user', function () {
assert(true, 'called')
resolve()
})
var toby = r.match('/tobi')
toby.handler && toby.handler({foo: "bar"})
})
}
})
Deno.test({
name: 'nested routes should call parent default route',
fn(): Promise<void> {
return new Promise((resolve) => {
var r1 = new Router<void>('/404')
var r2 = new Router<void>()
var r3 = new Router<void>()
r2.on('/bar', r3)
r1.on('foo', r2)
r1.on('/404', pass)
r1.emit('')
r1.emit('foo')
r1.emit('foo/bar')
r1.emit('foo/beep/boop')
function pass () {
assert(true, 'called')
resolve()
}
})
}
})
Deno.test({
name: 'wildcards',
fn(): Promise<void> {
return new Promise((resolve) => {
let _catch = 0
var r = new Router<void>()
r.on('/bar/*', function (params) {
++_catch
assertEquals(params.wildcard, 'foo/beep/boop')
if (_catch === 2) resolve()
})
r.on('/foo/:match/*', function (params) {
++_catch
assertEquals(params.match, 'bar')
assertEquals(params.wildcard, 'beep/boop')
if (_catch === 2) resolve()
})
r.emit('/bar/foo/beep/boop')
r.emit('/foo/bar/beep/boop')
})
}
})
Deno.test({
name: 'wildcards dont conflict with params',
fn(): Promise<void> {
return new Promise((resolve) => {
let _catch = 0
let router = new Router<void>()
router.on('/*', function () {
assert(false, 'wildcard called')
})
router.on('/:match', function () {
++_catch
assert(true, 'param called')
if (_catch === 3) resolve()
})
router.emit('/foo')
router = new Router<void>()
router.on('/*', function () {
assert(false, 'wildcard called')
})
router.on('/:match/foo', function () {
++_catch
assert(true, 'param called')
if (_catch === 3) resolve()
})
router.emit('/foo/foo')
router = new Router<void>()
router.on('/*', function () {
++_catch
assert(true, 'wildcard called')
if (_catch === 3) resolve()
})
router.on('/:match/foo', function () {
assert(false, 'param called')
})
router.emit('/foo/bar')
})
}
})
Deno.test({
name: 'safe decodeURIComponent',
fn(): Promise<void> {
return new Promise((resolve) => {
var r = new Router<void>('/404')
r.on('/test/:id', function () {
assert(false, 'we should not be here')
})
r.on('/404', function () {
assert(true, 'called')
resolve()
})
r.emit('/test/hel%"Flo')
})
}
})
Deno.test({
name: 'safe decodeURIComponent - nested route',
fn(): Promise<void> {
return new Promise((resolve) => {
var r = new Router<void>('/404')
r.on('/Deno.test/hello/world/:id/blah', function () {
assert(false, 'we should not be here')
})
r.on('/404', function () {
assert(true, 'called')
resolve()
})
r.emit('/Deno.test/hello/world/hel%"Flo/blah')
})
}
})
Deno.test({
name: 'safe decodeURIComponent - wildcard',
fn(): Promise<void> {
return new Promise((resolve) => {
var r = new Router<void>('/404')
r.on('/Deno.test/*', function () {
assert(false, 'we should not be here')
})
r.on('/404', function () {
assert(true, 'called')
resolve()
})
r.emit('/Deno.test/hel%"Flo')
})
}
})
Deno.test({
name: 'should expose .route property',
fn(): void {
var r = new Router<void>()
r.on('/foo', function () {})
assertEquals(r.match('/foo').route, '/foo', 'exposes route property')
}
})
Deno.test({
name: 'should be called with self',
fn(): Promise<void> {
return new Promise((resolve) => {
var r = new Router<void>()
r.on('/foo', function callback (this: typeof callback) {
assertEquals(this, callback, 'calling context is self')
resolve()
})
r.emit('/foo')
})
}
})
Deno.test({
name: 'can register callback on many routes',
fn(): Promise<void> {
return new Promise((resolve) => {
let _catch = 0
var r = new Router<void>()
var routes = ['/foo', '/bar']
r.on('/foo', callback)
r.on('/bar', callback)
const len = routes.length
let matched
let i = 0
for (i; i < len; i++) {
matched = r.match(routes[i])
assertEquals(matched.handler, callback, 'matched callback is same')
assertEquals(matched.route, routes[i], 'matched route is same')
r.emit(routes[i])
}
function callback (this: typeof callback) {
++_catch
assertEquals(this, callback, 'calling context is same')
if (_catch === 2) resolve()
}
})
}
})