-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
handler-context.test.js
56 lines (46 loc) · 1.38 KB
/
handler-context.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
'use strict'
const http = require('http')
const test = require('tap').test
const fastify = require('../')
test('handlers receive correct `this` context', (t) => {
t.plan(4)
// simulate plugin that uses fastify-plugin
const plugin = function (instance, opts, next) {
instance.decorate('foo', 'foo')
next()
}
plugin[Symbol.for('skip-override')] = true
const instance = fastify()
instance.register(plugin)
instance.get('/', function (req, reply) {
t.ok(this.foo)
t.is(this.foo, 'foo')
reply.send()
})
instance.listen(0, (err) => {
instance.server.unref()
if (err) t.threw(err)
t.ok(instance.foo)
t.is(instance.foo, 'foo')
const address = `http://127.0.0.1:${instance.server.address().port}/`
http.get(address, () => {}).on('error', t.threw)
})
})
test('handlers have access to the internal context', (t) => {
t.plan(5)
const instance = fastify()
instance.get('/', { config: { foo: 'bar' } }, function (req, reply) {
t.ok(reply.context)
t.ok(reply.context.config)
t.type(reply.context.config, Object)
t.ok(reply.context.config.foo)
t.is(reply.context.config.foo, 'bar')
reply.send()
})
instance.listen(0, (err) => {
instance.server.unref()
if (err) t.threw(err)
const address = `http://127.0.0.1:${instance.server.address().port}/`
http.get(address, () => {}).on('error', t.threw)
})
})