-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
default-route.test.js
63 lines (48 loc) · 1.36 KB
/
default-route.test.js
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
'use strict'
const t = require('tap')
const test = t.test
const Fastify = require('..')
test('setDefaultRoute should emit a deprecation warning', t => {
t.plan(2)
const fastify = Fastify()
const defaultRoute = (req, res) => {
res.end('hello from defaultRoute')
}
process.on('warning', onWarning)
function onWarning (warning) {
t.equal(warning.name, 'FastifyDeprecation')
t.equal(warning.code, 'FSTDEP014')
}
t.teardown(() => process.removeListener('warning', onWarning))
fastify.setDefaultRoute(defaultRoute)
fastify.getDefaultRoute()
})
test('should fail if defaultRoute is not a function', t => {
t.plan(1)
const fastify = Fastify()
const defaultRoute = {}
fastify.get('/', () => {})
try {
fastify.setDefaultRoute(defaultRoute)
} catch (error) {
t.equal(error.code, 'FST_ERR_DEFAULT_ROUTE_INVALID_TYPE')
}
})
test('correctly sets, returns, and calls defaultRoute', t => {
t.plan(3)
const fastify = Fastify()
const defaultRoute = (req, res) => {
res.end('hello from defaultRoute')
}
fastify.setDefaultRoute(defaultRoute)
const returnedDefaultRoute = fastify.getDefaultRoute()
t.equal(returnedDefaultRoute, defaultRoute)
fastify.get('/', () => {})
fastify.inject({
method: 'GET',
url: '/random'
}, (err, res) => {
t.error(err)
t.equal(res.body, 'hello from defaultRoute')
})
})