/
middleware.test.js
61 lines (52 loc) · 1.25 KB
/
middleware.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
'use strict'
const { test } = require('tap')
const Fastify = require('..')
const {
FST_ERR_DEC_ALREADY_PRESENT,
FST_ERR_MISSING_MIDDLEWARE
} = require('../lib/errors')
test('Should throw if the basic use API has not been overridden', t => {
t.plan(1)
const fastify = Fastify()
try {
fastify.use()
t.fail('Should throw')
} catch (err) {
t.ok(err instanceof FST_ERR_MISSING_MIDDLEWARE)
}
})
test('Should be able to override the default use API', t => {
t.plan(1)
const fastify = Fastify()
fastify.decorate('use', () => true)
t.equal(fastify.use(), true)
})
test('Cannot decorate use twice', t => {
t.plan(1)
const fastify = Fastify()
fastify.decorate('use', () => true)
try {
fastify.decorate('use', () => true)
} catch (err) {
t.ok(err instanceof FST_ERR_DEC_ALREADY_PRESENT)
}
})
test('Encapsulation works', t => {
t.plan(2)
const fastify = Fastify()
fastify.register((instance, opts, done) => {
instance.decorate('use', () => true)
t.equal(instance.use(), true)
done()
})
fastify.register((instance, opts, done) => {
try {
instance.use()
t.fail('Should throw')
} catch (err) {
t.ok(err instanceof FST_ERR_MISSING_MIDDLEWARE)
}
done()
})
fastify.ready()
})