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
|
@@ -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
|
}
|
package/test/trust-proxy.test.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { test, before } = require('node:test')
|
|
4
|
-
const sget = require('simple-get').concat
|
|
5
4
|
const fastify = require('..')
|
|
6
5
|
const helper = require('./helper')
|
|
7
|
-
const { waitForCb } = require('./toolkit')
|
|
8
6
|
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const sgetForwardedRequest = (app, forHeader, path, protoHeader, testCaseDone) => {
|
|
7
|
+
const fetchForwardedRequest = async (fastifyServer, forHeader, path, protoHeader) => {
|
|
12
8
|
const headers = {
|
|
13
9
|
'X-Forwarded-For': forHeader,
|
|
14
10
|
'X-Forwarded-Host': 'example.com'
|
|
@@ -16,11 +12,10 @@ const sgetForwardedRequest = (app, forHeader, path, protoHeader, testCaseDone) =
|
|
|
16
12
|
if (protoHeader) {
|
|
17
13
|
headers['X-Forwarded-Proto'] = protoHeader
|
|
18
14
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
headers
|
|
22
|
-
|
|
23
|
-
}, testCaseDone || noop)
|
|
15
|
+
|
|
16
|
+
return fetch(fastifyServer + path, {
|
|
17
|
+
headers
|
|
18
|
+
})
|
|
24
19
|
}
|
|
25
20
|
|
|
26
21
|
const testRequestValues = (t, req, options) => {
|
|
@@ -52,8 +47,8 @@ before(async function () {
|
|
|
52
47
|
[localhost] = await helper.getLoopbackHost()
|
|
53
48
|
})
|
|
54
49
|
|
|
55
|
-
test('trust proxy, not add properties to node req',
|
|
56
|
-
t.plan(
|
|
50
|
+
test('trust proxy, not add properties to node req', async t => {
|
|
51
|
+
t.plan(13)
|
|
57
52
|
const app = fastify({
|
|
58
53
|
trustProxy: true
|
|
59
54
|
})
|
|
@@ -69,20 +64,14 @@ test('trust proxy, not add properties to node req', (t, done) => {
|
|
|
69
64
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
70
65
|
})
|
|
71
66
|
|
|
72
|
-
app.listen({ port: 0 }
|
|
73
|
-
t.assert.ifError(err)
|
|
74
|
-
|
|
75
|
-
const completion = waitForCb({ steps: 2 })
|
|
76
|
-
|
|
77
|
-
sgetForwardedRequest(app, '1.1.1.1', '/trustproxy', undefined, completion.stepIn)
|
|
78
|
-
sgetForwardedRequest(app, '2.2.2.2, 1.1.1.1', '/trustproxychain', undefined, completion.stepIn)
|
|
67
|
+
const fastifyServer = await app.listen({ port: 0 })
|
|
79
68
|
|
|
80
|
-
|
|
81
|
-
|
|
69
|
+
await fetchForwardedRequest(fastifyServer, '1.1.1.1', '/trustproxy', undefined)
|
|
70
|
+
await fetchForwardedRequest(fastifyServer, '2.2.2.2, 1.1.1.1', '/trustproxychain', undefined)
|
|
82
71
|
})
|
|
83
72
|
|
|
84
|
-
test('trust proxy chain',
|
|
85
|
-
t.plan(
|
|
73
|
+
test('trust proxy chain', async t => {
|
|
74
|
+
t.plan(8)
|
|
86
75
|
const app = fastify({
|
|
87
76
|
trustProxy: [localhost, '192.168.1.1']
|
|
88
77
|
})
|
|
@@ -93,14 +82,12 @@ test('trust proxy chain', (t, done) => {
|
|
|
93
82
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
94
83
|
})
|
|
95
84
|
|
|
96
|
-
app.listen({ port: 0 }
|
|
97
|
-
|
|
98
|
-
sgetForwardedRequest(app, '192.168.1.1, 1.1.1.1', '/trustproxychain', undefined, done)
|
|
99
|
-
})
|
|
85
|
+
const fastifyServer = await app.listen({ port: 0 })
|
|
86
|
+
await fetchForwardedRequest(fastifyServer, '192.168.1.1, 1.1.1.1', '/trustproxychain', undefined)
|
|
100
87
|
})
|
|
101
88
|
|
|
102
|
-
test('trust proxy function',
|
|
103
|
-
t.plan(
|
|
89
|
+
test('trust proxy function', async t => {
|
|
90
|
+
t.plan(8)
|
|
104
91
|
const app = fastify({
|
|
105
92
|
trustProxy: (address) => address === localhost
|
|
106
93
|
})
|
|
@@ -111,14 +98,12 @@ test('trust proxy function', (t, done) => {
|
|
|
111
98
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
112
99
|
})
|
|
113
100
|
|
|
114
|
-
app.listen({ port: 0 }
|
|
115
|
-
|
|
116
|
-
sgetForwardedRequest(app, '1.1.1.1', '/trustproxyfunc', undefined, done)
|
|
117
|
-
})
|
|
101
|
+
const fastifyServer = await app.listen({ port: 0 })
|
|
102
|
+
await fetchForwardedRequest(fastifyServer, '1.1.1.1', '/trustproxyfunc', undefined)
|
|
118
103
|
})
|
|
119
104
|
|
|
120
|
-
test('trust proxy number',
|
|
121
|
-
t.plan(
|
|
105
|
+
test('trust proxy number', async t => {
|
|
106
|
+
t.plan(9)
|
|
122
107
|
const app = fastify({
|
|
123
108
|
trustProxy: 1
|
|
124
109
|
})
|
|
@@ -129,14 +114,12 @@ test('trust proxy number', (t, done) => {
|
|
|
129
114
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
130
115
|
})
|
|
131
116
|
|
|
132
|
-
app.listen({ port: 0 }
|
|
133
|
-
|
|
134
|
-
sgetForwardedRequest(app, '2.2.2.2, 1.1.1.1', '/trustproxynumber', undefined, done)
|
|
135
|
-
})
|
|
117
|
+
const fastifyServer = await app.listen({ port: 0 })
|
|
118
|
+
await fetchForwardedRequest(fastifyServer, '2.2.2.2, 1.1.1.1', '/trustproxynumber', undefined)
|
|
136
119
|
})
|
|
137
120
|
|
|
138
|
-
test('trust proxy IP addresses',
|
|
139
|
-
t.plan(
|
|
121
|
+
test('trust proxy IP addresses', async t => {
|
|
122
|
+
t.plan(9)
|
|
140
123
|
const app = fastify({
|
|
141
124
|
trustProxy: `${localhost}, 2.2.2.2`
|
|
142
125
|
})
|
|
@@ -147,14 +130,12 @@ test('trust proxy IP addresses', (t, done) => {
|
|
|
147
130
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
148
131
|
})
|
|
149
132
|
|
|
150
|
-
app.listen({ port: 0 }
|
|
151
|
-
|
|
152
|
-
sgetForwardedRequest(app, '3.3.3.3, 2.2.2.2, 1.1.1.1', '/trustproxyipaddrs', undefined, done)
|
|
153
|
-
})
|
|
133
|
+
const fastifyServer = await app.listen({ port: 0 })
|
|
134
|
+
await fetchForwardedRequest(fastifyServer, '3.3.3.3, 2.2.2.2, 1.1.1.1', '/trustproxyipaddrs', undefined)
|
|
154
135
|
})
|
|
155
136
|
|
|
156
|
-
test('trust proxy protocol',
|
|
157
|
-
t.plan(
|
|
137
|
+
test('trust proxy protocol', async t => {
|
|
138
|
+
t.plan(30)
|
|
158
139
|
const app = fastify({
|
|
159
140
|
trustProxy: true
|
|
160
141
|
})
|
|
@@ -173,16 +154,9 @@ test('trust proxy protocol', (t, done) => {
|
|
|
173
154
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
174
155
|
})
|
|
175
156
|
|
|
176
|
-
app.listen({ port: 0 }
|
|
177
|
-
t.assert.ifError(err)
|
|
178
|
-
|
|
179
|
-
const completion = waitForCb({ steps: 3 })
|
|
180
|
-
sgetForwardedRequest(app, '1.1.1.1', '/trustproxyprotocol', 'lorem', completion.stepIn)
|
|
181
|
-
sgetForwardedRequest(app, '1.1.1.1', '/trustproxynoprotocol', undefined, completion.stepIn)
|
|
157
|
+
const fastifyServer = await app.listen({ port: 0 })
|
|
182
158
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
completion.patience.then(done)
|
|
187
|
-
})
|
|
159
|
+
await fetchForwardedRequest(fastifyServer, '1.1.1.1', '/trustproxyprotocol', 'lorem')
|
|
160
|
+
await fetchForwardedRequest(fastifyServer, '1.1.1.1', '/trustproxynoprotocol', undefined)
|
|
161
|
+
await fetchForwardedRequest(fastifyServer, '1.1.1.1', '/trustproxyprotocols', 'ipsum, dolor')
|
|
188
162
|
})
|
|
@@ -273,3 +273,6 @@ expectType<FastifyErrorCodes>(fastify.errorCodes)
|
|
|
273
273
|
fastify({ allowUnsafeRegex: true })
|
|
274
274
|
fastify({ allowUnsafeRegex: false })
|
|
275
275
|
expectError(fastify({ allowUnsafeRegex: 'invalid' }))
|
|
276
|
+
|
|
277
|
+
expectAssignable<FastifyInstance>(fastify({ allowErrorHandlerOverride: true }))
|
|
278
|
+
expectAssignable<FastifyInstance>(fastify({ allowErrorHandlerOverride: false }))
|
|
@@ -78,6 +78,7 @@ const getHandler: RouteHandler = function (request, _reply) {
|
|
|
78
78
|
expectType<FastifySchema | undefined>(request.routeOptions.schema)
|
|
79
79
|
expectType<RouteHandlerMethod>(request.routeOptions.handler)
|
|
80
80
|
expectType<string | undefined>(request.routeOptions.url)
|
|
81
|
+
expectType<string | undefined>(request.routeOptions.version)
|
|
81
82
|
|
|
82
83
|
expectType<RequestHeadersDefault & RawRequestDefaultExpression['headers']>(request.headers)
|
|
83
84
|
request.headers = {}
|