-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathhooks.test.js
106 lines (94 loc) · 3.58 KB
/
hooks.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
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
'use strict'
const { test } = require('node:test')
const Fastify = require('fastify')
const kFastifyContext = require('fastify/lib/symbols').kRouteContext
const plugin = require('../')
const keys = new Set(['123456'])
const authorization = 'Bearer 123456'
test('onRequest hook used by default', async (t) => {
t.plan(9)
const fastify = Fastify()
fastify.register(plugin, { keys, addHook: undefined }).get('/test', (_req, res) => {
res.send({ hello: 'world' })
})
fastify.addHook('onResponse', (request, _reply, done) => {
t.assert.strictEqual(request[kFastifyContext].onError, null)
t.assert.strictEqual(request[kFastifyContext].onRequest.length, 1)
t.assert.strictEqual(request[kFastifyContext].onSend, null)
t.assert.strictEqual(request[kFastifyContext].preHandler, null)
t.assert.strictEqual(request[kFastifyContext].preParsing, null)
t.assert.strictEqual(request[kFastifyContext].preSerialization, null)
t.assert.strictEqual(request[kFastifyContext].preValidation, null)
done()
})
const response = await fastify.inject({
method: 'GET',
url: '/test',
headers: {
authorization
}
})
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(JSON.parse(response.body), { hello: 'world' })
})
test('preParsing hook used when specified', async (t) => {
t.plan(9)
const fastify = Fastify()
fastify.register(plugin, { keys, addHook: 'preParsing' }).get('/test', (_req, res) => {
res.send({ hello: 'world' })
})
fastify.addHook('onResponse', (request, _reply, done) => {
t.assert.strictEqual(request[kFastifyContext].onError, null)
t.assert.strictEqual(request[kFastifyContext].onRequest, null)
t.assert.strictEqual(request[kFastifyContext].onSend, null)
t.assert.strictEqual(request[kFastifyContext].preHandler, null)
t.assert.strictEqual(request[kFastifyContext].preParsing.length, 1)
t.assert.strictEqual(request[kFastifyContext].preSerialization, null)
t.assert.strictEqual(request[kFastifyContext].preValidation, null)
done()
})
const response = await fastify.inject({
method: 'GET',
url: '/test',
headers: {
authorization
}
})
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(JSON.parse(response.body), { hello: 'world' })
})
test('onRequest hook used when specified', async (t) => {
t.plan(9)
const fastify = Fastify()
fastify.register(plugin, { keys, addHook: 'onRequest' }).get('/test', (_req, res) => {
res.send({ hello: 'world' })
})
fastify.addHook('onResponse', (request, _reply, done) => {
t.assert.strictEqual(request[kFastifyContext].onError, null)
t.assert.strictEqual(request[kFastifyContext].onRequest.length, 1)
t.assert.strictEqual(request[kFastifyContext].onSend, null)
t.assert.strictEqual(request[kFastifyContext].preHandler, null)
t.assert.strictEqual(request[kFastifyContext].preParsing, null)
t.assert.strictEqual(request[kFastifyContext].preSerialization, null)
t.assert.strictEqual(request[kFastifyContext].preValidation, null)
done()
})
const response = await fastify.inject({
method: 'GET',
url: '/test',
headers: {
authorization
}
})
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(JSON.parse(response.body), { hello: 'world' })
})
test('error when invalid hook specified', async (t) => {
t.plan(1)
const fastify = Fastify()
try {
await fastify.register(plugin, { keys, addHook: 'onResponse' })
} catch (err) {
t.assert.strictEqual(err.message, 'options.addHook must be either "onRequest" or "preParsing"')
}
})