fastify 5.3.3 → 5.5.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/.vscode/settings.json +15 -15
- package/LICENSE +1 -1
- package/README.md +2 -0
- package/SECURITY.md +158 -2
- package/build/build-validation.js +20 -1
- package/docs/Guides/Delay-Accepting-Requests.md +8 -5
- package/docs/Guides/Ecosystem.md +20 -5
- package/docs/Guides/Migration-Guide-V5.md +6 -10
- package/docs/Guides/Recommendations.md +1 -1
- package/docs/Reference/ContentTypeParser.md +1 -1
- package/docs/Reference/Errors.md +5 -3
- package/docs/Reference/Hooks.md +16 -20
- package/docs/Reference/Lifecycle.md +2 -2
- 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 +2 -2
- package/docs/Reference/Routes.md +7 -6
- package/docs/Reference/Server.md +341 -200
- package/docs/Reference/TypeScript.md +1 -3
- package/docs/Reference/Validation-and-Serialization.md +56 -4
- package/docs/Reference/Warnings.md +2 -1
- package/fastify.d.ts +4 -3
- package/fastify.js +47 -34
- package/lib/configValidator.js +196 -28
- package/lib/contentTypeParser.js +41 -48
- package/lib/error-handler.js +3 -3
- package/lib/errors.js +11 -0
- package/lib/handleRequest.js +13 -17
- package/lib/pluginOverride.js +3 -1
- package/lib/promise.js +23 -0
- package/lib/reply.js +24 -30
- package/lib/request.js +3 -10
- package/lib/route.js +37 -3
- package/lib/server.js +36 -35
- package/lib/symbols.js +1 -0
- package/lib/warnings.js +19 -1
- package/package.json +14 -10
- 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 +84 -128
- package/test/async_hooks.test.js +18 -37
- package/test/body-limit.test.js +90 -63
- package/test/buffer.test.js +22 -0
- package/test/build-certificate.js +1 -1
- package/test/case-insensitive.test.js +44 -65
- package/test/check.test.js +17 -21
- package/test/close-pipelining.test.js +24 -15
- package/test/constrained-routes.test.js +231 -0
- package/test/custom-http-server.test.js +7 -15
- package/test/custom-parser-async.test.js +17 -22
- package/test/custom-parser.0.test.js +267 -348
- package/test/custom-parser.1.test.js +141 -191
- package/test/custom-parser.2.test.js +34 -44
- package/test/custom-parser.3.test.js +56 -104
- package/test/custom-parser.4.test.js +106 -144
- package/test/custom-parser.5.test.js +56 -75
- package/test/custom-querystring-parser.test.js +51 -77
- package/test/decorator-namespace.test._js_ +3 -4
- package/test/decorator.test.js +76 -259
- package/test/delete.test.js +101 -110
- package/test/diagnostics-channel/404.test.js +7 -15
- package/test/diagnostics-channel/async-delay-request.test.js +7 -16
- package/test/diagnostics-channel/async-request.test.js +8 -16
- package/test/diagnostics-channel/error-request.test.js +7 -15
- package/test/diagnostics-channel/sync-delay-request.test.js +7 -16
- package/test/diagnostics-channel/sync-request-reply.test.js +9 -16
- package/test/diagnostics-channel/sync-request.test.js +9 -16
- package/test/fastify-instance.test.js +1 -1
- package/test/header-overflow.test.js +18 -29
- package/test/helper.js +139 -135
- package/test/hooks-async.test.js +259 -235
- package/test/hooks.test.js +951 -996
- package/test/http-methods/copy.test.js +14 -19
- package/test/http-methods/get.test.js +131 -143
- package/test/http-methods/head.test.js +53 -84
- package/test/http-methods/lock.test.js +31 -31
- package/test/http-methods/mkcalendar.test.js +45 -72
- package/test/http-methods/mkcol.test.js +5 -9
- package/test/http-methods/move.test.js +6 -10
- package/test/http-methods/propfind.test.js +34 -44
- 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/http-methods/unlock.test.js +5 -9
- package/test/http2/closing.test.js +38 -20
- package/test/http2/secure-with-fallback.test.js +31 -28
- package/test/https/custom-https-server.test.js +9 -13
- package/test/https/https.test.js +56 -53
- package/test/input-validation.js +139 -150
- package/test/internals/errors.test.js +50 -1
- package/test/internals/handle-request.test.js +72 -65
- package/test/internals/promise.test.js +63 -0
- package/test/internals/reply.test.js +277 -496
- 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.1.test.js +40 -68
- package/test/plugin.2.test.js +108 -120
- package/test/plugin.3.test.js +50 -72
- package/test/plugin.4.test.js +124 -119
- package/test/promises.test.js +42 -63
- package/test/proto-poisoning.test.js +78 -97
- package/test/register.test.js +8 -18
- package/test/request-error.test.js +57 -146
- package/test/request-id.test.js +30 -49
- package/test/route-hooks.test.js +117 -101
- package/test/route-prefix.test.js +194 -133
- package/test/route-shorthand.test.js +9 -27
- package/test/route.1.test.js +74 -131
- package/test/route.8.test.js +9 -17
- package/test/router-options.test.js +450 -0
- package/test/schema-serialization.test.js +177 -154
- package/test/schema-special-usage.test.js +165 -132
- package/test/schema-validation.test.js +254 -218
- package/test/server.test.js +143 -5
- package/test/set-error-handler.test.js +58 -1
- package/test/skip-reply-send.test.js +64 -69
- package/test/stream.1.test.js +33 -50
- package/test/stream.4.test.js +18 -28
- package/test/stream.5.test.js +11 -19
- package/test/trust-proxy.test.js +32 -58
- package/test/types/errors.test-d.ts +13 -1
- package/test/types/fastify.test-d.ts +3 -0
- package/test/types/request.test-d.ts +1 -0
- package/test/types/type-provider.test-d.ts +55 -0
- package/test/url-rewriting.test.js +45 -62
- package/test/use-semicolon-delimiter.test.js +117 -59
- package/test/versioned-routes.test.js +39 -56
- package/types/errors.d.ts +11 -1
- package/types/hooks.d.ts +1 -1
- package/types/instance.d.ts +1 -1
- package/types/reply.d.ts +2 -2
- package/types/request.d.ts +1 -0
- package/.taprc +0 -7
package/test/server.test.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
const dns = require('node:dns')
|
|
3
4
|
const { test } = require('node:test')
|
|
4
5
|
const Fastify = require('..')
|
|
5
|
-
const sget = require('simple-get').concat
|
|
6
6
|
const undici = require('undici')
|
|
7
|
+
const proxyquire = require('proxyquire')
|
|
7
8
|
|
|
8
9
|
test('listen should accept null port', async t => {
|
|
9
10
|
const fastify = Fastify()
|
|
@@ -65,7 +66,7 @@ test('listen should reject string port', async (t) => {
|
|
|
65
66
|
}
|
|
66
67
|
})
|
|
67
68
|
|
|
68
|
-
test('Test for hostname and port', (t
|
|
69
|
+
test('Test for hostname and port', async (t) => {
|
|
69
70
|
const app = Fastify()
|
|
70
71
|
t.after(() => app.close())
|
|
71
72
|
app.get('/host', (req, res) => {
|
|
@@ -76,12 +77,149 @@ test('Test for hostname and port', (t, end) => {
|
|
|
76
77
|
res.send('ok')
|
|
77
78
|
})
|
|
78
79
|
|
|
79
|
-
app.listen({ port: 8000 }
|
|
80
|
-
|
|
81
|
-
})
|
|
80
|
+
await app.listen({ port: 8000 })
|
|
81
|
+
await fetch('http://localhost:8000/host')
|
|
82
82
|
})
|
|
83
83
|
|
|
84
84
|
test('abort signal', async t => {
|
|
85
|
+
await t.test('should close server when aborted after', (t, end) => {
|
|
86
|
+
t.plan(2)
|
|
87
|
+
function onClose (instance, done) {
|
|
88
|
+
t.assert.strictEqual(instance, fastify)
|
|
89
|
+
done()
|
|
90
|
+
end()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const controller = new AbortController()
|
|
94
|
+
|
|
95
|
+
const fastify = Fastify()
|
|
96
|
+
fastify.addHook('onClose', onClose)
|
|
97
|
+
fastify.listen({ port: 1234, signal: controller.signal }, (err) => {
|
|
98
|
+
t.assert.ifError(err)
|
|
99
|
+
controller.abort()
|
|
100
|
+
})
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
await t.test('should close server when aborted after - promise', async (t) => {
|
|
104
|
+
t.plan(2)
|
|
105
|
+
const resolver = {}
|
|
106
|
+
resolver.promise = new Promise(function (resolve) {
|
|
107
|
+
resolver.resolve = resolve
|
|
108
|
+
})
|
|
109
|
+
function onClose (instance, done) {
|
|
110
|
+
t.assert.strictEqual(instance, fastify)
|
|
111
|
+
done()
|
|
112
|
+
resolver.resolve()
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const controller = new AbortController()
|
|
116
|
+
|
|
117
|
+
const fastify = Fastify()
|
|
118
|
+
fastify.addHook('onClose', onClose)
|
|
119
|
+
const address = await fastify.listen({ port: 1234, signal: controller.signal })
|
|
120
|
+
t.assert.ok(address)
|
|
121
|
+
controller.abort()
|
|
122
|
+
await resolver.promise
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
await t.test('should close server when aborted during fastify.ready - promise', async (t) => {
|
|
126
|
+
t.plan(2)
|
|
127
|
+
const resolver = {}
|
|
128
|
+
resolver.promise = new Promise(function (resolve) {
|
|
129
|
+
resolver.resolve = resolve
|
|
130
|
+
})
|
|
131
|
+
function onClose (instance, done) {
|
|
132
|
+
t.assert.strictEqual(instance, fastify)
|
|
133
|
+
done()
|
|
134
|
+
resolver.resolve()
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const controller = new AbortController()
|
|
138
|
+
|
|
139
|
+
const fastify = Fastify()
|
|
140
|
+
fastify.addHook('onClose', onClose)
|
|
141
|
+
const promise = fastify.listen({ port: 1234, signal: controller.signal })
|
|
142
|
+
controller.abort()
|
|
143
|
+
const address = await promise
|
|
144
|
+
// since the main server is not listening yet, or will not listen
|
|
145
|
+
// it should return undefined
|
|
146
|
+
t.assert.strictEqual(address, undefined)
|
|
147
|
+
await resolver.promise
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
await t.test('should close server when aborted during dns.lookup - promise', async (t) => {
|
|
151
|
+
t.plan(2)
|
|
152
|
+
const Fastify = proxyquire('..', {
|
|
153
|
+
'./lib/server.js': proxyquire('../lib/server.js', {
|
|
154
|
+
'node:dns': {
|
|
155
|
+
lookup: function (host, option, callback) {
|
|
156
|
+
controller.abort()
|
|
157
|
+
dns.lookup(host, option, callback)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
})
|
|
161
|
+
})
|
|
162
|
+
const resolver = {}
|
|
163
|
+
resolver.promise = new Promise(function (resolve) {
|
|
164
|
+
resolver.resolve = resolve
|
|
165
|
+
})
|
|
166
|
+
function onClose (instance, done) {
|
|
167
|
+
t.assert.strictEqual(instance, fastify)
|
|
168
|
+
done()
|
|
169
|
+
resolver.resolve()
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const controller = new AbortController()
|
|
173
|
+
|
|
174
|
+
const fastify = Fastify()
|
|
175
|
+
fastify.addHook('onClose', onClose)
|
|
176
|
+
const address = await fastify.listen({ port: 1234, signal: controller.signal })
|
|
177
|
+
// since the main server is already listening then close
|
|
178
|
+
// it should return address
|
|
179
|
+
t.assert.ok(address)
|
|
180
|
+
await resolver.promise
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
await t.test('should close server when aborted before', (t, end) => {
|
|
184
|
+
t.plan(1)
|
|
185
|
+
function onClose (instance, done) {
|
|
186
|
+
t.assert.strictEqual(instance, fastify)
|
|
187
|
+
done()
|
|
188
|
+
end()
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const controller = new AbortController()
|
|
192
|
+
controller.abort()
|
|
193
|
+
|
|
194
|
+
const fastify = Fastify()
|
|
195
|
+
fastify.addHook('onClose', onClose)
|
|
196
|
+
fastify.listen({ port: 1234, signal: controller.signal }, () => {
|
|
197
|
+
t.assert.fail('should not reach callback')
|
|
198
|
+
})
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
await t.test('should close server when aborted before - promise', async (t) => {
|
|
202
|
+
t.plan(2)
|
|
203
|
+
const resolver = {}
|
|
204
|
+
resolver.promise = new Promise(function (resolve) {
|
|
205
|
+
resolver.resolve = resolve
|
|
206
|
+
})
|
|
207
|
+
function onClose (instance, done) {
|
|
208
|
+
t.assert.strictEqual(instance, fastify)
|
|
209
|
+
done()
|
|
210
|
+
resolver.resolve()
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const controller = new AbortController()
|
|
214
|
+
controller.abort()
|
|
215
|
+
|
|
216
|
+
const fastify = Fastify()
|
|
217
|
+
fastify.addHook('onClose', onClose)
|
|
218
|
+
const address = await fastify.listen({ port: 1234, signal: controller.signal })
|
|
219
|
+
t.assert.strictEqual(address, undefined) // ensure the API signature
|
|
220
|
+
await resolver.promise
|
|
221
|
+
})
|
|
222
|
+
|
|
85
223
|
await t.test('listen should not start server', (t, end) => {
|
|
86
224
|
t.plan(2)
|
|
87
225
|
function onClose (instance, done) {
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { test } = require('node:test')
|
|
4
4
|
const Fastify = require('..')
|
|
5
|
-
const { FST_ERR_ERROR_HANDLER_NOT_FN } = require('../lib/errors')
|
|
5
|
+
const { FST_ERR_ERROR_HANDLER_NOT_FN, FST_ERR_ERROR_HANDLER_ALREADY_SET } = require('../lib/errors')
|
|
6
6
|
|
|
7
7
|
test('setErrorHandler should throw an error if the handler is not a function', t => {
|
|
8
8
|
t.plan(1)
|
|
@@ -10,3 +10,60 @@ test('setErrorHandler should throw an error if the handler is not a function', t
|
|
|
10
10
|
|
|
11
11
|
t.assert.throws(() => fastify.setErrorHandler('not a function'), new FST_ERR_ERROR_HANDLER_NOT_FN())
|
|
12
12
|
})
|
|
13
|
+
|
|
14
|
+
test('setErrorHandler can be set independently in parent and child scopes', async t => {
|
|
15
|
+
t.plan(1)
|
|
16
|
+
|
|
17
|
+
const fastify = Fastify()
|
|
18
|
+
|
|
19
|
+
t.assert.doesNotThrow(() => {
|
|
20
|
+
fastify.setErrorHandler(() => {})
|
|
21
|
+
fastify.register(async (child) => {
|
|
22
|
+
child.setErrorHandler(() => {})
|
|
23
|
+
})
|
|
24
|
+
})
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('setErrorHandler can be overriden if allowErrorHandlerOverride is set to true', async t => {
|
|
28
|
+
t.plan(2)
|
|
29
|
+
|
|
30
|
+
const fastify = Fastify()
|
|
31
|
+
t.after(() => fastify.close())
|
|
32
|
+
|
|
33
|
+
fastify.register(async (child) => {
|
|
34
|
+
child.setErrorHandler(() => {})
|
|
35
|
+
t.assert.doesNotThrow(() => child.setErrorHandler(() => {}))
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
fastify.setErrorHandler(() => {})
|
|
39
|
+
t.assert.doesNotThrow(() => fastify.setErrorHandler(() => {}))
|
|
40
|
+
|
|
41
|
+
await fastify.ready()
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('if `allowErrorHandlerOverride` is disabled, setErrorHandler should throw if called more than once in the same scope', t => {
|
|
45
|
+
t.plan(1)
|
|
46
|
+
|
|
47
|
+
const fastify = Fastify({
|
|
48
|
+
allowErrorHandlerOverride: false
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
fastify.setErrorHandler(() => {})
|
|
52
|
+
t.assert.throws(() => fastify.setErrorHandler(() => {}), new FST_ERR_ERROR_HANDLER_ALREADY_SET())
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('if `allowErrorHandlerOverride` is disabled, setErrorHandler should throw if called more than once in the same scope 2', async t => {
|
|
56
|
+
t.plan(1)
|
|
57
|
+
|
|
58
|
+
const fastify = Fastify({
|
|
59
|
+
allowErrorHandlerOverride: false
|
|
60
|
+
})
|
|
61
|
+
t.after(() => fastify.close())
|
|
62
|
+
|
|
63
|
+
fastify.register(async (child) => {
|
|
64
|
+
child.setErrorHandler(() => {})
|
|
65
|
+
t.assert.throws(() => child.setErrorHandler(() => {}), new FST_ERR_ERROR_HANDLER_ALREADY_SET())
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
await fastify.ready()
|
|
69
|
+
})
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { test } = require('
|
|
3
|
+
const { test, describe } = require('node:test')
|
|
4
4
|
const split = require('split2')
|
|
5
5
|
const net = require('node:net')
|
|
6
6
|
const Fastify = require('../fastify')
|
|
@@ -19,7 +19,7 @@ const lifecycleHooks = [
|
|
|
19
19
|
'onError'
|
|
20
20
|
]
|
|
21
21
|
|
|
22
|
-
test('skip automatic reply.send() with reply.hijack and a body', (t) => {
|
|
22
|
+
test('skip automatic reply.send() with reply.hijack and a body', async (t) => {
|
|
23
23
|
const stream = split(JSON.parse)
|
|
24
24
|
const app = Fastify({
|
|
25
25
|
logger: {
|
|
@@ -28,8 +28,8 @@ test('skip automatic reply.send() with reply.hijack and a body', (t) => {
|
|
|
28
28
|
})
|
|
29
29
|
|
|
30
30
|
stream.on('data', (line) => {
|
|
31
|
-
t.
|
|
32
|
-
t.
|
|
31
|
+
t.assert.notStrictEqual(line.level, 40) // there are no errors
|
|
32
|
+
t.assert.notStrictEqual(line.level, 50) // there are no errors
|
|
33
33
|
})
|
|
34
34
|
|
|
35
35
|
app.get('/', (req, reply) => {
|
|
@@ -39,16 +39,16 @@ test('skip automatic reply.send() with reply.hijack and a body', (t) => {
|
|
|
39
39
|
return Promise.resolve('this will be skipped')
|
|
40
40
|
})
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
await app.inject({
|
|
43
43
|
method: 'GET',
|
|
44
44
|
url: '/'
|
|
45
45
|
}).then((res) => {
|
|
46
|
-
t.
|
|
47
|
-
t.
|
|
46
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
47
|
+
t.assert.strictEqual(res.body, 'hello world')
|
|
48
48
|
})
|
|
49
49
|
})
|
|
50
50
|
|
|
51
|
-
test('skip automatic reply.send() with reply.hijack and no body', (t) => {
|
|
51
|
+
test('skip automatic reply.send() with reply.hijack and no body', async (t) => {
|
|
52
52
|
const stream = split(JSON.parse)
|
|
53
53
|
const app = Fastify({
|
|
54
54
|
logger: {
|
|
@@ -57,8 +57,8 @@ test('skip automatic reply.send() with reply.hijack and no body', (t) => {
|
|
|
57
57
|
})
|
|
58
58
|
|
|
59
59
|
stream.on('data', (line) => {
|
|
60
|
-
t.
|
|
61
|
-
t.
|
|
60
|
+
t.assert.notStrictEqual(line.level, 40) // there are no error
|
|
61
|
+
t.assert.notStrictEqual(line.level, 50) // there are no error
|
|
62
62
|
})
|
|
63
63
|
|
|
64
64
|
app.get('/', (req, reply) => {
|
|
@@ -68,16 +68,16 @@ test('skip automatic reply.send() with reply.hijack and no body', (t) => {
|
|
|
68
68
|
return Promise.resolve()
|
|
69
69
|
})
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
await app.inject({
|
|
72
72
|
method: 'GET',
|
|
73
73
|
url: '/'
|
|
74
74
|
}).then((res) => {
|
|
75
|
-
t.
|
|
76
|
-
t.
|
|
75
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
76
|
+
t.assert.strictEqual(res.body, 'hello world')
|
|
77
77
|
})
|
|
78
78
|
})
|
|
79
79
|
|
|
80
|
-
test('skip automatic reply.send() with reply.hijack and an error', (t) => {
|
|
80
|
+
test('skip automatic reply.send() with reply.hijack and an error', async (t) => {
|
|
81
81
|
const stream = split(JSON.parse)
|
|
82
82
|
const app = Fastify({
|
|
83
83
|
logger: {
|
|
@@ -90,8 +90,8 @@ test('skip automatic reply.send() with reply.hijack and an error', (t) => {
|
|
|
90
90
|
stream.on('data', (line) => {
|
|
91
91
|
if (line.level === 50) {
|
|
92
92
|
errorSeen = true
|
|
93
|
-
t.
|
|
94
|
-
t.
|
|
93
|
+
t.assert.strictEqual(line.err.message, 'kaboom')
|
|
94
|
+
t.assert.strictEqual(line.msg, 'Promise errored, but reply.sent = true was set')
|
|
95
95
|
}
|
|
96
96
|
})
|
|
97
97
|
|
|
@@ -102,13 +102,13 @@ test('skip automatic reply.send() with reply.hijack and an error', (t) => {
|
|
|
102
102
|
return Promise.reject(new Error('kaboom'))
|
|
103
103
|
})
|
|
104
104
|
|
|
105
|
-
|
|
105
|
+
await app.inject({
|
|
106
106
|
method: 'GET',
|
|
107
107
|
url: '/'
|
|
108
108
|
}).then((res) => {
|
|
109
|
-
t.
|
|
110
|
-
t.
|
|
111
|
-
t.
|
|
109
|
+
t.assert.strictEqual(errorSeen, true)
|
|
110
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
111
|
+
t.assert.strictEqual(res.body, 'hello world')
|
|
112
112
|
})
|
|
113
113
|
})
|
|
114
114
|
|
|
@@ -117,11 +117,8 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
|
|
117
117
|
const previousHooks = lifecycleHooks.slice(0, idx)
|
|
118
118
|
const nextHooks = lifecycleHooks.slice(idx + 1)
|
|
119
119
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const test = t.test
|
|
123
|
-
|
|
124
|
-
test('Sending a response using reply.raw => onResponse hook is called', t => {
|
|
120
|
+
describe(`Hijacking inside ${hookOrHandler} skips all the following hooks and handler execution`, () => {
|
|
121
|
+
test('Sending a response using reply.raw => onResponse hook is called', async (t) => {
|
|
125
122
|
const stream = split(JSON.parse)
|
|
126
123
|
const app = Fastify({
|
|
127
124
|
logger: {
|
|
@@ -130,11 +127,11 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
|
|
130
127
|
})
|
|
131
128
|
|
|
132
129
|
stream.on('data', (line) => {
|
|
133
|
-
t.
|
|
134
|
-
t.
|
|
130
|
+
t.assert.notStrictEqual(line.level, 40) // there are no errors
|
|
131
|
+
t.assert.notStrictEqual(line.level, 50) // there are no errors
|
|
135
132
|
})
|
|
136
133
|
|
|
137
|
-
previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.
|
|
134
|
+
previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.assert.ok(`${h} should be called`)))
|
|
138
135
|
|
|
139
136
|
if (hookOrHandler === 'handler') {
|
|
140
137
|
app.get('/', (req, reply) => {
|
|
@@ -146,41 +143,41 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
|
|
146
143
|
reply.hijack()
|
|
147
144
|
reply.raw.end(`hello from ${hookOrHandler}`)
|
|
148
145
|
})
|
|
149
|
-
app.get('/', (req, reply) => t.fail('Handler should not be called'))
|
|
146
|
+
app.get('/', (req, reply) => t.assert.fail('Handler should not be called'))
|
|
150
147
|
}
|
|
151
148
|
|
|
152
149
|
nextHooks.forEach(h => {
|
|
153
150
|
if (h === 'onResponse') {
|
|
154
|
-
app.addHook(h, async (req, reply) => t.
|
|
151
|
+
app.addHook(h, async (req, reply) => t.assert.ok(`${h} should be called`))
|
|
155
152
|
} else {
|
|
156
|
-
app.addHook(h, async (req, reply) => t.fail(`${h} should not be called`))
|
|
153
|
+
app.addHook(h, async (req, reply) => t.assert.fail(`${h} should not be called`))
|
|
157
154
|
}
|
|
158
155
|
})
|
|
159
156
|
|
|
160
|
-
|
|
157
|
+
await app.inject({
|
|
161
158
|
method: 'GET',
|
|
162
159
|
url: '/'
|
|
163
160
|
}).then((res) => {
|
|
164
|
-
t.
|
|
165
|
-
t.
|
|
161
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
162
|
+
t.assert.strictEqual(res.body, `hello from ${hookOrHandler}`)
|
|
166
163
|
})
|
|
167
164
|
})
|
|
168
165
|
|
|
169
|
-
test('Sending a response using req.socket => onResponse not called', t => {
|
|
166
|
+
test('Sending a response using req.socket => onResponse not called', (t, testDone) => {
|
|
170
167
|
const stream = split(JSON.parse)
|
|
171
168
|
const app = Fastify({
|
|
172
169
|
logger: {
|
|
173
170
|
stream
|
|
174
171
|
}
|
|
175
172
|
})
|
|
176
|
-
t.
|
|
173
|
+
t.after(() => app.close())
|
|
177
174
|
|
|
178
175
|
stream.on('data', (line) => {
|
|
179
|
-
t.
|
|
180
|
-
t.
|
|
176
|
+
t.assert.notStrictEqual(line.level, 40) // there are no errors
|
|
177
|
+
t.assert.notStrictEqual(line.level, 50) // there are no errors
|
|
181
178
|
})
|
|
182
179
|
|
|
183
|
-
previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.
|
|
180
|
+
previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.assert.ok(`${h} should be called`)))
|
|
184
181
|
|
|
185
182
|
if (hookOrHandler === 'handler') {
|
|
186
183
|
app.get('/', (req, reply) => {
|
|
@@ -196,13 +193,13 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
|
|
196
193
|
req.socket.write(`hello from ${hookOrHandler}`)
|
|
197
194
|
req.socket.end()
|
|
198
195
|
})
|
|
199
|
-
app.get('/', (req, reply) => t.fail('Handler should not be called'))
|
|
196
|
+
app.get('/', (req, reply) => t.assert.fail('Handler should not be called'))
|
|
200
197
|
}
|
|
201
198
|
|
|
202
|
-
nextHooks.forEach(h => app.addHook(h, async (req, reply) => t.fail(`${h} should not be called`)))
|
|
199
|
+
nextHooks.forEach(h => app.addHook(h, async (req, reply) => t.assert.fail(`${h} should not be called`)))
|
|
203
200
|
|
|
204
201
|
app.listen({ port: 0 }, err => {
|
|
205
|
-
t.
|
|
202
|
+
t.assert.ifError(err)
|
|
206
203
|
const client = net.createConnection({ port: (app.server.address()).port }, () => {
|
|
207
204
|
client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
|
|
208
205
|
|
|
@@ -213,36 +210,36 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
|
|
213
210
|
})
|
|
214
211
|
|
|
215
212
|
client.on('end', function () {
|
|
216
|
-
t.match(chunks, new RegExp(`hello from ${hookOrHandler}`, 'i'))
|
|
217
|
-
|
|
213
|
+
t.assert.match(chunks, new RegExp(`hello from ${hookOrHandler}`, 'i'))
|
|
214
|
+
testDone()
|
|
218
215
|
})
|
|
219
216
|
})
|
|
220
217
|
})
|
|
221
218
|
})
|
|
222
219
|
|
|
223
|
-
test('Throwing an error does not trigger any hooks', t => {
|
|
220
|
+
test('Throwing an error does not trigger any hooks', async (t) => {
|
|
224
221
|
const stream = split(JSON.parse)
|
|
225
222
|
const app = Fastify({
|
|
226
223
|
logger: {
|
|
227
224
|
stream
|
|
228
225
|
}
|
|
229
226
|
})
|
|
230
|
-
t.
|
|
227
|
+
t.after(() => app.close())
|
|
231
228
|
|
|
232
229
|
let errorSeen = false
|
|
233
230
|
stream.on('data', (line) => {
|
|
234
231
|
if (hookOrHandler === 'handler') {
|
|
235
232
|
if (line.level === 40) {
|
|
236
233
|
errorSeen = true
|
|
237
|
-
t.
|
|
234
|
+
t.assert.strictEqual(line.err.code, 'FST_ERR_REP_ALREADY_SENT')
|
|
238
235
|
}
|
|
239
236
|
} else {
|
|
240
|
-
t.
|
|
241
|
-
t.
|
|
237
|
+
t.assert.notStrictEqual(line.level, 40) // there are no errors
|
|
238
|
+
t.assert.notStrictEqual(line.level, 50) // there are no errors
|
|
242
239
|
}
|
|
243
240
|
})
|
|
244
241
|
|
|
245
|
-
previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.
|
|
242
|
+
previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.assert.ok(`${h} should be called`)))
|
|
246
243
|
|
|
247
244
|
if (hookOrHandler === 'handler') {
|
|
248
245
|
app.get('/', (req, reply) => {
|
|
@@ -254,23 +251,22 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
|
|
254
251
|
reply.hijack()
|
|
255
252
|
throw new Error('This wil be skipped')
|
|
256
253
|
})
|
|
257
|
-
app.get('/', (req, reply) => t.fail('Handler should not be called'))
|
|
254
|
+
app.get('/', (req, reply) => t.assert.fail('Handler should not be called'))
|
|
258
255
|
}
|
|
259
256
|
|
|
260
|
-
nextHooks.forEach(h => app.addHook(h, async (req, reply) => t.fail(`${h} should not be called`)))
|
|
257
|
+
nextHooks.forEach(h => app.addHook(h, async (req, reply) => t.assert.fail(`${h} should not be called`)))
|
|
261
258
|
|
|
262
|
-
|
|
259
|
+
await Promise.race([
|
|
263
260
|
app.inject({ method: 'GET', url: '/' }),
|
|
264
261
|
new Promise((resolve, reject) => setTimeout(resolve, 1000))
|
|
265
|
-
])
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
})
|
|
262
|
+
])
|
|
263
|
+
|
|
264
|
+
if (hookOrHandler === 'handler') {
|
|
265
|
+
t.assert.strictEqual(errorSeen, true)
|
|
266
|
+
}
|
|
271
267
|
})
|
|
272
268
|
|
|
273
|
-
test('Calling reply.send() after hijacking logs a warning', t => {
|
|
269
|
+
test('Calling reply.send() after hijacking logs a warning', async (t) => {
|
|
274
270
|
const stream = split(JSON.parse)
|
|
275
271
|
const app = Fastify({
|
|
276
272
|
logger: {
|
|
@@ -283,11 +279,11 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
|
|
283
279
|
stream.on('data', (line) => {
|
|
284
280
|
if (line.level === 40) {
|
|
285
281
|
errorSeen = true
|
|
286
|
-
t.
|
|
282
|
+
t.assert.strictEqual(line.err.code, 'FST_ERR_REP_ALREADY_SENT')
|
|
287
283
|
}
|
|
288
284
|
})
|
|
289
285
|
|
|
290
|
-
previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.
|
|
286
|
+
previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.assert.ok(`${h} should be called`)))
|
|
291
287
|
|
|
292
288
|
if (hookOrHandler === 'handler') {
|
|
293
289
|
app.get('/', (req, reply) => {
|
|
@@ -299,18 +295,17 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
|
|
299
295
|
reply.hijack()
|
|
300
296
|
return reply.send('hello from reply.send()')
|
|
301
297
|
})
|
|
302
|
-
app.get('/', (req, reply) => t.fail('Handler should not be called'))
|
|
298
|
+
app.get('/', (req, reply) => t.assert.fail('Handler should not be called'))
|
|
303
299
|
}
|
|
304
300
|
|
|
305
|
-
nextHooks.forEach(h => app.addHook(h, async (req, reply) => t.fail(`${h} should not be called`)))
|
|
301
|
+
nextHooks.forEach(h => app.addHook(h, async (req, reply) => t.assert.fail(`${h} should not be called`)))
|
|
306
302
|
|
|
307
|
-
|
|
303
|
+
await Promise.race([
|
|
308
304
|
app.inject({ method: 'GET', url: '/' }),
|
|
309
305
|
new Promise((resolve, reject) => setTimeout(resolve, 1000))
|
|
310
|
-
])
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
})
|
|
306
|
+
])
|
|
307
|
+
|
|
308
|
+
t.assert.strictEqual(errorSeen, true)
|
|
314
309
|
})
|
|
315
310
|
})
|
|
316
311
|
}
|