-
-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathrouter.test.ts
More file actions
431 lines (304 loc) · 10.9 KB
/
router.test.ts
File metadata and controls
431 lines (304 loc) · 10.9 KB
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
import { describe, expect, it } from 'vitest'
import type { Response } from '../../packages/app/src'
import { type Middleware, pushMiddleware, Router } from '../../packages/router/src'
describe('Testing Router', () => {
describe('Basic', () => {
it('new router has empty ware array', () => {
const app = new Router()
expect(app.middleware).toStrictEqual([])
})
})
describe('.use method', () => {
it('new ware gets added via .use', () => {
const app = new Router()
app.use((_: unknown, res: Response) => res.end('hi'))
expect(app.middleware[0]).toMatchObject({
path: '/',
type: 'mw'
})
})
it('accepts a list of wares', () => {
const app = new Router()
app.use(
function m1(_req, _res, next) {
next()
},
function m2(_req, _res, next) {
next()
},
function m3(_req, _res, next) {
next()
}
)
expect(app.middleware).toHaveLength(3)
})
it('accepts an array of wares', () => {
const app = new Router<Router>()
app.use((_req, _res, next) => next?.(), [(_req, _res, next) => next?.(), (_req, _res, next) => next?.()])
expect(app.middleware).toHaveLength(3)
})
it('accepts an array of wares as a first argument', () => {
const app = new Router<Router>()
app.use([(_req, _res, next) => next?.(), (_req, _res, next) => next()])
expect(app.middleware).toHaveLength(2)
})
it('accepts an array of wares and path as first argument', () => {
const app = new Router<Router>()
app.use('/path', [(_req, _res, next) => next(), (_req, _res, next) => next()])
expect(app.middleware).toHaveLength(2)
expect(app.middleware.every((x) => x.path === '/path')).toBe(true)
})
})
})
describe('Testing HTTP methods', () => {
it('should accept arrays for first second arg', () => {
const router = new Router()
router.get('/', [() => 0, () => 0])
expect(router.middleware.length).toBe(2)
})
it('should accept arrays for third argument', () => {
const router = new Router()
router.get('/', () => 0, [() => 0, () => 0])
expect(router.middleware.length).toBe(3)
})
it('app.get should set GET as HTTP method', () => {
const router = new Router()
router.get('/', () => void 0)
expect(router.middleware[0].method).toBe('GET')
})
it('app.post should set POST as HTTP method', () => {
const router = new Router()
router.post('/', () => void 0)
expect(router.middleware[0].method).toBe('POST')
})
it('app.put should set PUT as HTTP method', () => {
const router = new Router()
router.put('/', () => void 0)
expect(router.middleware[0].method).toBe('PUT')
})
it('app.patch should set PATCH as HTTP method', () => {
const router = new Router()
router.patch('/', () => void 0)
expect(router.middleware[0].method).toBe('PATCH')
})
it('app.delete should set DELETE as HTTP method', () => {
const router = new Router()
router.delete('/', () => void 0)
expect(router.middleware[0].method).toBe('DELETE')
})
it('app.head should set HEAD as HTTP method', () => {
const router = new Router()
router.head('/', () => void 0)
expect(router.middleware[0].method).toBe('HEAD')
})
it('app.options should set OPTIONS as HTTP method', () => {
const router = new Router()
router.options('/', () => void 0)
expect(router.middleware[0].method).toBe('OPTIONS')
})
it('app.acl should set ACL as HTTP method', () => {
const router = new Router()
router.acl('/', () => void 0)
expect(router.middleware[0].method).toBe('ACL')
})
it('app.bind should set BIND as HTTP method', () => {
const router = new Router()
router.bind('/', () => void 0)
expect(router.middleware[0].method).toBe('BIND')
})
it('app.checkout should set CHECKOUT as HTTP method', () => {
const router = new Router()
router.checkout('/', () => void 0)
expect(router.middleware[0].method).toBe('CHECKOUT')
})
it('app.copy should set COPY as HTTP method', () => {
const router = new Router()
router.copy('/', () => void 0)
expect(router.middleware[0].method).toBe('COPY')
})
it('app.lock should set LOCK as HTTP method', () => {
const router = new Router()
router.lock('/', () => void 0)
expect(router.middleware[0].method).toBe('LOCK')
})
it('app.unlock should set UNLOCK as HTTP method', () => {
const router = new Router()
router.unlock('/', () => void 0)
expect(router.middleware[0].method).toBe('UNLOCK')
})
it('app.merge should set MERGE as HTTP method', () => {
const router = new Router()
router.merge('/', () => void 0)
expect(router.middleware[0].method).toBe('MERGE')
})
it('app.mkactivity should set MKACTIVITY as HTTP method', () => {
const router = new Router()
router.mkactivity('/', () => void 0)
expect(router.middleware[0].method).toBe('MKACTIVITY')
})
it('app.mkcol should set MKCOL as HTTP method', () => {
const router = new Router()
router.mkcol('/', () => void 0)
expect(router.middleware[0].method).toBe('MKCOL')
})
it('app.move should set MOVE as HTTP method', () => {
const router = new Router()
router.move('/', () => void 0)
expect(router.middleware[0].method).toBe('MOVE')
})
it('app.search should set SEARCH as HTTP method', () => {
const router = new Router()
router.search('/', () => void 0)
expect(router.middleware[0].method).toBe('SEARCH')
})
it('app.msearch should set M-SEARCH as HTTP method', () => {
const router = new Router()
router.msearch('/', () => void 0)
expect(router.middleware[0].method).toBe('M-SEARCH')
})
it('app.notify should set NOTIFY as HTTP method', () => {
const router = new Router()
router.notify('/', () => void 0)
expect(router.middleware[0].method).toBe('NOTIFY')
})
it('app.notify should set NOTIFY as HTTP method', () => {
const router = new Router()
router.notify('/', () => void 0)
expect(router.middleware[0].method).toBe('NOTIFY')
})
it('app.purge should set PURGE as HTTP method', () => {
const router = new Router()
router.purge('/', () => void 0)
expect(router.middleware[0].method).toBe('PURGE')
})
it('app.report should set REPORT as HTTP method', () => {
const router = new Router()
router.report('/', () => void 0)
expect(router.middleware[0].method).toBe('REPORT')
})
it('app.subscribe should set SUBSCRIBE as HTTP method', () => {
const router = new Router()
router.subscribe('/', () => void 0)
expect(router.middleware[0].method).toBe('SUBSCRIBE')
})
it('app.unsubscribe should set UNSUBSCRIBE as HTTP method', () => {
const router = new Router()
router.unsubscribe('/', () => void 0)
expect(router.middleware[0].method).toBe('UNSUBSCRIBE')
})
it('app.trace should set TRACE as HTTP method', () => {
const router = new Router()
router.trace('/', () => void 0)
expect(router.middleware[0].method).toBe('TRACE')
})
it('app.acl should set ACL as HTTP method', () => {
const router = new Router()
router.acl('/', () => void 0)
expect(router.middleware[0].method).toBe('ACL')
})
it('app.connect should set CONNECT as HTTP method', () => {
const router = new Router()
router.connect('/', () => void 0)
expect(router.middleware[0].method).toBe('CONNECT')
})
it('app.bind should set BIND as HTTP method', () => {
const router = new Router()
router.bind('/', () => void 0)
expect(router.middleware[0].method).toBe('BIND')
})
it('app.unbind should set UNBIND as HTTP method', () => {
const router = new Router()
router.unbind('/', () => void 0)
expect(router.middleware[0].method).toBe('UNBIND')
})
it('app.rebind should set REBIND as HTTP method', () => {
const router = new Router()
router.rebind('/', () => void 0)
expect(router.middleware[0].method).toBe('REBIND')
})
it('app.link should set LINK as HTTP method', () => {
const router = new Router()
router.link('/', () => void 0)
expect(router.middleware[0].method).toBe('LINK')
})
it('app.unlink should set UNLINK as HTTP method', () => {
const router = new Router()
router.unlink('/', () => void 0)
expect(router.middleware[0].method).toBe('UNLINK')
})
it('app.mkcalendar should set MKCALENDAR as HTTP method', () => {
const router = new Router()
router.mkcalendar('/', () => void 0)
expect(router.middleware[0].method).toBe('MKCALENDAR')
})
it('app.propfind should set PROPFIND as HTTP method', () => {
const router = new Router()
router.propfind('/', () => void 0)
expect(router.middleware[0].method).toBe('PROPFIND')
})
it('app.proppatch should set PROPPATCH as HTTP method', () => {
const router = new Router()
router.proppatch('/', () => void 0)
expect(router.middleware[0].method).toBe('PROPPATCH')
})
it('app.source should set SOURCE as HTTP method', () => {
const router = new Router()
router.source('/', () => void 0)
expect(router.middleware[0].method).toBe('SOURCE')
})
it('app.all should push all HTTP methods handlers', () => {
const router = new Router()
router.all('/', () => void 0)
expect(router.middleware[0].type).toBe('route')
})
it('should push a dummy GET HTTP method and its handlers and expect the middleware object to match', () => {
function dummyHandler(_req, res) {
res.send('Hello World!')
}
const middleware: Middleware[] = []
pushMiddleware(middleware)({
path: dummyHandler,
method: 'GET',
type: 'route'
})
expect(middleware[0]).toMatchObject({
path: '/',
method: 'GET',
type: 'route',
handler: dummyHandler,
fullPath: dummyHandler
})
})
it('should push a dummy GET HTTP method containing the fullPaths array and expect the middleware object to match', () => {
function dummyHandler(_req, res) {
res.send('Hello World!')
}
const fullPaths = ['']
const middleware: Middleware[] = []
pushMiddleware(middleware)({
method: 'GET',
type: 'route',
fullPaths: fullPaths,
handlers: [dummyHandler]
})
expect(middleware[0]).toMatchObject({
path: '/',
method: 'GET',
type: 'route'
})
})
it('app.get with array of paths should set GET as HTTP method', () => {
const router = new Router()
router.get(['/', '/test'], () => void 0)
expect(router.middleware).toHaveLength(2)
})
it('should skip non-string elements in path array', () => {
const router = new Router()
// Pass array with non-string element (type coercion scenario)
router.get(['/', 123 as unknown as string, '/test'], () => void 0)
// Should only register the string paths
expect(router.middleware).toHaveLength(2)
expect(router.middleware[0].path).toBe('/')
expect(router.middleware[1].path).toBe('/test')
})
})