-
Notifications
You must be signed in to change notification settings - Fork 9
/
functionality.ts
153 lines (123 loc) · 4.06 KB
/
functionality.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
import test from 'ava'
import { Router } from '../src/router'
test('should match route with wildcard', async t => {
const router = new Router<string>()
router
.head('*', 'foo1')
.get('/', 'foo2')
.get('/foo', 'foo3')
const match1 = router.match('HEAD', '/foo')
const match2 = router.match('GET', '/')
const match3 = router.match('GET', '/foo')
const match4 = router.match('POST', '/foo')
t.true(match1 instanceof Object)
t.true(match2 instanceof Object)
t.true(match3 instanceof Object)
t.false(match4 instanceof Object)
if (match1) {
t.is(match1.handler, 'foo1')
}
if (match2) {
t.is(match2.handler, 'foo2')
}
if (match3) {
t.is(match3.handler, 'foo3')
t.true(match3.matches && match3.matches.length === 1)
t.is(match3.method, 'GET')
t.deepEqual(match3.options, {})
t.deepEqual(match3.params, {})
t.is(match3.path, '/foo')
}
})
test('should match wildcard OPTIONS', async t => {
const router = new Router<string>()
router.options('*', 'foo1')
const match1 = router.match('GET', '/foo')
const match2 = router.match('OPTIONS', '/foo')
t.false(match1 instanceof Object)
t.true(match2 instanceof Object)
if (match2) {
t.is(match2.handler, 'foo1')
}
})
test('should match wildcard method', async t => {
const router = new Router<string>()
router.all('/secret', 'foo1')
const match1 = router.match('GET', '/foo')
const match2 = router.match('OPTIONS', '/secret')
t.false(match1 instanceof Object)
t.true(match2 instanceof Object)
if (match2) {
t.is(match2.handler, 'foo1')
}
})
test('should match route with named params', async t => {
const router = new Router<string>()
router
.get('/(v1|v2)/:name/:age', 'foo1') // ok!
// .get('/(v1|v2)/:name/:age/*', 'foo2') // Not ok!
.get('/(v1|v2)/:name/:age/(.*)', 'foo3') // ok!
.get('/v2/:name/:age', 'foo4')
.get('/v3/alice', 'foo5')
const match1 = router.match('GET', '/v1/')
const match2 = router.match('GET', '/v1/bob/22')
const match3 = router.match('GET', '/v1/bob/22/hello')
const match4 = router.match('GET', '/v2/keith,89')
t.false(match1 instanceof Object)
t.true(match2 instanceof Object)
t.true(match3 instanceof Object)
t.false(match4 instanceof Object)
if (match2) {
t.is(match2.handler, 'foo1')
t.is(match2.params.name, 'bob')
t.is(match2.params.age, '22')
t.is(match2.params['0'], 'v1')
}
if (match3) {
t.is(match3.handler, 'foo3')
t.is(match3.params.name, 'bob')
t.is(match3.params.age, '22')
t.is(match3.params['0'], 'v1')
t.is(match3.params['1'], 'hello')
}
})
test('should not be case sensitive by default', async t => {
// By default not case sensitive
const router = new Router<string>()
router.all('/:name/LOCATION', 'foo1')
const match1 = router.match('GET', '/bob/')
const match2 = router.match('GET', '/bob/location')
const match3 = router.match('GET', '/bob/LOCATION')
t.false(match1 instanceof Object)
t.true(match2 instanceof Object)
t.true(match3 instanceof Object)
})
test('should allow options to be case sensitive', async t => {
// By default not case sensitive
const router = new Router<string>()
router.all('/:name/LOCATION', 'foo1', { sensitive: true })
const match1 = router.match('GET', '/bob/')
const match2 = router.match('GET', '/bob/location')
const match3 = router.match('GET', '/bob/LOCATION')
t.false(match1 instanceof Object)
t.false(match2 instanceof Object)
t.true(match3 instanceof Object)
})
type HandlerType = () => Response
test('should allow functions as handlers', async t => {
const router = new Router<HandlerType>()
router.all('*', () => new Response('Hello'))
const match1 = router.match('GET', '/foo')
t.true(match1 instanceof Object)
})
test('should allow async functions as handlers', async t => {
const router = new Router<() => Promise<number>>()
router.all('*', async () => 123)
const match = router.match('GET', '/foo')
t.true(match instanceof Object)
if (match) {
const response = await match.handler()
console.log(response)
// => Response('Hello')
}
})