fastify 5.2.0 → 5.2.2
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/LICENSE +1 -1
- package/PROJECT_CHARTER.md +7 -7
- package/README.md +65 -67
- package/SPONSORS.md +2 -0
- package/build/build-validation.js +1 -1
- package/docs/Guides/Benchmarking.md +4 -4
- package/docs/Guides/Database.md +1 -1
- package/docs/Guides/Delay-Accepting-Requests.md +10 -10
- package/docs/Guides/Ecosystem.md +5 -1
- package/docs/Guides/Fluent-Schema.md +1 -1
- package/docs/Guides/Getting-Started.md +9 -5
- package/docs/Guides/Index.md +1 -1
- package/docs/Guides/Migration-Guide-V4.md +1 -1
- package/docs/Guides/Migration-Guide-V5.md +12 -2
- package/docs/Guides/Plugins-Guide.md +6 -6
- package/docs/Guides/Serverless.md +14 -48
- package/docs/Guides/Style-Guide.md +2 -2
- package/docs/Guides/Testing.md +2 -2
- package/docs/Guides/Write-Plugin.md +2 -3
- package/docs/Reference/ContentTypeParser.md +58 -78
- package/docs/Reference/Decorators.md +50 -60
- package/docs/Reference/Encapsulation.md +28 -33
- package/docs/Reference/Errors.md +52 -53
- package/docs/Reference/HTTP2.md +7 -7
- package/docs/Reference/Hooks.md +31 -30
- package/docs/Reference/LTS.md +10 -15
- package/docs/Reference/Lifecycle.md +19 -24
- package/docs/Reference/Logging.md +59 -56
- package/docs/Reference/Middleware.md +19 -19
- package/docs/Reference/Plugins.md +55 -71
- package/docs/Reference/Principles.md +25 -30
- package/docs/Reference/Reply.md +11 -10
- package/docs/Reference/Request.md +89 -99
- package/docs/Reference/Routes.md +108 -128
- package/docs/Reference/Server.md +19 -17
- package/docs/Reference/Type-Providers.md +19 -21
- package/docs/Reference/TypeScript.md +1 -18
- package/docs/Reference/Validation-and-Serialization.md +134 -159
- package/docs/Reference/Warnings.md +22 -25
- package/fastify.js +1 -1
- package/lib/contentTypeParser.js +7 -8
- package/lib/error-handler.js +14 -12
- package/lib/errors.js +4 -0
- package/lib/headRoute.js +4 -2
- package/lib/pluginUtils.js +4 -2
- package/lib/reply.js +4 -0
- package/lib/request.js +13 -9
- package/lib/server.js +5 -0
- package/lib/validation.js +1 -1
- package/lib/warnings.js +9 -0
- package/lib/wrapThenable.js +8 -1
- package/package.json +28 -17
- package/test/build/error-serializer.test.js +2 -1
- package/test/bundler/esbuild/package.json +1 -1
- package/test/close.test.js +125 -108
- package/test/custom-parser-async.test.js +34 -36
- package/test/custom-parser.2.test.js +19 -20
- package/test/custom-parser.3.test.js +56 -45
- package/test/delete.test.js +79 -67
- package/test/genReqId.test.js +125 -174
- package/test/has-route.test.js +1 -3
- package/test/internals/content-type-parser.test.js +1 -1
- package/test/internals/errors.test.js +19 -7
- package/test/issue-4959.test.js +84 -0
- package/test/listen.1.test.js +37 -34
- package/test/listen.2.test.js +47 -40
- package/test/listen.3.test.js +28 -32
- package/test/listen.4.test.js +61 -45
- package/test/listen.5.test.js +23 -0
- package/test/nullable-validation.test.js +30 -27
- package/test/register.test.js +55 -50
- package/test/request-error.test.js +114 -94
- package/test/route-shorthand.test.js +36 -32
- package/test/server.test.js +0 -175
- package/test/stream.5.test.js +35 -33
- package/test/throw.test.js +87 -91
- package/test/toolkit.js +32 -0
- package/test/trust-proxy.test.js +23 -23
- package/test/types/instance.test-d.ts +1 -0
- package/test/upgrade.test.js +32 -30
- package/test/web-api.test.js +44 -0
- package/types/instance.d.ts +4 -0
- package/test/test-reporter.mjs +0 -68
package/test/throw.test.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { test } = require('
|
|
3
|
+
const { test } = require('node:test')
|
|
4
4
|
const Fastify = require('..')
|
|
5
5
|
|
|
6
|
-
test('Fastify should throw on wrong options', t => {
|
|
6
|
+
test('Fastify should throw on wrong options', (t) => {
|
|
7
7
|
t.plan(2)
|
|
8
8
|
try {
|
|
9
9
|
Fastify('lol')
|
|
10
|
-
t.fail()
|
|
10
|
+
t.assert.fail()
|
|
11
11
|
} catch (e) {
|
|
12
|
-
t.
|
|
13
|
-
t.
|
|
12
|
+
t.assert.strictEqual(e.message, 'Options must be an object')
|
|
13
|
+
t.assert.ok(true)
|
|
14
14
|
}
|
|
15
15
|
})
|
|
16
16
|
|
|
17
|
-
test('Fastify should throw on multiple assignment to the same route', t => {
|
|
17
|
+
test('Fastify should throw on multiple assignment to the same route', (t) => {
|
|
18
18
|
t.plan(1)
|
|
19
19
|
const fastify = Fastify()
|
|
20
20
|
|
|
@@ -22,14 +22,14 @@ test('Fastify should throw on multiple assignment to the same route', t => {
|
|
|
22
22
|
|
|
23
23
|
try {
|
|
24
24
|
fastify.get('/', () => {})
|
|
25
|
-
t.fail('Should throw fastify duplicated route declaration')
|
|
25
|
+
t.assert.fail('Should throw fastify duplicated route declaration')
|
|
26
26
|
} catch (error) {
|
|
27
|
-
t.
|
|
27
|
+
t.assert.strictEqual(error.code, 'FST_ERR_DUPLICATED_ROUTE')
|
|
28
28
|
}
|
|
29
29
|
})
|
|
30
30
|
|
|
31
|
-
test('Fastify should throw for an invalid schema, printing the error route - headers', t => {
|
|
32
|
-
t.plan(
|
|
31
|
+
test('Fastify should throw for an invalid schema, printing the error route - headers', async (t) => {
|
|
32
|
+
t.plan(1)
|
|
33
33
|
|
|
34
34
|
const badSchema = {
|
|
35
35
|
type: 'object',
|
|
@@ -39,20 +39,18 @@ test('Fastify should throw for an invalid schema, printing the error route - hea
|
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
-
|
|
43
42
|
const fastify = Fastify()
|
|
44
43
|
fastify.get('/', { schema: { headers: badSchema } }, () => {})
|
|
45
44
|
fastify.get('/not-loaded', { schema: { headers: badSchema } }, () => {})
|
|
46
45
|
|
|
47
|
-
fastify.ready(
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
await t.assert.rejects(fastify.ready(), {
|
|
47
|
+
code: 'FST_ERR_SCH_VALIDATION_BUILD',
|
|
48
|
+
message: /Failed building the validation schema for GET: \//
|
|
50
49
|
})
|
|
51
50
|
})
|
|
52
51
|
|
|
53
|
-
test('Fastify should throw for an invalid schema, printing the error route - body', t => {
|
|
54
|
-
t.plan(
|
|
55
|
-
|
|
52
|
+
test('Fastify should throw for an invalid schema, printing the error route - body', async (t) => {
|
|
53
|
+
t.plan(1)
|
|
56
54
|
const badSchema = {
|
|
57
55
|
type: 'object',
|
|
58
56
|
properties: {
|
|
@@ -68,13 +66,13 @@ test('Fastify should throw for an invalid schema, printing the error route - bod
|
|
|
68
66
|
done()
|
|
69
67
|
}, { prefix: 'hello' })
|
|
70
68
|
|
|
71
|
-
fastify.ready(
|
|
72
|
-
|
|
73
|
-
|
|
69
|
+
await t.assert.rejects(fastify.ready(), {
|
|
70
|
+
code: 'FST_ERR_SCH_VALIDATION_BUILD',
|
|
71
|
+
message: /Failed building the validation schema for POST: \/hello\/form/
|
|
74
72
|
})
|
|
75
73
|
})
|
|
76
74
|
|
|
77
|
-
test('Should throw on unsupported method', t => {
|
|
75
|
+
test('Should throw on unsupported method', async (t) => {
|
|
78
76
|
t.plan(1)
|
|
79
77
|
const fastify = Fastify()
|
|
80
78
|
try {
|
|
@@ -84,13 +82,13 @@ test('Should throw on unsupported method', t => {
|
|
|
84
82
|
schema: {},
|
|
85
83
|
handler: function (req, reply) {}
|
|
86
84
|
})
|
|
87
|
-
t.fail()
|
|
85
|
+
t.assert.fail()
|
|
88
86
|
} catch (e) {
|
|
89
|
-
t.
|
|
87
|
+
t.assert.ok(true)
|
|
90
88
|
}
|
|
91
89
|
})
|
|
92
90
|
|
|
93
|
-
test('Should throw on missing handler', t => {
|
|
91
|
+
test('Should throw on missing handler', (t) => {
|
|
94
92
|
t.plan(1)
|
|
95
93
|
const fastify = Fastify()
|
|
96
94
|
try {
|
|
@@ -98,15 +96,15 @@ test('Should throw on missing handler', t => {
|
|
|
98
96
|
method: 'GET',
|
|
99
97
|
url: '/'
|
|
100
98
|
})
|
|
101
|
-
t.fail()
|
|
99
|
+
t.assert.fail()
|
|
102
100
|
} catch (e) {
|
|
103
|
-
t.
|
|
101
|
+
t.assert.ok(true)
|
|
104
102
|
}
|
|
105
103
|
})
|
|
106
104
|
|
|
107
|
-
test('Should throw if one method is unsupported', t => {
|
|
108
|
-
const fastify = Fastify()
|
|
105
|
+
test('Should throw if one method is unsupported', async (t) => {
|
|
109
106
|
t.plan(1)
|
|
107
|
+
const fastify = Fastify()
|
|
110
108
|
try {
|
|
111
109
|
fastify.route({
|
|
112
110
|
method: ['GET', 'TROLL'],
|
|
@@ -114,28 +112,27 @@ test('Should throw if one method is unsupported', t => {
|
|
|
114
112
|
schema: {},
|
|
115
113
|
handler: function (req, reply) {}
|
|
116
114
|
})
|
|
117
|
-
t.fail()
|
|
115
|
+
t.assert.fail()
|
|
118
116
|
} catch (e) {
|
|
119
|
-
t.
|
|
117
|
+
t.assert.ok(true)
|
|
120
118
|
}
|
|
121
119
|
})
|
|
122
120
|
|
|
123
|
-
test('Should throw on duplicate content type parser', t => {
|
|
121
|
+
test('Should throw on duplicate content type parser', async (t) => {
|
|
124
122
|
t.plan(1)
|
|
125
|
-
|
|
126
123
|
const fastify = Fastify()
|
|
127
124
|
function customParser (req, payload, done) { done(null, '') }
|
|
128
125
|
|
|
129
126
|
fastify.addContentTypeParser('application/qq', customParser)
|
|
130
127
|
try {
|
|
131
128
|
fastify.addContentTypeParser('application/qq', customParser)
|
|
132
|
-
t.fail()
|
|
129
|
+
t.assert.fail()
|
|
133
130
|
} catch (e) {
|
|
134
|
-
t.
|
|
131
|
+
t.assert.ok(true)
|
|
135
132
|
}
|
|
136
133
|
})
|
|
137
134
|
|
|
138
|
-
test('Should throw on duplicate decorator', t => {
|
|
135
|
+
test('Should throw on duplicate decorator', async (t) => {
|
|
139
136
|
t.plan(1)
|
|
140
137
|
|
|
141
138
|
const fastify = Fastify()
|
|
@@ -144,31 +141,30 @@ test('Should throw on duplicate decorator', t => {
|
|
|
144
141
|
fastify.decorate('foo', fooObj)
|
|
145
142
|
try {
|
|
146
143
|
fastify.decorate('foo', fooObj)
|
|
147
|
-
t.fail()
|
|
144
|
+
t.assert.fail()
|
|
148
145
|
} catch (e) {
|
|
149
|
-
t.
|
|
146
|
+
t.assert.ok(true)
|
|
150
147
|
}
|
|
151
148
|
})
|
|
152
149
|
|
|
153
|
-
test('Should not throw on duplicate decorator encapsulation', t => {
|
|
150
|
+
test('Should not throw on duplicate decorator encapsulation', async (t) => {
|
|
154
151
|
t.plan(1)
|
|
155
|
-
|
|
156
152
|
const fastify = Fastify()
|
|
157
153
|
const foo2Obj = {}
|
|
158
154
|
|
|
159
155
|
fastify.decorate('foo2', foo2Obj)
|
|
160
156
|
|
|
161
157
|
fastify.register(function (fastify, opts, done) {
|
|
162
|
-
t.doesNotThrow(() => {
|
|
158
|
+
t.assert.doesNotThrow(() => {
|
|
163
159
|
fastify.decorate('foo2', foo2Obj)
|
|
164
160
|
})
|
|
165
161
|
done()
|
|
166
162
|
})
|
|
167
163
|
|
|
168
|
-
fastify.ready()
|
|
164
|
+
await fastify.ready()
|
|
169
165
|
})
|
|
170
166
|
|
|
171
|
-
test('Should throw on duplicate request decorator', t => {
|
|
167
|
+
test('Should throw on duplicate request decorator', async (t) => {
|
|
172
168
|
t.plan(2)
|
|
173
169
|
|
|
174
170
|
const fastify = Fastify()
|
|
@@ -176,28 +172,28 @@ test('Should throw on duplicate request decorator', t => {
|
|
|
176
172
|
fastify.decorateRequest('foo', null)
|
|
177
173
|
try {
|
|
178
174
|
fastify.decorateRequest('foo', null)
|
|
179
|
-
t.fail()
|
|
175
|
+
t.assert.fail()
|
|
180
176
|
} catch (e) {
|
|
181
|
-
t.
|
|
182
|
-
t.
|
|
177
|
+
t.assert.strictEqual(e.code, 'FST_ERR_DEC_ALREADY_PRESENT')
|
|
178
|
+
t.assert.strictEqual(e.message, 'The decorator \'foo\' has already been added!')
|
|
183
179
|
}
|
|
184
180
|
})
|
|
185
181
|
|
|
186
|
-
test('Should throw if request decorator dependencies are not met', t => {
|
|
182
|
+
test('Should throw if request decorator dependencies are not met', async (t) => {
|
|
187
183
|
t.plan(2)
|
|
188
184
|
|
|
189
185
|
const fastify = Fastify()
|
|
190
186
|
|
|
191
187
|
try {
|
|
192
188
|
fastify.decorateRequest('bar', null, ['world'])
|
|
193
|
-
t.fail()
|
|
189
|
+
t.assert.fail()
|
|
194
190
|
} catch (e) {
|
|
195
|
-
t.
|
|
196
|
-
t.
|
|
191
|
+
t.assert.strictEqual(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')
|
|
192
|
+
t.assert.strictEqual(e.message, 'The decorator is missing dependency \'world\'.')
|
|
197
193
|
}
|
|
198
194
|
})
|
|
199
195
|
|
|
200
|
-
test('Should throw on duplicate reply decorator', t => {
|
|
196
|
+
test('Should throw on duplicate reply decorator', async (t) => {
|
|
201
197
|
t.plan(1)
|
|
202
198
|
|
|
203
199
|
const fastify = Fastify()
|
|
@@ -205,149 +201,149 @@ test('Should throw on duplicate reply decorator', t => {
|
|
|
205
201
|
fastify.decorateReply('foo', null)
|
|
206
202
|
try {
|
|
207
203
|
fastify.decorateReply('foo', null)
|
|
208
|
-
t.fail()
|
|
204
|
+
t.assert.fail()
|
|
209
205
|
} catch (e) {
|
|
210
|
-
t.ok(/has already been added/.test(e.message))
|
|
206
|
+
t.assert.ok(/has already been added/.test(e.message))
|
|
211
207
|
}
|
|
212
208
|
})
|
|
213
209
|
|
|
214
|
-
test('Should throw if reply decorator dependencies are not met', t => {
|
|
210
|
+
test('Should throw if reply decorator dependencies are not met', async (t) => {
|
|
215
211
|
t.plan(1)
|
|
216
212
|
|
|
217
213
|
const fastify = Fastify()
|
|
218
214
|
|
|
219
215
|
try {
|
|
220
216
|
fastify.decorateReply('bar', null, ['world'])
|
|
221
|
-
t.fail()
|
|
217
|
+
t.assert.fail()
|
|
222
218
|
} catch (e) {
|
|
223
|
-
t.ok(/missing dependency/.test(e.message))
|
|
219
|
+
t.assert.ok(/missing dependency/.test(e.message))
|
|
224
220
|
}
|
|
225
221
|
})
|
|
226
222
|
|
|
227
|
-
test('Should throw if handler as the third parameter to the shortcut method is missing and the second parameter is not a function and also not an object', t => {
|
|
223
|
+
test('Should throw if handler as the third parameter to the shortcut method is missing and the second parameter is not a function and also not an object', async (t) => {
|
|
228
224
|
t.plan(5)
|
|
229
225
|
|
|
230
226
|
const fastify = Fastify()
|
|
231
227
|
|
|
232
228
|
try {
|
|
233
229
|
fastify.get('/foo/1', '')
|
|
234
|
-
t.fail()
|
|
230
|
+
t.assert.fail()
|
|
235
231
|
} catch (e) {
|
|
236
|
-
t.
|
|
232
|
+
t.assert.ok(true)
|
|
237
233
|
}
|
|
238
234
|
|
|
239
235
|
try {
|
|
240
236
|
fastify.get('/foo/2', 1)
|
|
241
|
-
t.fail()
|
|
237
|
+
t.assert.fail()
|
|
242
238
|
} catch (e) {
|
|
243
|
-
t.
|
|
239
|
+
t.assert.ok(true)
|
|
244
240
|
}
|
|
245
241
|
|
|
246
242
|
try {
|
|
247
243
|
fastify.get('/foo/3', [])
|
|
248
|
-
t.fail()
|
|
244
|
+
t.assert.fail()
|
|
249
245
|
} catch (e) {
|
|
250
|
-
t.
|
|
246
|
+
t.assert.ok(true)
|
|
251
247
|
}
|
|
252
248
|
|
|
253
249
|
try {
|
|
254
250
|
fastify.get('/foo/4', undefined)
|
|
255
|
-
t.fail()
|
|
251
|
+
t.assert.fail()
|
|
256
252
|
} catch (e) {
|
|
257
|
-
t.
|
|
253
|
+
t.assert.ok(true)
|
|
258
254
|
}
|
|
259
255
|
|
|
260
256
|
try {
|
|
261
257
|
fastify.get('/foo/5', null)
|
|
262
|
-
t.fail()
|
|
258
|
+
t.assert.fail()
|
|
263
259
|
} catch (e) {
|
|
264
|
-
t.
|
|
260
|
+
t.assert.ok(true)
|
|
265
261
|
}
|
|
266
262
|
})
|
|
267
263
|
|
|
268
|
-
test('Should throw if handler as the third parameter to the shortcut method is missing and the second parameter is not a function and also not an object', t => {
|
|
264
|
+
test('Should throw if handler as the third parameter to the shortcut method is missing and the second parameter is not a function and also not an object', async (t) => {
|
|
269
265
|
t.plan(5)
|
|
270
266
|
|
|
271
267
|
const fastify = Fastify()
|
|
272
268
|
|
|
273
269
|
try {
|
|
274
270
|
fastify.get('/foo/1', '')
|
|
275
|
-
t.fail()
|
|
271
|
+
t.assert.fail()
|
|
276
272
|
} catch (e) {
|
|
277
|
-
t.
|
|
273
|
+
t.assert.ok(true)
|
|
278
274
|
}
|
|
279
275
|
|
|
280
276
|
try {
|
|
281
277
|
fastify.get('/foo/2', 1)
|
|
282
|
-
t.fail()
|
|
278
|
+
t.assert.fail()
|
|
283
279
|
} catch (e) {
|
|
284
|
-
t.
|
|
280
|
+
t.assert.ok(true)
|
|
285
281
|
}
|
|
286
282
|
|
|
287
283
|
try {
|
|
288
284
|
fastify.get('/foo/3', [])
|
|
289
|
-
t.fail()
|
|
285
|
+
t.assert.fail()
|
|
290
286
|
} catch (e) {
|
|
291
|
-
t.
|
|
287
|
+
t.assert.ok(true)
|
|
292
288
|
}
|
|
293
289
|
|
|
294
290
|
try {
|
|
295
291
|
fastify.get('/foo/4', undefined)
|
|
296
|
-
t.fail()
|
|
292
|
+
t.assert.fail()
|
|
297
293
|
} catch (e) {
|
|
298
|
-
t.
|
|
294
|
+
t.assert.ok(true)
|
|
299
295
|
}
|
|
300
296
|
|
|
301
297
|
try {
|
|
302
298
|
fastify.get('/foo/5', null)
|
|
303
|
-
t.fail()
|
|
299
|
+
t.assert.fail()
|
|
304
300
|
} catch (e) {
|
|
305
|
-
t.
|
|
301
|
+
t.assert.ok(true)
|
|
306
302
|
}
|
|
307
303
|
})
|
|
308
304
|
|
|
309
|
-
test('Should throw if there is handler function as the third parameter to the shortcut method and options as the second parameter is not an object', t => {
|
|
305
|
+
test('Should throw if there is handler function as the third parameter to the shortcut method and options as the second parameter is not an object', async (t) => {
|
|
310
306
|
t.plan(5)
|
|
311
307
|
|
|
312
308
|
const fastify = Fastify()
|
|
313
309
|
|
|
314
310
|
try {
|
|
315
311
|
fastify.get('/foo/1', '', (req, res) => {})
|
|
316
|
-
t.fail()
|
|
312
|
+
t.assert.fail()
|
|
317
313
|
} catch (e) {
|
|
318
|
-
t.
|
|
314
|
+
t.assert.ok(true)
|
|
319
315
|
}
|
|
320
316
|
|
|
321
317
|
try {
|
|
322
318
|
fastify.get('/foo/2', 1, (req, res) => {})
|
|
323
|
-
t.fail()
|
|
319
|
+
t.assert.fail()
|
|
324
320
|
} catch (e) {
|
|
325
|
-
t.
|
|
321
|
+
t.assert.ok(true)
|
|
326
322
|
}
|
|
327
323
|
|
|
328
324
|
try {
|
|
329
325
|
fastify.get('/foo/3', [], (req, res) => {})
|
|
330
|
-
t.fail()
|
|
326
|
+
t.assert.fail()
|
|
331
327
|
} catch (e) {
|
|
332
|
-
t.
|
|
328
|
+
t.assert.ok(true)
|
|
333
329
|
}
|
|
334
330
|
|
|
335
331
|
try {
|
|
336
332
|
fastify.get('/foo/4', undefined, (req, res) => {})
|
|
337
|
-
t.fail()
|
|
333
|
+
t.assert.fail()
|
|
338
334
|
} catch (e) {
|
|
339
|
-
t.
|
|
335
|
+
t.assert.ok(true)
|
|
340
336
|
}
|
|
341
337
|
|
|
342
338
|
try {
|
|
343
339
|
fastify.get('/foo/5', null, (req, res) => {})
|
|
344
|
-
t.fail()
|
|
340
|
+
t.assert.fail()
|
|
345
341
|
} catch (e) {
|
|
346
|
-
t.
|
|
342
|
+
t.assert.ok(true)
|
|
347
343
|
}
|
|
348
344
|
})
|
|
349
345
|
|
|
350
|
-
test('Should throw if found duplicate handler as the third parameter to the shortcut method and in options', t => {
|
|
346
|
+
test('Should throw if found duplicate handler as the third parameter to the shortcut method and in options', async (t) => {
|
|
351
347
|
t.plan(1)
|
|
352
348
|
|
|
353
349
|
const fastify = Fastify()
|
|
@@ -356,8 +352,8 @@ test('Should throw if found duplicate handler as the third parameter to the shor
|
|
|
356
352
|
fastify.get('/foo/abc', {
|
|
357
353
|
handler: (req, res) => {}
|
|
358
354
|
}, (req, res) => {})
|
|
359
|
-
t.fail()
|
|
355
|
+
t.assert.fail()
|
|
360
356
|
} catch (e) {
|
|
361
|
-
t.
|
|
357
|
+
t.assert.ok(true)
|
|
362
358
|
}
|
|
363
359
|
})
|
package/test/toolkit.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
exports.waitForCb = function (options) {
|
|
4
|
+
let count = null
|
|
5
|
+
let done = false
|
|
6
|
+
let iResolve
|
|
7
|
+
let iReject
|
|
8
|
+
|
|
9
|
+
function stepIn () {
|
|
10
|
+
if (done) {
|
|
11
|
+
iReject(new Error('Unexpected done call'))
|
|
12
|
+
return
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (--count) {
|
|
16
|
+
return
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
done = true
|
|
20
|
+
iResolve()
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const patience = new Promise((resolve, reject) => {
|
|
24
|
+
iResolve = resolve
|
|
25
|
+
iReject = reject
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
count = options.steps || 1
|
|
29
|
+
done = false
|
|
30
|
+
|
|
31
|
+
return { stepIn, patience }
|
|
32
|
+
}
|
package/test/trust-proxy.test.js
CHANGED
|
@@ -4,6 +4,7 @@ const { test, before } = require('node:test')
|
|
|
4
4
|
const sget = require('simple-get').concat
|
|
5
5
|
const fastify = require('..')
|
|
6
6
|
const helper = require('./helper')
|
|
7
|
+
const { waitForCb } = require('./toolkit')
|
|
7
8
|
|
|
8
9
|
const noop = () => {}
|
|
9
10
|
|
|
@@ -69,18 +70,14 @@ test('trust proxy, not add properties to node req', (t, done) => {
|
|
|
69
70
|
})
|
|
70
71
|
|
|
71
72
|
app.listen({ port: 0 }, (err) => {
|
|
72
|
-
app.server.unref()
|
|
73
73
|
t.assert.ifError(err)
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
sgetForwardedRequest(app, '2.2.2.2, 1.1.1.1', '/trustproxychain', undefined, completed)
|
|
75
|
+
const completion = waitForCb({ steps: 2 })
|
|
77
76
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
}
|
|
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)
|
|
79
|
+
|
|
80
|
+
completion.patience.then(done)
|
|
84
81
|
})
|
|
85
82
|
})
|
|
86
83
|
|
|
@@ -89,6 +86,7 @@ test('trust proxy chain', (t, done) => {
|
|
|
89
86
|
const app = fastify({
|
|
90
87
|
trustProxy: [localhost, '192.168.1.1']
|
|
91
88
|
})
|
|
89
|
+
t.after(() => app.close())
|
|
92
90
|
|
|
93
91
|
app.get('/trustproxychain', function (req, reply) {
|
|
94
92
|
testRequestValues(t, req, { ip: '1.1.1.1', host: 'example.com', port: app.server.address().port })
|
|
@@ -96,9 +94,7 @@ test('trust proxy chain', (t, done) => {
|
|
|
96
94
|
})
|
|
97
95
|
|
|
98
96
|
app.listen({ port: 0 }, (err) => {
|
|
99
|
-
app.server.unref()
|
|
100
97
|
t.assert.ifError(err)
|
|
101
|
-
t.after(() => app.close())
|
|
102
98
|
sgetForwardedRequest(app, '192.168.1.1, 1.1.1.1', '/trustproxychain', undefined, done)
|
|
103
99
|
})
|
|
104
100
|
})
|
|
@@ -108,15 +104,15 @@ test('trust proxy function', (t, done) => {
|
|
|
108
104
|
const app = fastify({
|
|
109
105
|
trustProxy: (address) => address === localhost
|
|
110
106
|
})
|
|
107
|
+
t.after(() => app.close())
|
|
108
|
+
|
|
111
109
|
app.get('/trustproxyfunc', function (req, reply) {
|
|
112
110
|
testRequestValues(t, req, { ip: '1.1.1.1', host: 'example.com', port: app.server.address().port })
|
|
113
111
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
114
112
|
})
|
|
115
113
|
|
|
116
114
|
app.listen({ port: 0 }, (err) => {
|
|
117
|
-
app.server.unref()
|
|
118
115
|
t.assert.ifError(err)
|
|
119
|
-
t.after(() => app.close())
|
|
120
116
|
sgetForwardedRequest(app, '1.1.1.1', '/trustproxyfunc', undefined, done)
|
|
121
117
|
})
|
|
122
118
|
})
|
|
@@ -126,15 +122,15 @@ test('trust proxy number', (t, done) => {
|
|
|
126
122
|
const app = fastify({
|
|
127
123
|
trustProxy: 1
|
|
128
124
|
})
|
|
125
|
+
t.after(() => app.close())
|
|
126
|
+
|
|
129
127
|
app.get('/trustproxynumber', function (req, reply) {
|
|
130
128
|
testRequestValues(t, req, { ip: '1.1.1.1', ips: [localhost, '1.1.1.1'], host: 'example.com', port: app.server.address().port })
|
|
131
129
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
132
130
|
})
|
|
133
131
|
|
|
134
132
|
app.listen({ port: 0 }, (err) => {
|
|
135
|
-
app.server.unref()
|
|
136
133
|
t.assert.ifError(err)
|
|
137
|
-
t.after(() => app.close())
|
|
138
134
|
sgetForwardedRequest(app, '2.2.2.2, 1.1.1.1', '/trustproxynumber', undefined, done)
|
|
139
135
|
})
|
|
140
136
|
})
|
|
@@ -144,15 +140,15 @@ test('trust proxy IP addresses', (t, done) => {
|
|
|
144
140
|
const app = fastify({
|
|
145
141
|
trustProxy: `${localhost}, 2.2.2.2`
|
|
146
142
|
})
|
|
143
|
+
t.after(() => app.close())
|
|
144
|
+
|
|
147
145
|
app.get('/trustproxyipaddrs', function (req, reply) {
|
|
148
146
|
testRequestValues(t, req, { ip: '1.1.1.1', ips: [localhost, '1.1.1.1'], host: 'example.com', port: app.server.address().port })
|
|
149
147
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
150
148
|
})
|
|
151
149
|
|
|
152
150
|
app.listen({ port: 0 }, (err) => {
|
|
153
|
-
app.server.unref()
|
|
154
151
|
t.assert.ifError(err)
|
|
155
|
-
t.after(() => app.close())
|
|
156
152
|
sgetForwardedRequest(app, '3.3.3.3, 2.2.2.2, 1.1.1.1', '/trustproxyipaddrs', undefined, done)
|
|
157
153
|
})
|
|
158
154
|
})
|
|
@@ -162,6 +158,8 @@ test('trust proxy protocol', (t, done) => {
|
|
|
162
158
|
const app = fastify({
|
|
163
159
|
trustProxy: true
|
|
164
160
|
})
|
|
161
|
+
t.after(() => app.close())
|
|
162
|
+
|
|
165
163
|
app.get('/trustproxyprotocol', function (req, reply) {
|
|
166
164
|
testRequestValues(t, req, { ip: '1.1.1.1', protocol: 'lorem', host: 'example.com', port: app.server.address().port })
|
|
167
165
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
@@ -175,14 +173,16 @@ test('trust proxy protocol', (t, done) => {
|
|
|
175
173
|
reply.code(200).send({ ip: req.ip, host: req.host })
|
|
176
174
|
})
|
|
177
175
|
|
|
178
|
-
t.after(() => app.close())
|
|
179
|
-
|
|
180
176
|
app.listen({ port: 0 }, (err) => {
|
|
181
|
-
app.server.unref()
|
|
182
177
|
t.assert.ifError(err)
|
|
183
|
-
|
|
184
|
-
|
|
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)
|
|
182
|
+
|
|
185
183
|
// Allow for sgetForwardedRequest requests above to finish
|
|
186
|
-
setTimeout(() => sgetForwardedRequest(app, '1.1.1.1', '/trustproxyprotocols', 'ipsum, dolor',
|
|
184
|
+
setTimeout(() => sgetForwardedRequest(app, '1.1.1.1', '/trustproxyprotocols', 'ipsum, dolor', completion.stepIn))
|
|
185
|
+
|
|
186
|
+
completion.patience.then(done)
|
|
187
187
|
})
|
|
188
188
|
})
|
|
@@ -36,6 +36,7 @@ expectType<unknown>(server.getSchema('SchemaId'))
|
|
|
36
36
|
expectType<string>(server.printRoutes())
|
|
37
37
|
expectType<string>(server.printPlugins())
|
|
38
38
|
expectType<string>(server.listeningOrigin)
|
|
39
|
+
expectType<string[]>(server.supportedMethods)
|
|
39
40
|
|
|
40
41
|
expectAssignable<FastifyInstance>(
|
|
41
42
|
server.setErrorHandler(function (error, request, reply) {
|