fastify 5.3.2 → 5.3.3
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 +22 -0
- package/docs/Guides/Ecosystem.md +7 -2
- package/docs/Guides/Serverless.md +28 -69
- package/docs/Reference/Errors.md +0 -2
- package/docs/Reference/Server.md +17 -1
- package/eslint.config.js +17 -9
- package/fastify.js +6 -2
- package/lib/decorate.js +2 -2
- package/lib/errors.js +0 -8
- package/lib/logger-factory.js +1 -1
- package/lib/logger-pino.js +2 -2
- package/lib/reply.js +2 -2
- package/lib/request.js +1 -1
- package/lib/server.js +30 -51
- package/package.json +4 -4
- package/test/close-pipelining.test.js +5 -4
- package/test/decorator.test.js +422 -341
- package/test/helper.js +107 -69
- package/test/hooks.on-listen.test.js +255 -239
- package/test/hooks.on-ready.test.js +110 -92
- package/test/inject.test.js +114 -97
- package/test/input-validation.js +63 -53
- package/test/internals/errors.test.js +1 -11
- package/test/internals/hooks.test.js +17 -0
- package/test/issue-4959.test.js +2 -2
- package/test/logger/response.test.js +19 -20
- package/test/options.error-handler.test.js +1 -1
- package/test/options.test.js +1 -1
- package/test/output-validation.test.js +49 -70
- package/test/patch.error-handler.test.js +1 -1
- package/test/patch.test.js +1 -1
- package/test/plugin.1.test.js +71 -60
- package/test/promises.test.js +36 -30
- package/test/put.error-handler.test.js +1 -1
- package/test/put.test.js +1 -1
- package/test/reply-error.test.js +169 -148
- package/test/reply-trailers.test.js +119 -108
- package/test/schema-feature.test.js +309 -238
- package/test/schema-validation.test.js +44 -2
- package/test/stream.1.test.js +30 -27
- package/test/stream.2.test.js +20 -10
- package/test/stream.3.test.js +37 -31
- package/test/types/errors.test-d.ts +0 -1
- package/test/types/plugin.test-d.ts +1 -1
- package/test/types/register.test-d.ts +1 -1
- package/test/use-semicolon-delimiter.test.js +1 -1
- package/types/errors.d.ts +0 -1
- package/test/http2/missing-http2-module.test.js +0 -17
package/test/input-validation.js
CHANGED
|
@@ -4,6 +4,7 @@ const sget = require('simple-get').concat
|
|
|
4
4
|
const Ajv = require('ajv')
|
|
5
5
|
const Joi = require('joi')
|
|
6
6
|
const yup = require('yup')
|
|
7
|
+
const assert = require('node:assert')
|
|
7
8
|
|
|
8
9
|
module.exports.payloadMethod = function (method, t) {
|
|
9
10
|
const test = t.test
|
|
@@ -127,28 +128,27 @@ module.exports.payloadMethod = function (method, t) {
|
|
|
127
128
|
|
|
128
129
|
done()
|
|
129
130
|
})
|
|
130
|
-
t.
|
|
131
|
+
t.assert.ok(true)
|
|
131
132
|
} catch (e) {
|
|
132
|
-
t.fail()
|
|
133
|
+
t.assert.fail()
|
|
133
134
|
}
|
|
134
135
|
})
|
|
135
136
|
|
|
136
137
|
fastify.listen({ port: 0 }, function (err) {
|
|
137
|
-
|
|
138
|
-
t.error(err)
|
|
139
|
-
}
|
|
138
|
+
assert.ifError(err)
|
|
140
139
|
|
|
141
|
-
t.
|
|
140
|
+
t.after(() => { fastify.close() })
|
|
142
141
|
|
|
143
|
-
test(`${upMethod} - correctly replies`, t => {
|
|
142
|
+
test(`${upMethod} - correctly replies`, (t, testDone) => {
|
|
144
143
|
if (upMethod === 'HEAD') {
|
|
145
144
|
t.plan(2)
|
|
146
145
|
sget({
|
|
147
146
|
method: upMethod,
|
|
148
147
|
url: 'http://localhost:' + fastify.server.address().port
|
|
149
148
|
}, (err, response) => {
|
|
150
|
-
t.
|
|
151
|
-
t.
|
|
149
|
+
t.assert.ifError(err)
|
|
150
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
151
|
+
testDone()
|
|
152
152
|
})
|
|
153
153
|
} else {
|
|
154
154
|
t.plan(3)
|
|
@@ -160,14 +160,15 @@ module.exports.payloadMethod = function (method, t) {
|
|
|
160
160
|
},
|
|
161
161
|
json: true
|
|
162
162
|
}, (err, response, body) => {
|
|
163
|
-
t.
|
|
164
|
-
t.
|
|
165
|
-
t.
|
|
163
|
+
t.assert.ifError(err)
|
|
164
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
165
|
+
t.assert.deepStrictEqual(body, { hello: 42 })
|
|
166
|
+
testDone()
|
|
166
167
|
})
|
|
167
168
|
}
|
|
168
169
|
})
|
|
169
170
|
|
|
170
|
-
test(`${upMethod} - 400 on bad parameters`, t => {
|
|
171
|
+
test(`${upMethod} - 400 on bad parameters`, (t, testDone) => {
|
|
171
172
|
t.plan(3)
|
|
172
173
|
sget({
|
|
173
174
|
method: upMethod,
|
|
@@ -177,18 +178,19 @@ module.exports.payloadMethod = function (method, t) {
|
|
|
177
178
|
},
|
|
178
179
|
json: true
|
|
179
180
|
}, (err, response, body) => {
|
|
180
|
-
t.
|
|
181
|
-
t.
|
|
182
|
-
t.
|
|
181
|
+
t.assert.ifError(err)
|
|
182
|
+
t.assert.strictEqual(response.statusCode, 400)
|
|
183
|
+
t.assert.deepStrictEqual(body, {
|
|
183
184
|
error: 'Bad Request',
|
|
184
185
|
message: 'body/hello must be integer',
|
|
185
186
|
statusCode: 400,
|
|
186
187
|
code: 'FST_ERR_VALIDATION'
|
|
187
188
|
})
|
|
189
|
+
testDone()
|
|
188
190
|
})
|
|
189
191
|
})
|
|
190
192
|
|
|
191
|
-
test(`${upMethod} - input-validation coerce`, t => {
|
|
193
|
+
test(`${upMethod} - input-validation coerce`, (t, testDone) => {
|
|
192
194
|
t.plan(3)
|
|
193
195
|
sget({
|
|
194
196
|
method: upMethod,
|
|
@@ -198,13 +200,14 @@ module.exports.payloadMethod = function (method, t) {
|
|
|
198
200
|
},
|
|
199
201
|
json: true
|
|
200
202
|
}, (err, response, body) => {
|
|
201
|
-
t.
|
|
202
|
-
t.
|
|
203
|
-
t.
|
|
203
|
+
t.assert.ifError(err)
|
|
204
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
205
|
+
t.assert.deepStrictEqual(body, { hello: 42 })
|
|
206
|
+
testDone()
|
|
204
207
|
})
|
|
205
208
|
})
|
|
206
209
|
|
|
207
|
-
test(`${upMethod} - input-validation custom schema compiler`, t => {
|
|
210
|
+
test(`${upMethod} - input-validation custom schema compiler`, (t, testDone) => {
|
|
208
211
|
t.plan(3)
|
|
209
212
|
sget({
|
|
210
213
|
method: upMethod,
|
|
@@ -215,13 +218,14 @@ module.exports.payloadMethod = function (method, t) {
|
|
|
215
218
|
},
|
|
216
219
|
json: true
|
|
217
220
|
}, (err, response, body) => {
|
|
218
|
-
t.
|
|
219
|
-
t.
|
|
220
|
-
t.
|
|
221
|
+
t.assert.ifError(err)
|
|
222
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
223
|
+
t.assert.deepStrictEqual(body, { hello: 42 })
|
|
224
|
+
testDone()
|
|
221
225
|
})
|
|
222
226
|
})
|
|
223
227
|
|
|
224
|
-
test(`${upMethod} - input-validation joi schema compiler ok`, t => {
|
|
228
|
+
test(`${upMethod} - input-validation joi schema compiler ok`, (t, testDone) => {
|
|
225
229
|
t.plan(3)
|
|
226
230
|
sget({
|
|
227
231
|
method: upMethod,
|
|
@@ -231,13 +235,14 @@ module.exports.payloadMethod = function (method, t) {
|
|
|
231
235
|
},
|
|
232
236
|
json: true
|
|
233
237
|
}, (err, response, body) => {
|
|
234
|
-
t.
|
|
235
|
-
t.
|
|
236
|
-
t.
|
|
238
|
+
t.assert.ifError(err)
|
|
239
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
240
|
+
t.assert.deepStrictEqual(body, { hello: '42' })
|
|
241
|
+
testDone()
|
|
237
242
|
})
|
|
238
243
|
})
|
|
239
244
|
|
|
240
|
-
test(`${upMethod} - input-validation joi schema compiler ko`, t => {
|
|
245
|
+
test(`${upMethod} - input-validation joi schema compiler ko`, (t, testDone) => {
|
|
241
246
|
t.plan(3)
|
|
242
247
|
sget({
|
|
243
248
|
method: upMethod,
|
|
@@ -247,18 +252,19 @@ module.exports.payloadMethod = function (method, t) {
|
|
|
247
252
|
},
|
|
248
253
|
json: true
|
|
249
254
|
}, (err, response, body) => {
|
|
250
|
-
t.
|
|
251
|
-
t.
|
|
252
|
-
t.
|
|
255
|
+
t.assert.ifError(err)
|
|
256
|
+
t.assert.strictEqual(response.statusCode, 400)
|
|
257
|
+
t.assert.deepStrictEqual(body, {
|
|
253
258
|
error: 'Bad Request',
|
|
254
259
|
message: '"hello" must be a string',
|
|
255
260
|
statusCode: 400,
|
|
256
261
|
code: 'FST_ERR_VALIDATION'
|
|
257
262
|
})
|
|
263
|
+
testDone()
|
|
258
264
|
})
|
|
259
265
|
})
|
|
260
266
|
|
|
261
|
-
test(`${upMethod} - input-validation yup schema compiler ok`, t => {
|
|
267
|
+
test(`${upMethod} - input-validation yup schema compiler ok`, (t, testDone) => {
|
|
262
268
|
t.plan(3)
|
|
263
269
|
sget({
|
|
264
270
|
method: upMethod,
|
|
@@ -268,13 +274,14 @@ module.exports.payloadMethod = function (method, t) {
|
|
|
268
274
|
},
|
|
269
275
|
json: true
|
|
270
276
|
}, (err, response, body) => {
|
|
271
|
-
t.
|
|
272
|
-
t.
|
|
273
|
-
t.
|
|
277
|
+
t.assert.ifError(err)
|
|
278
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
279
|
+
t.assert.deepStrictEqual(body, { hello: '42' })
|
|
280
|
+
testDone()
|
|
274
281
|
})
|
|
275
282
|
})
|
|
276
283
|
|
|
277
|
-
test(`${upMethod} - input-validation yup schema compiler ko`, t => {
|
|
284
|
+
test(`${upMethod} - input-validation yup schema compiler ko`, (t, testDone) => {
|
|
278
285
|
t.plan(3)
|
|
279
286
|
sget({
|
|
280
287
|
method: upMethod,
|
|
@@ -284,52 +291,55 @@ module.exports.payloadMethod = function (method, t) {
|
|
|
284
291
|
},
|
|
285
292
|
json: true
|
|
286
293
|
}, (err, response, body) => {
|
|
287
|
-
t.
|
|
288
|
-
t.
|
|
289
|
-
t.
|
|
294
|
+
t.assert.ifError(err)
|
|
295
|
+
t.assert.strictEqual(response.statusCode, 400)
|
|
296
|
+
t.assert.deepStrictEqual(body, {
|
|
290
297
|
error: 'Bad Request',
|
|
291
|
-
message:
|
|
298
|
+
message: 'body hello must be a `string` type, but the final value was: `44`.',
|
|
292
299
|
statusCode: 400,
|
|
293
300
|
code: 'FST_ERR_VALIDATION'
|
|
294
301
|
})
|
|
302
|
+
testDone()
|
|
295
303
|
})
|
|
296
304
|
})
|
|
297
305
|
|
|
298
|
-
test(`${upMethod} - input-validation instance custom schema compiler encapsulated`, t => {
|
|
306
|
+
test(`${upMethod} - input-validation instance custom schema compiler encapsulated`, (t, testDone) => {
|
|
299
307
|
t.plan(3)
|
|
300
308
|
sget({
|
|
301
309
|
method: upMethod,
|
|
302
310
|
url: 'http://localhost:' + fastify.server.address().port + '/plugin',
|
|
303
|
-
body: {
|
|
311
|
+
body: {},
|
|
304
312
|
json: true
|
|
305
313
|
}, (err, response, body) => {
|
|
306
|
-
t.
|
|
307
|
-
t.
|
|
308
|
-
t.
|
|
314
|
+
t.assert.ifError(err)
|
|
315
|
+
t.assert.strictEqual(response.statusCode, 400)
|
|
316
|
+
t.assert.deepStrictEqual(body, {
|
|
309
317
|
error: 'Bad Request',
|
|
310
318
|
message: 'From custom schema compiler!',
|
|
311
|
-
statusCode:
|
|
319
|
+
statusCode: 400,
|
|
312
320
|
code: 'FST_ERR_VALIDATION'
|
|
313
321
|
})
|
|
322
|
+
testDone()
|
|
314
323
|
})
|
|
315
324
|
})
|
|
316
325
|
|
|
317
|
-
test(`${upMethod} - input-validation custom schema compiler encapsulated`, t => {
|
|
326
|
+
test(`${upMethod} - input-validation custom schema compiler encapsulated`, (t, testDone) => {
|
|
318
327
|
t.plan(3)
|
|
319
328
|
sget({
|
|
320
329
|
method: upMethod,
|
|
321
330
|
url: 'http://localhost:' + fastify.server.address().port + '/plugin/custom',
|
|
322
|
-
body: {
|
|
331
|
+
body: {},
|
|
323
332
|
json: true
|
|
324
333
|
}, (err, response, body) => {
|
|
325
|
-
t.
|
|
326
|
-
t.
|
|
327
|
-
t.
|
|
334
|
+
t.assert.ifError(err)
|
|
335
|
+
t.assert.strictEqual(response.statusCode, 400)
|
|
336
|
+
t.assert.deepStrictEqual(body, {
|
|
328
337
|
error: 'Bad Request',
|
|
329
338
|
message: 'Always fail!',
|
|
330
|
-
statusCode:
|
|
339
|
+
statusCode: 400,
|
|
331
340
|
code: 'FST_ERR_VALIDATION'
|
|
332
341
|
})
|
|
342
|
+
testDone()
|
|
333
343
|
})
|
|
334
344
|
})
|
|
335
345
|
})
|
|
@@ -5,7 +5,7 @@ const errors = require('../../lib/errors')
|
|
|
5
5
|
const { readFileSync } = require('node:fs')
|
|
6
6
|
const { resolve } = require('node:path')
|
|
7
7
|
|
|
8
|
-
const expectedErrors =
|
|
8
|
+
const expectedErrors = 84
|
|
9
9
|
|
|
10
10
|
test(`should expose ${expectedErrors} errors`, t => {
|
|
11
11
|
t.plan(1)
|
|
@@ -580,16 +580,6 @@ test('FST_ERR_SCH_RESPONSE_SCHEMA_NOT_NESTED_2XX', t => {
|
|
|
580
580
|
t.assert.ok(error instanceof Error)
|
|
581
581
|
})
|
|
582
582
|
|
|
583
|
-
test('FST_ERR_HTTP2_INVALID_VERSION', t => {
|
|
584
|
-
t.plan(5)
|
|
585
|
-
const error = new errors.FST_ERR_HTTP2_INVALID_VERSION()
|
|
586
|
-
t.assert.strictEqual(error.name, 'FastifyError')
|
|
587
|
-
t.assert.strictEqual(error.code, 'FST_ERR_HTTP2_INVALID_VERSION')
|
|
588
|
-
t.assert.strictEqual(error.message, 'HTTP2 is available only from node >= 8.8.1')
|
|
589
|
-
t.assert.strictEqual(error.statusCode, 500)
|
|
590
|
-
t.assert.ok(error instanceof Error)
|
|
591
|
-
})
|
|
592
|
-
|
|
593
583
|
test('FST_ERR_INIT_OPTS_INVALID', t => {
|
|
594
584
|
t.plan(5)
|
|
595
585
|
const error = new errors.FST_ERR_INIT_OPTS_INVALID()
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { test } = require('node:test')
|
|
4
4
|
const { Hooks } = require('../../lib/hooks')
|
|
5
|
+
const { default: fastify } = require('../../fastify')
|
|
5
6
|
const noop = () => {}
|
|
6
7
|
|
|
7
8
|
test('hooks should have 4 array with the registered hooks', t => {
|
|
@@ -77,3 +78,19 @@ test('should throw on wrong parameters', t => {
|
|
|
77
78
|
t.assert.strictEqual(e.message, 'onSend hook should be a function, instead got [object Null]')
|
|
78
79
|
}
|
|
79
80
|
})
|
|
81
|
+
|
|
82
|
+
test('Integration test: internal function _addHook should be turned into app.ready() rejection', async (t) => {
|
|
83
|
+
const app = fastify()
|
|
84
|
+
|
|
85
|
+
app.register(async function () {
|
|
86
|
+
app.addHook('notRealHook', async () => {})
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
await app.ready()
|
|
91
|
+
t.assert.fail('Expected ready() to throw')
|
|
92
|
+
} catch (err) {
|
|
93
|
+
t.assert.strictEqual(err.code, 'FST_ERR_HOOK_NOT_SUPPORTED')
|
|
94
|
+
t.assert.match(err.message, /hook not supported/i)
|
|
95
|
+
}
|
|
96
|
+
})
|
package/test/issue-4959.test.js
CHANGED
|
@@ -17,7 +17,7 @@ function runBadClientCall (reqOptions, payload) {
|
|
|
17
17
|
...reqOptions,
|
|
18
18
|
headers: {
|
|
19
19
|
'Content-Type': 'application/json',
|
|
20
|
-
'Content-Length': Buffer.byteLength(postData)
|
|
20
|
+
'Content-Length': Buffer.byteLength(postData)
|
|
21
21
|
}
|
|
22
22
|
}, () => {
|
|
23
23
|
innerReject(new Error('Request should have failed'))
|
|
@@ -78,7 +78,7 @@ test('should handle a soket error', async (t) => {
|
|
|
78
78
|
hostname: 'localhost',
|
|
79
79
|
port: fastify.server.address().port,
|
|
80
80
|
path: '/',
|
|
81
|
-
method: 'PUT'
|
|
81
|
+
method: 'PUT'
|
|
82
82
|
}, { test: 'me' })
|
|
83
83
|
t.assert.equal(err.code, 'ECONNRESET')
|
|
84
84
|
})
|
|
@@ -2,19 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
const stream = require('node:stream')
|
|
4
4
|
|
|
5
|
-
const t = require('
|
|
5
|
+
const t = require('node:test')
|
|
6
6
|
const split = require('split2')
|
|
7
7
|
const pino = require('pino')
|
|
8
8
|
|
|
9
9
|
const Fastify = require('../../fastify')
|
|
10
|
+
const { partialDeepStrictEqual } = require('../toolkit')
|
|
10
11
|
const { on } = stream
|
|
11
12
|
|
|
12
|
-
t.test('response serialization', (t) => {
|
|
13
|
-
t.setTimeout(60000)
|
|
14
|
-
|
|
13
|
+
t.test('response serialization', { timeout: 60000 }, async (t) => {
|
|
15
14
|
t.plan(4)
|
|
16
15
|
|
|
17
|
-
t.test('Should use serializers from plugin and route', async (t) => {
|
|
16
|
+
await t.test('Should use serializers from plugin and route', async (t) => {
|
|
18
17
|
const lines = [
|
|
19
18
|
{ msg: 'incoming request' },
|
|
20
19
|
{ test: 'XHello', test2: 'ZHello' },
|
|
@@ -28,7 +27,7 @@ t.test('response serialization', (t) => {
|
|
|
28
27
|
const fastify = Fastify({
|
|
29
28
|
loggerInstance
|
|
30
29
|
})
|
|
31
|
-
t.
|
|
30
|
+
t.after(() => fastify.close())
|
|
32
31
|
|
|
33
32
|
fastify.register(context1, {
|
|
34
33
|
logSerializers: { test: value => 'X' + value }
|
|
@@ -51,16 +50,16 @@ t.test('response serialization', (t) => {
|
|
|
51
50
|
{
|
|
52
51
|
const response = await fastify.inject({ method: 'GET', url: '/' })
|
|
53
52
|
const body = await response.json()
|
|
54
|
-
t.
|
|
53
|
+
t.assert.deepStrictEqual(body, { hello: 'world' })
|
|
55
54
|
}
|
|
56
55
|
|
|
57
56
|
for await (const [line] of on(stream, 'data')) {
|
|
58
|
-
t.
|
|
57
|
+
t.assert.ok(partialDeepStrictEqual(line, lines.shift()))
|
|
59
58
|
if (lines.length === 0) break
|
|
60
59
|
}
|
|
61
60
|
})
|
|
62
61
|
|
|
63
|
-
t.test('Should use serializers from instance fastify and route', async (t) => {
|
|
62
|
+
await t.test('Should use serializers from instance fastify and route', async (t) => {
|
|
64
63
|
const lines = [
|
|
65
64
|
{ msg: 'incoming request' },
|
|
66
65
|
{ test: 'XHello', test2: 'ZHello' },
|
|
@@ -80,7 +79,7 @@ t.test('response serialization', (t) => {
|
|
|
80
79
|
const fastify = Fastify({
|
|
81
80
|
loggerInstance
|
|
82
81
|
})
|
|
83
|
-
t.
|
|
82
|
+
t.after(() => fastify.close())
|
|
84
83
|
|
|
85
84
|
fastify.get('/', {
|
|
86
85
|
logSerializers: {
|
|
@@ -96,16 +95,16 @@ t.test('response serialization', (t) => {
|
|
|
96
95
|
{
|
|
97
96
|
const response = await fastify.inject({ method: 'GET', url: '/' })
|
|
98
97
|
const body = await response.json()
|
|
99
|
-
t.
|
|
98
|
+
t.assert.deepStrictEqual(body, { hello: 'world' })
|
|
100
99
|
}
|
|
101
100
|
|
|
102
101
|
for await (const [line] of on(stream, 'data')) {
|
|
103
|
-
t.
|
|
102
|
+
t.assert.ok(partialDeepStrictEqual(line, lines.shift()))
|
|
104
103
|
if (lines.length === 0) break
|
|
105
104
|
}
|
|
106
105
|
})
|
|
107
106
|
|
|
108
|
-
t.test('Should use serializers inherit from contexts', async (t) => {
|
|
107
|
+
await t.test('Should use serializers inherit from contexts', async (t) => {
|
|
109
108
|
const lines = [
|
|
110
109
|
{ msg: 'incoming request' },
|
|
111
110
|
{ test: 'XHello', test2: 'YHello', test3: 'ZHello' },
|
|
@@ -123,7 +122,7 @@ t.test('response serialization', (t) => {
|
|
|
123
122
|
}, stream)
|
|
124
123
|
|
|
125
124
|
const fastify = Fastify({ loggerInstance })
|
|
126
|
-
t.
|
|
125
|
+
t.after(() => fastify.close())
|
|
127
126
|
|
|
128
127
|
fastify.register(context1, { logSerializers: { test2: value => 'Y' + value } })
|
|
129
128
|
|
|
@@ -144,16 +143,16 @@ t.test('response serialization', (t) => {
|
|
|
144
143
|
{
|
|
145
144
|
const response = await fastify.inject({ method: 'GET', url: '/' })
|
|
146
145
|
const body = await response.json()
|
|
147
|
-
t.
|
|
146
|
+
t.assert.deepStrictEqual(body, { hello: 'world' })
|
|
148
147
|
}
|
|
149
148
|
|
|
150
149
|
for await (const [line] of on(stream, 'data')) {
|
|
151
|
-
t.
|
|
150
|
+
t.assert.ok(partialDeepStrictEqual(line, lines.shift()))
|
|
152
151
|
if (lines.length === 0) break
|
|
153
152
|
}
|
|
154
153
|
})
|
|
155
154
|
|
|
156
|
-
t.test('should serialize request and response', async (t) => {
|
|
155
|
+
await t.test('should serialize request and response', async (t) => {
|
|
157
156
|
const lines = [
|
|
158
157
|
{ req: { method: 'GET', url: '/500' }, msg: 'incoming request' },
|
|
159
158
|
{ req: { method: 'GET', url: '/500' }, msg: '500 error' },
|
|
@@ -163,7 +162,7 @@ t.test('response serialization', (t) => {
|
|
|
163
162
|
|
|
164
163
|
const stream = split(JSON.parse)
|
|
165
164
|
const fastify = Fastify({ logger: { level: 'info', stream } })
|
|
166
|
-
t.
|
|
165
|
+
t.after(() => fastify.close())
|
|
167
166
|
|
|
168
167
|
fastify.get('/500', (req, reply) => {
|
|
169
168
|
reply.code(500).send(Error('500 error'))
|
|
@@ -173,11 +172,11 @@ t.test('response serialization', (t) => {
|
|
|
173
172
|
|
|
174
173
|
{
|
|
175
174
|
const response = await fastify.inject({ method: 'GET', url: '/500' })
|
|
176
|
-
t.
|
|
175
|
+
t.assert.strictEqual(response.statusCode, 500)
|
|
177
176
|
}
|
|
178
177
|
|
|
179
178
|
for await (const [line] of on(stream, 'data')) {
|
|
180
|
-
t.
|
|
179
|
+
t.assert.ok(partialDeepStrictEqual(line, lines.shift()))
|
|
181
180
|
if (lines.length === 0) break
|
|
182
181
|
}
|
|
183
182
|
})
|
package/test/options.test.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const test = t.test
|
|
3
|
+
const { test } = require('node:test')
|
|
5
4
|
const sget = require('simple-get').concat
|
|
6
5
|
const fastify = require('..')()
|
|
7
6
|
|
|
@@ -34,9 +33,9 @@ test('shorthand - output string', t => {
|
|
|
34
33
|
fastify.get('/string', opts, function (req, reply) {
|
|
35
34
|
reply.code(200).send({ hello: 'world' })
|
|
36
35
|
})
|
|
37
|
-
t.
|
|
36
|
+
t.assert.ok(true)
|
|
38
37
|
} catch (e) {
|
|
39
|
-
t.fail()
|
|
38
|
+
t.assert.fail()
|
|
40
39
|
}
|
|
41
40
|
})
|
|
42
41
|
|
|
@@ -46,9 +45,9 @@ test('shorthand - output number', t => {
|
|
|
46
45
|
fastify.get('/number', opts, function (req, reply) {
|
|
47
46
|
reply.code(201).send({ hello: 55 })
|
|
48
47
|
})
|
|
49
|
-
t.
|
|
48
|
+
t.assert.ok(true)
|
|
50
49
|
} catch (e) {
|
|
51
|
-
t.fail()
|
|
50
|
+
t.assert.fail()
|
|
52
51
|
}
|
|
53
52
|
})
|
|
54
53
|
|
|
@@ -59,9 +58,9 @@ test('wrong object for schema - output', t => {
|
|
|
59
58
|
// will send { }
|
|
60
59
|
reply.code(201).send({ hello: 'world' })
|
|
61
60
|
})
|
|
62
|
-
t.
|
|
61
|
+
t.assert.ok(true)
|
|
63
62
|
} catch (e) {
|
|
64
|
-
t.fail()
|
|
63
|
+
t.assert.fail()
|
|
65
64
|
}
|
|
66
65
|
})
|
|
67
66
|
|
|
@@ -72,9 +71,9 @@ test('empty response', t => {
|
|
|
72
71
|
fastify.get('/empty', opts, function (req, reply) {
|
|
73
72
|
reply.code(204).send()
|
|
74
73
|
})
|
|
75
|
-
t.
|
|
74
|
+
t.assert.ok(true)
|
|
76
75
|
} catch (e) {
|
|
77
|
-
t.fail()
|
|
76
|
+
t.assert.fail()
|
|
78
77
|
}
|
|
79
78
|
})
|
|
80
79
|
|
|
@@ -84,80 +83,60 @@ test('unlisted response code', t => {
|
|
|
84
83
|
fastify.get('/400', opts, function (req, reply) {
|
|
85
84
|
reply.code(400).send({ hello: 'DOOM' })
|
|
86
85
|
})
|
|
87
|
-
t.
|
|
86
|
+
t.assert.ok(true)
|
|
88
87
|
} catch (e) {
|
|
89
|
-
t.fail()
|
|
88
|
+
t.assert.fail()
|
|
90
89
|
}
|
|
91
90
|
})
|
|
92
91
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
92
|
+
test('start server and run tests', async (t) => {
|
|
93
|
+
await fastify.listen({ port: 0 })
|
|
94
|
+
const baseUrl = 'http://localhost:' + fastify.server.address().port
|
|
95
|
+
t.after(() => fastify.close())
|
|
96
96
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
t.error(err)
|
|
104
|
-
t.equal(response.statusCode, 200)
|
|
105
|
-
t.equal(response.headers['content-length'], '' + body.length)
|
|
106
|
-
t.same(JSON.parse(body), { hello: 'world' })
|
|
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
|
+
})
|
|
107
103
|
})
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
await test('shorthand - string get ok', async (t) => {
|
|
107
|
+
const { res, body } = await sgetAsync({ method: 'GET', url: `${baseUrl}/string` })
|
|
108
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
109
|
+
t.assert.strictEqual(res.headers['content-length'], '' + body.length)
|
|
110
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
108
111
|
})
|
|
109
112
|
|
|
110
|
-
test('shorthand - number get ok', t => {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}, (err, response, body) => {
|
|
116
|
-
t.error(err)
|
|
117
|
-
t.equal(response.statusCode, 201)
|
|
118
|
-
t.equal(response.headers['content-length'], '' + body.length)
|
|
119
|
-
t.same(JSON.parse(body), { hello: 55 })
|
|
120
|
-
})
|
|
113
|
+
await test('shorthand - number get ok', async (t) => {
|
|
114
|
+
const { res, body } = await sgetAsync({ method: 'GET', url: `${baseUrl}/number` })
|
|
115
|
+
t.assert.strictEqual(res.statusCode, 201)
|
|
116
|
+
t.assert.strictEqual(res.headers['content-length'], '' + body.length)
|
|
117
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 55 })
|
|
121
118
|
})
|
|
122
119
|
|
|
123
|
-
test('shorthand - wrong-object-for-schema', t => {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
t.equal(response.headers['content-length'], '' + body.length)
|
|
132
|
-
t.same(JSON.parse(body), {
|
|
133
|
-
statusCode: 500,
|
|
134
|
-
error: 'Internal Server Error',
|
|
135
|
-
message: 'The value "world" cannot be converted to a number.'
|
|
136
|
-
})
|
|
120
|
+
await test('shorthand - wrong-object-for-schema', async (t) => {
|
|
121
|
+
const { res, body } = await sgetAsync({ method: 'GET', url: `${baseUrl}/wrong-object-for-schema` })
|
|
122
|
+
t.assert.strictEqual(res.statusCode, 500)
|
|
123
|
+
t.assert.strictEqual(res.headers['content-length'], '' + body.length)
|
|
124
|
+
t.assert.deepStrictEqual(JSON.parse(body), {
|
|
125
|
+
statusCode: 500,
|
|
126
|
+
error: 'Internal Server Error',
|
|
127
|
+
message: 'The value "world" cannot be converted to a number.'
|
|
137
128
|
})
|
|
138
129
|
})
|
|
139
130
|
|
|
140
|
-
test('shorthand - empty', t => {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
method: 'GET',
|
|
144
|
-
url: 'http://localhost:' + fastify.server.address().port + '/empty'
|
|
145
|
-
}, (err, response, body) => {
|
|
146
|
-
t.error(err)
|
|
147
|
-
t.equal(response.statusCode, 204)
|
|
148
|
-
})
|
|
131
|
+
await test('shorthand - empty', async (t) => {
|
|
132
|
+
const { res } = await sgetAsync({ method: 'GET', url: `${baseUrl}/empty` })
|
|
133
|
+
t.assert.strictEqual(res.statusCode, 204)
|
|
149
134
|
})
|
|
150
135
|
|
|
151
|
-
test('shorthand - 400', t => {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}, (err, response, body) => {
|
|
157
|
-
t.error(err)
|
|
158
|
-
t.equal(response.statusCode, 400)
|
|
159
|
-
t.equal(response.headers['content-length'], '' + body.length)
|
|
160
|
-
t.same(JSON.parse(body), { hello: 'DOOM' })
|
|
161
|
-
})
|
|
136
|
+
await test('shorthand - 400', async (t) => {
|
|
137
|
+
const { res, body } = await sgetAsync({ method: 'GET', url: `${baseUrl}/400` })
|
|
138
|
+
t.assert.strictEqual(res.statusCode, 400)
|
|
139
|
+
t.assert.strictEqual(res.headers['content-length'], '' + body.length)
|
|
140
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'DOOM' })
|
|
162
141
|
})
|
|
163
142
|
})
|
package/test/patch.test.js
CHANGED