fastify 5.3.3 → 5.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/build/build-validation.js +2 -1
- package/docs/Guides/Delay-Accepting-Requests.md +3 -3
- package/docs/Guides/Ecosystem.md +9 -5
- package/docs/Reference/ContentTypeParser.md +1 -1
- package/docs/Reference/Errors.md +2 -2
- package/docs/Reference/Hooks.md +14 -14
- package/docs/Reference/Logging.md +3 -3
- package/docs/Reference/Middleware.md +1 -1
- package/docs/Reference/Reply.md +8 -8
- package/docs/Reference/Request.md +1 -1
- package/docs/Reference/Routes.md +3 -3
- package/docs/Reference/Server.md +35 -21
- package/docs/Reference/Validation-and-Serialization.md +1 -1
- package/fastify.d.ts +2 -1
- package/fastify.js +14 -2
- package/lib/configValidator.js +1 -1
- package/lib/errors.js +6 -0
- package/lib/pluginOverride.js +3 -1
- package/lib/reply.js +7 -11
- package/lib/request.js +3 -10
- package/lib/symbols.js +1 -0
- package/lib/warnings.js +8 -0
- package/package.json +8 -4
- package/test/404s.test.js +226 -325
- package/test/allow-unsafe-regex.test.js +19 -48
- package/test/als.test.js +28 -40
- package/test/async-await.test.js +11 -2
- package/test/body-limit.test.js +41 -65
- package/test/build-certificate.js +1 -1
- package/test/custom-parser-async.test.js +17 -22
- package/test/decorator-namespace.test._js_ +3 -4
- package/test/diagnostics-channel/async-delay-request.test.js +7 -16
- package/test/diagnostics-channel/sync-delay-request.test.js +7 -16
- package/test/helper.js +1 -1
- package/test/hooks-async.test.js +248 -218
- package/test/hooks.test.js +910 -769
- package/test/http-methods/lock.test.js +31 -31
- package/test/http-methods/mkcol.test.js +5 -9
- package/test/http-methods/proppatch.test.js +23 -29
- package/test/http-methods/report.test.js +44 -69
- package/test/http-methods/search.test.js +67 -82
- package/test/http2/closing.test.js +38 -20
- package/test/http2/secure-with-fallback.test.js +28 -27
- package/test/https/https.test.js +56 -53
- package/test/internals/errors.test.js +1 -1
- package/test/internals/handle-request.test.js +49 -66
- package/test/issue-4959.test.js +12 -3
- package/test/listen.4.test.js +31 -43
- package/test/nullable-validation.test.js +33 -46
- package/test/output-validation.test.js +24 -26
- package/test/plugin.2.test.js +104 -86
- package/test/plugin.3.test.js +56 -35
- package/test/plugin.4.test.js +124 -119
- package/test/proto-poisoning.test.js +78 -97
- package/test/request-error.test.js +0 -46
- package/test/route-hooks.test.js +112 -92
- package/test/route-prefix.test.js +194 -133
- package/test/schema-serialization.test.js +177 -154
- package/test/schema-special-usage.test.js +165 -132
- package/test/schema-validation.test.js +242 -205
- package/test/set-error-handler.test.js +58 -1
- package/test/skip-reply-send.test.js +64 -69
- package/test/trust-proxy.test.js +32 -58
- package/test/types/fastify.test-d.ts +3 -0
- package/test/types/request.test-d.ts +1 -0
- package/test/url-rewriting.test.js +45 -62
- package/types/request.d.ts +1 -0
- package/.taprc +0 -7
- package/.vscode/settings.json +0 -22
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { test } = require('node:test')
|
|
4
|
-
const sget = require('simple-get').concat
|
|
5
4
|
const Fastify = require('..')
|
|
6
5
|
|
|
7
6
|
test('nullable string', (t, done) => {
|
|
@@ -52,8 +51,8 @@ test('nullable string', (t, done) => {
|
|
|
52
51
|
})
|
|
53
52
|
})
|
|
54
53
|
|
|
55
|
-
test('object or null body', (t
|
|
56
|
-
t.plan(
|
|
54
|
+
test('object or null body', async (t) => {
|
|
55
|
+
t.plan(4)
|
|
57
56
|
|
|
58
57
|
const fastify = Fastify()
|
|
59
58
|
|
|
@@ -88,24 +87,20 @@ test('object or null body', (t, done) => {
|
|
|
88
87
|
}
|
|
89
88
|
})
|
|
90
89
|
|
|
91
|
-
fastify.listen({ port: 0 }
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
method: 'POST',
|
|
97
|
-
url: 'http://localhost:' + fastify.server.address().port
|
|
98
|
-
}, (err, response, body) => {
|
|
99
|
-
t.assert.ifError(err)
|
|
100
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
101
|
-
t.assert.deepStrictEqual(JSON.parse(body), { isUndefinedBody: true })
|
|
102
|
-
done()
|
|
103
|
-
})
|
|
90
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
91
|
+
t.after(() => { fastify.close() })
|
|
92
|
+
|
|
93
|
+
const result = await fetch(fastifyServer, {
|
|
94
|
+
method: 'POST'
|
|
104
95
|
})
|
|
96
|
+
|
|
97
|
+
t.assert.ok(result.ok)
|
|
98
|
+
t.assert.strictEqual(result.status, 200)
|
|
99
|
+
t.assert.deepStrictEqual(await result.json(), { isUndefinedBody: true })
|
|
105
100
|
})
|
|
106
101
|
|
|
107
|
-
test('nullable body', (t
|
|
108
|
-
t.plan(
|
|
102
|
+
test('nullable body', async (t) => {
|
|
103
|
+
t.plan(4)
|
|
109
104
|
|
|
110
105
|
const fastify = Fastify()
|
|
111
106
|
|
|
@@ -141,24 +136,20 @@ test('nullable body', (t, done) => {
|
|
|
141
136
|
}
|
|
142
137
|
})
|
|
143
138
|
|
|
144
|
-
fastify.listen({ port: 0 }
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
method: 'POST',
|
|
150
|
-
url: 'http://localhost:' + fastify.server.address().port
|
|
151
|
-
}, (err, response, body) => {
|
|
152
|
-
t.assert.ifError(err)
|
|
153
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
154
|
-
t.assert.deepStrictEqual(JSON.parse(body), { isUndefinedBody: true })
|
|
155
|
-
done()
|
|
156
|
-
})
|
|
139
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
140
|
+
t.after(() => fastify.close())
|
|
141
|
+
|
|
142
|
+
const result = await fetch(fastifyServer, {
|
|
143
|
+
method: 'POST'
|
|
157
144
|
})
|
|
145
|
+
|
|
146
|
+
t.assert.ok(result.ok)
|
|
147
|
+
t.assert.strictEqual(result.status, 200)
|
|
148
|
+
t.assert.deepStrictEqual(await result.json(), { isUndefinedBody: true })
|
|
158
149
|
})
|
|
159
150
|
|
|
160
|
-
test('Nullable body with 204', (t
|
|
161
|
-
t.plan(
|
|
151
|
+
test('Nullable body with 204', async (t) => {
|
|
152
|
+
t.plan(4)
|
|
162
153
|
|
|
163
154
|
const fastify = Fastify()
|
|
164
155
|
|
|
@@ -183,18 +174,14 @@ test('Nullable body with 204', (t, done) => {
|
|
|
183
174
|
}
|
|
184
175
|
})
|
|
185
176
|
|
|
186
|
-
fastify.listen({ port: 0 }
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
method: 'POST',
|
|
192
|
-
url: 'http://localhost:' + fastify.server.address().port
|
|
193
|
-
}, (err, response, body) => {
|
|
194
|
-
t.assert.ifError(err)
|
|
195
|
-
t.assert.strictEqual(response.statusCode, 204)
|
|
196
|
-
t.assert.strictEqual(body.length, 0)
|
|
197
|
-
done()
|
|
198
|
-
})
|
|
177
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
178
|
+
t.after(() => fastify.close())
|
|
179
|
+
|
|
180
|
+
const result = await fetch(fastifyServer, {
|
|
181
|
+
method: 'POST'
|
|
199
182
|
})
|
|
183
|
+
|
|
184
|
+
t.assert.ok(result.ok)
|
|
185
|
+
t.assert.strictEqual(result.status, 204)
|
|
186
|
+
t.assert.strictEqual((await result.text()).length, 0)
|
|
200
187
|
})
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { test } = require('node:test')
|
|
4
|
-
const sget = require('simple-get').concat
|
|
5
4
|
const fastify = require('..')()
|
|
6
5
|
|
|
7
6
|
const opts = {
|
|
@@ -90,37 +89,33 @@ test('unlisted response code', t => {
|
|
|
90
89
|
})
|
|
91
90
|
|
|
92
91
|
test('start server and run tests', async (t) => {
|
|
93
|
-
await fastify.listen({ port: 0 })
|
|
94
|
-
const baseUrl = 'http://localhost:' + fastify.server.address().port
|
|
92
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
95
93
|
t.after(() => fastify.close())
|
|
96
94
|
|
|
97
|
-
function sgetAsync (opts) {
|
|
98
|
-
return new Promise((resolve, reject) => {
|
|
99
|
-
sget(opts, (err, res, body) => {
|
|
100
|
-
if (err) return reject(err)
|
|
101
|
-
resolve({ res, body })
|
|
102
|
-
})
|
|
103
|
-
})
|
|
104
|
-
}
|
|
105
|
-
|
|
106
95
|
await test('shorthand - string get ok', async (t) => {
|
|
107
|
-
const
|
|
108
|
-
t.assert.
|
|
109
|
-
t.assert.strictEqual(
|
|
96
|
+
const result = await fetch(fastifyServer + '/string')
|
|
97
|
+
t.assert.ok(result.ok)
|
|
98
|
+
t.assert.strictEqual(result.status, 200)
|
|
99
|
+
const body = await result.text()
|
|
100
|
+
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
|
110
101
|
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
111
102
|
})
|
|
112
103
|
|
|
113
104
|
await test('shorthand - number get ok', async (t) => {
|
|
114
|
-
const
|
|
115
|
-
t.assert.
|
|
116
|
-
t.assert.strictEqual(
|
|
105
|
+
const result = await fetch(fastifyServer + '/number')
|
|
106
|
+
t.assert.ok(result.ok)
|
|
107
|
+
t.assert.strictEqual(result.status, 201)
|
|
108
|
+
const body = await result.text()
|
|
109
|
+
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
|
117
110
|
t.assert.deepStrictEqual(JSON.parse(body), { hello: 55 })
|
|
118
111
|
})
|
|
119
112
|
|
|
120
113
|
await test('shorthand - wrong-object-for-schema', async (t) => {
|
|
121
|
-
const
|
|
122
|
-
t.assert.
|
|
123
|
-
t.assert.strictEqual(
|
|
114
|
+
const result = await fetch(fastifyServer + '/wrong-object-for-schema')
|
|
115
|
+
t.assert.ok(!result.ok)
|
|
116
|
+
t.assert.strictEqual(result.status, 500)
|
|
117
|
+
const body = await result.text()
|
|
118
|
+
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
|
124
119
|
t.assert.deepStrictEqual(JSON.parse(body), {
|
|
125
120
|
statusCode: 500,
|
|
126
121
|
error: 'Internal Server Error',
|
|
@@ -129,14 +124,17 @@ test('start server and run tests', async (t) => {
|
|
|
129
124
|
})
|
|
130
125
|
|
|
131
126
|
await test('shorthand - empty', async (t) => {
|
|
132
|
-
const
|
|
133
|
-
t.assert.
|
|
127
|
+
const result = await fetch(fastifyServer + '/empty')
|
|
128
|
+
t.assert.ok(result.ok)
|
|
129
|
+
t.assert.strictEqual(result.status, 204)
|
|
134
130
|
})
|
|
135
131
|
|
|
136
132
|
await test('shorthand - 400', async (t) => {
|
|
137
|
-
const
|
|
138
|
-
t.assert.
|
|
139
|
-
t.assert.strictEqual(
|
|
133
|
+
const result = await fetch(fastifyServer + '/400')
|
|
134
|
+
t.assert.ok(!result.ok)
|
|
135
|
+
t.assert.strictEqual(result.status, 400)
|
|
136
|
+
const body = await result.text()
|
|
137
|
+
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
|
|
140
138
|
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'DOOM' })
|
|
141
139
|
})
|
|
142
140
|
})
|
package/test/plugin.2.test.js
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const test = t.test
|
|
3
|
+
const { test } = require('node:test')
|
|
5
4
|
const Fastify = require('../fastify')
|
|
6
5
|
const sget = require('simple-get').concat
|
|
7
6
|
const fp = require('fastify-plugin')
|
|
7
|
+
const { waitForCb } = require('./toolkit')
|
|
8
8
|
|
|
9
|
-
test('check dependencies - should not throw', t => {
|
|
9
|
+
test('check dependencies - should not throw', (t, testDone) => {
|
|
10
10
|
t.plan(12)
|
|
11
11
|
const fastify = Fastify()
|
|
12
12
|
|
|
13
13
|
fastify.register((instance, opts, done) => {
|
|
14
14
|
instance.register(fp((i, o, n) => {
|
|
15
15
|
i.decorate('test', () => {})
|
|
16
|
-
t.ok(i.test)
|
|
16
|
+
t.assert.ok(i.test)
|
|
17
17
|
n()
|
|
18
18
|
}))
|
|
19
19
|
|
|
20
20
|
instance.register(fp((i, o, n) => {
|
|
21
21
|
try {
|
|
22
22
|
i.decorate('otherTest', () => {}, ['test'])
|
|
23
|
-
t.ok(i.test)
|
|
24
|
-
t.ok(i.otherTest)
|
|
23
|
+
t.assert.ok(i.test)
|
|
24
|
+
t.assert.ok(i.otherTest)
|
|
25
25
|
n()
|
|
26
26
|
} catch (e) {
|
|
27
|
-
t.fail()
|
|
27
|
+
t.assert.fail()
|
|
28
28
|
}
|
|
29
29
|
}))
|
|
30
30
|
|
|
31
31
|
instance.get('/', (req, reply) => {
|
|
32
|
-
t.ok(instance.test)
|
|
33
|
-
t.ok(instance.otherTest)
|
|
32
|
+
t.assert.ok(instance.test)
|
|
33
|
+
t.assert.ok(instance.otherTest)
|
|
34
34
|
reply.send({ hello: 'world' })
|
|
35
35
|
})
|
|
36
36
|
|
|
@@ -38,27 +38,28 @@ test('check dependencies - should not throw', t => {
|
|
|
38
38
|
})
|
|
39
39
|
|
|
40
40
|
fastify.ready(() => {
|
|
41
|
-
t.
|
|
42
|
-
t.
|
|
41
|
+
t.assert.ok(!fastify.test)
|
|
42
|
+
t.assert.ok(!fastify.otherTest)
|
|
43
43
|
})
|
|
44
44
|
|
|
45
45
|
fastify.listen({ port: 0 }, err => {
|
|
46
|
-
t.
|
|
47
|
-
t.
|
|
46
|
+
t.assert.ifError(err)
|
|
47
|
+
t.after(() => { fastify.close() })
|
|
48
48
|
|
|
49
49
|
sget({
|
|
50
50
|
method: 'GET',
|
|
51
51
|
url: 'http://localhost:' + fastify.server.address().port
|
|
52
52
|
}, (err, response, body) => {
|
|
53
|
-
t.
|
|
54
|
-
t.
|
|
55
|
-
t.
|
|
56
|
-
t.
|
|
53
|
+
t.assert.ifError(err)
|
|
54
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
55
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
56
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
57
|
+
testDone()
|
|
57
58
|
})
|
|
58
59
|
})
|
|
59
60
|
})
|
|
60
61
|
|
|
61
|
-
test('check dependencies - should throw', t => {
|
|
62
|
+
test('check dependencies - should throw', (t, testDone) => {
|
|
62
63
|
t.plan(12)
|
|
63
64
|
const fastify = Fastify()
|
|
64
65
|
|
|
@@ -66,24 +67,24 @@ test('check dependencies - should throw', t => {
|
|
|
66
67
|
instance.register(fp((i, o, n) => {
|
|
67
68
|
try {
|
|
68
69
|
i.decorate('otherTest', () => {}, ['test'])
|
|
69
|
-
t.fail()
|
|
70
|
+
t.assert.fail()
|
|
70
71
|
} catch (e) {
|
|
71
|
-
t.
|
|
72
|
-
t.
|
|
72
|
+
t.assert.strictEqual(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')
|
|
73
|
+
t.assert.strictEqual(e.message, 'The decorator is missing dependency \'test\'.')
|
|
73
74
|
}
|
|
74
75
|
n()
|
|
75
76
|
}))
|
|
76
77
|
|
|
77
78
|
instance.register(fp((i, o, n) => {
|
|
78
79
|
i.decorate('test', () => {})
|
|
79
|
-
t.ok(i.test)
|
|
80
|
-
t.
|
|
80
|
+
t.assert.ok(i.test)
|
|
81
|
+
t.assert.ok(!i.otherTest)
|
|
81
82
|
n()
|
|
82
83
|
}))
|
|
83
84
|
|
|
84
85
|
instance.get('/', (req, reply) => {
|
|
85
|
-
t.ok(instance.test)
|
|
86
|
-
t.
|
|
86
|
+
t.assert.ok(instance.test)
|
|
87
|
+
t.assert.ok(!instance.otherTest)
|
|
87
88
|
reply.send({ hello: 'world' })
|
|
88
89
|
})
|
|
89
90
|
|
|
@@ -91,183 +92,192 @@ test('check dependencies - should throw', t => {
|
|
|
91
92
|
})
|
|
92
93
|
|
|
93
94
|
fastify.ready(() => {
|
|
94
|
-
t.
|
|
95
|
+
t.assert.ok(!fastify.test)
|
|
95
96
|
})
|
|
96
97
|
|
|
97
98
|
fastify.listen({ port: 0 }, err => {
|
|
98
|
-
t.
|
|
99
|
-
t.
|
|
99
|
+
t.assert.ifError(err)
|
|
100
|
+
t.after(() => { fastify.close() })
|
|
100
101
|
|
|
101
102
|
sget({
|
|
102
103
|
method: 'GET',
|
|
103
104
|
url: 'http://localhost:' + fastify.server.address().port
|
|
104
105
|
}, (err, response, body) => {
|
|
105
|
-
t.
|
|
106
|
-
t.
|
|
107
|
-
t.
|
|
108
|
-
t.
|
|
106
|
+
t.assert.ifError(err)
|
|
107
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
108
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
109
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
110
|
+
testDone()
|
|
109
111
|
})
|
|
110
112
|
})
|
|
111
113
|
})
|
|
112
114
|
|
|
113
|
-
test('set the plugin name based on the plugin displayName symbol', t => {
|
|
115
|
+
test('set the plugin name based on the plugin displayName symbol', (t, testDone) => {
|
|
114
116
|
t.plan(6)
|
|
115
117
|
const fastify = Fastify()
|
|
118
|
+
t.after(() => fastify.close())
|
|
116
119
|
|
|
117
120
|
fastify.register(fp((fastify, opts, done) => {
|
|
118
|
-
t.
|
|
121
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A')
|
|
119
122
|
fastify.register(fp((fastify, opts, done) => {
|
|
120
|
-
t.
|
|
123
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB')
|
|
121
124
|
done()
|
|
122
125
|
}, { name: 'plugin-AB' }))
|
|
123
126
|
fastify.register(fp((fastify, opts, done) => {
|
|
124
|
-
t.
|
|
127
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
|
|
125
128
|
done()
|
|
126
129
|
}, { name: 'plugin-AC' }))
|
|
127
130
|
done()
|
|
128
131
|
}, { name: 'plugin-A' }))
|
|
129
132
|
|
|
130
133
|
fastify.register(fp((fastify, opts, done) => {
|
|
131
|
-
t.
|
|
134
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC -> plugin-B')
|
|
132
135
|
done()
|
|
133
136
|
}, { name: 'plugin-B' }))
|
|
134
137
|
|
|
135
|
-
t.
|
|
138
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify')
|
|
136
139
|
|
|
137
140
|
fastify.listen({ port: 0 }, err => {
|
|
138
|
-
t.
|
|
139
|
-
|
|
141
|
+
t.assert.ifError(err)
|
|
142
|
+
testDone()
|
|
140
143
|
})
|
|
141
144
|
})
|
|
142
145
|
|
|
143
|
-
test('plugin name will change when using no encapsulation', t => {
|
|
146
|
+
test('plugin name will change when using no encapsulation', (t, testDone) => {
|
|
144
147
|
t.plan(6)
|
|
145
148
|
const fastify = Fastify()
|
|
149
|
+
t.after(() => fastify.close())
|
|
146
150
|
|
|
147
151
|
fastify.register(fp((fastify, opts, done) => {
|
|
148
152
|
// store it in a different variable will hold the correct name
|
|
149
153
|
const pluginName = fastify.pluginName
|
|
150
154
|
fastify.register(fp((fastify, opts, done) => {
|
|
151
|
-
t.
|
|
155
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB')
|
|
152
156
|
done()
|
|
153
157
|
}, { name: 'plugin-AB' }))
|
|
154
158
|
fastify.register(fp((fastify, opts, done) => {
|
|
155
|
-
t.
|
|
159
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
|
|
156
160
|
done()
|
|
157
161
|
}, { name: 'plugin-AC' }))
|
|
158
162
|
setImmediate(() => {
|
|
159
163
|
// normally we would expect the name plugin-A
|
|
160
164
|
// but we operate on the same instance in each plugin
|
|
161
|
-
t.
|
|
162
|
-
t.
|
|
165
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
|
|
166
|
+
t.assert.strictEqual(pluginName, 'fastify -> plugin-A')
|
|
163
167
|
})
|
|
164
168
|
done()
|
|
165
169
|
}, { name: 'plugin-A' }))
|
|
166
170
|
|
|
167
|
-
t.
|
|
171
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify')
|
|
168
172
|
|
|
169
173
|
fastify.listen({ port: 0 }, err => {
|
|
170
|
-
t.
|
|
171
|
-
|
|
174
|
+
t.assert.ifError(err)
|
|
175
|
+
testDone()
|
|
172
176
|
})
|
|
173
177
|
})
|
|
174
178
|
|
|
175
|
-
test('plugin name is undefined when accessing in no plugin context', t => {
|
|
179
|
+
test('plugin name is undefined when accessing in no plugin context', (t, testDone) => {
|
|
176
180
|
t.plan(2)
|
|
177
181
|
const fastify = Fastify()
|
|
182
|
+
t.after(() => fastify.close())
|
|
178
183
|
|
|
179
|
-
t.
|
|
184
|
+
t.assert.strictEqual(fastify.pluginName, 'fastify')
|
|
180
185
|
|
|
181
186
|
fastify.listen({ port: 0 }, err => {
|
|
182
|
-
t.
|
|
183
|
-
|
|
187
|
+
t.assert.ifError(err)
|
|
188
|
+
testDone()
|
|
184
189
|
})
|
|
185
190
|
})
|
|
186
191
|
|
|
187
|
-
test('set the plugin name based on the plugin function name', t => {
|
|
192
|
+
test('set the plugin name based on the plugin function name', (t, testDone) => {
|
|
188
193
|
t.plan(5)
|
|
189
194
|
const fastify = Fastify()
|
|
195
|
+
t.after(() => fastify.close())
|
|
190
196
|
|
|
191
197
|
fastify.register(function myPluginA (fastify, opts, done) {
|
|
192
|
-
t.
|
|
198
|
+
t.assert.strictEqual(fastify.pluginName, 'myPluginA')
|
|
193
199
|
fastify.register(function myPluginAB (fastify, opts, done) {
|
|
194
|
-
t.
|
|
200
|
+
t.assert.strictEqual(fastify.pluginName, 'myPluginAB')
|
|
195
201
|
done()
|
|
196
202
|
})
|
|
197
203
|
setImmediate(() => {
|
|
198
204
|
// exact name due to encapsulation
|
|
199
|
-
t.
|
|
205
|
+
t.assert.strictEqual(fastify.pluginName, 'myPluginA')
|
|
200
206
|
})
|
|
201
207
|
done()
|
|
202
208
|
})
|
|
203
209
|
|
|
204
210
|
fastify.register(function myPluginB (fastify, opts, done) {
|
|
205
|
-
t.
|
|
211
|
+
t.assert.strictEqual(fastify.pluginName, 'myPluginB')
|
|
206
212
|
done()
|
|
207
213
|
})
|
|
208
214
|
|
|
209
215
|
fastify.listen({ port: 0 }, err => {
|
|
210
|
-
t.
|
|
211
|
-
|
|
216
|
+
t.assert.ifError(err)
|
|
217
|
+
testDone()
|
|
212
218
|
})
|
|
213
219
|
})
|
|
214
220
|
|
|
215
|
-
test('approximate a plugin name when no meta data is available', t => {
|
|
221
|
+
test('approximate a plugin name when no meta data is available', (t, testDone) => {
|
|
216
222
|
t.plan(7)
|
|
217
223
|
const fastify = Fastify()
|
|
224
|
+
t.after(() => fastify.close())
|
|
218
225
|
|
|
219
226
|
fastify.register((fastify, opts, done) => {
|
|
220
227
|
// A
|
|
221
|
-
t.
|
|
222
|
-
t.
|
|
228
|
+
t.assert.strictEqual(fastify.pluginName.startsWith('(fastify, opts, done)'), true)
|
|
229
|
+
t.assert.strictEqual(fastify.pluginName.includes('// A'), true)
|
|
223
230
|
fastify.register((fastify, opts, done) => {
|
|
224
231
|
// B
|
|
225
|
-
t.
|
|
226
|
-
t.
|
|
232
|
+
t.assert.strictEqual(fastify.pluginName.startsWith('(fastify, opts, done)'), true)
|
|
233
|
+
t.assert.strictEqual(fastify.pluginName.includes('// B'), true)
|
|
227
234
|
done()
|
|
228
235
|
})
|
|
229
236
|
setImmediate(() => {
|
|
230
|
-
t.
|
|
231
|
-
t.
|
|
237
|
+
t.assert.strictEqual(fastify.pluginName.startsWith('(fastify, opts, done)'), true)
|
|
238
|
+
t.assert.strictEqual(fastify.pluginName.includes('// A'), true)
|
|
232
239
|
})
|
|
233
240
|
done()
|
|
234
241
|
})
|
|
235
242
|
|
|
236
243
|
fastify.listen({ port: 0 }, err => {
|
|
237
|
-
t.
|
|
238
|
-
|
|
244
|
+
t.assert.ifError(err)
|
|
245
|
+
testDone()
|
|
239
246
|
})
|
|
240
247
|
})
|
|
241
248
|
|
|
242
|
-
test('approximate a plugin name also when fastify-plugin has no meta data', t => {
|
|
249
|
+
test('approximate a plugin name also when fastify-plugin has no meta data', (t, testDone) => {
|
|
243
250
|
t.plan(4)
|
|
244
251
|
const fastify = Fastify()
|
|
252
|
+
t.after(() => fastify.close())
|
|
253
|
+
|
|
245
254
|
// plugin name is got from current file name
|
|
246
255
|
const pluginName = /plugin\.2\.test/
|
|
247
256
|
const pluginNameWithFunction = /plugin\.2\.test-auto-\d+ -> B/
|
|
248
257
|
|
|
249
258
|
fastify.register(fp((fastify, opts, done) => {
|
|
250
|
-
t.match(fastify.pluginName, pluginName)
|
|
259
|
+
t.assert.match(fastify.pluginName, pluginName)
|
|
251
260
|
fastify.register(fp(function B (fastify, opts, done) {
|
|
252
261
|
// function has name
|
|
253
|
-
t.match(fastify.pluginName, pluginNameWithFunction)
|
|
262
|
+
t.assert.match(fastify.pluginName, pluginNameWithFunction)
|
|
254
263
|
done()
|
|
255
264
|
}))
|
|
256
265
|
setImmediate(() => {
|
|
257
|
-
t.match(fastify.pluginName, pluginNameWithFunction)
|
|
266
|
+
t.assert.match(fastify.pluginName, pluginNameWithFunction)
|
|
258
267
|
})
|
|
259
268
|
done()
|
|
260
269
|
}))
|
|
261
270
|
|
|
262
271
|
fastify.listen({ port: 0 }, err => {
|
|
263
|
-
t.
|
|
264
|
-
|
|
272
|
+
t.assert.ifError(err)
|
|
273
|
+
testDone()
|
|
265
274
|
})
|
|
266
275
|
})
|
|
267
276
|
|
|
268
|
-
test('plugin encapsulation', t => {
|
|
277
|
+
test('plugin encapsulation', (t, testDone) => {
|
|
269
278
|
t.plan(10)
|
|
270
279
|
const fastify = Fastify()
|
|
280
|
+
t.after(() => fastify.close())
|
|
271
281
|
|
|
272
282
|
fastify.register((instance, opts, done) => {
|
|
273
283
|
instance.register(fp((i, o, n) => {
|
|
@@ -296,31 +306,39 @@ test('plugin encapsulation', t => {
|
|
|
296
306
|
})
|
|
297
307
|
|
|
298
308
|
fastify.ready(() => {
|
|
299
|
-
t.
|
|
309
|
+
t.assert.ok(!fastify.test)
|
|
300
310
|
})
|
|
301
311
|
|
|
302
312
|
fastify.listen({ port: 0 }, err => {
|
|
303
|
-
t.
|
|
304
|
-
t.
|
|
313
|
+
t.assert.ifError(err)
|
|
314
|
+
t.after(() => { fastify.close() })
|
|
315
|
+
|
|
316
|
+
const completion = waitForCb({
|
|
317
|
+
steps: 2
|
|
318
|
+
})
|
|
305
319
|
|
|
306
320
|
sget({
|
|
307
321
|
method: 'GET',
|
|
308
322
|
url: 'http://localhost:' + fastify.server.address().port + '/first'
|
|
309
323
|
}, (err, response, body) => {
|
|
310
|
-
t.
|
|
311
|
-
t.
|
|
312
|
-
t.
|
|
313
|
-
t.
|
|
324
|
+
t.assert.ifError(err)
|
|
325
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
326
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
327
|
+
t.assert.deepStrictEqual(JSON.parse(body), { plugin: 'first' })
|
|
328
|
+
completion.stepIn()
|
|
314
329
|
})
|
|
315
330
|
|
|
316
331
|
sget({
|
|
317
332
|
method: 'GET',
|
|
318
333
|
url: 'http://localhost:' + fastify.server.address().port + '/second'
|
|
319
334
|
}, (err, response, body) => {
|
|
320
|
-
t.
|
|
321
|
-
t.
|
|
322
|
-
t.
|
|
323
|
-
t.
|
|
335
|
+
t.assert.ifError(err)
|
|
336
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
337
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
338
|
+
t.assert.deepStrictEqual(JSON.parse(body), { plugin: 'second' })
|
|
339
|
+
completion.stepIn()
|
|
324
340
|
})
|
|
341
|
+
|
|
342
|
+
completion.patience.then(testDone)
|
|
325
343
|
})
|
|
326
344
|
})
|