fastify 5.2.2 → 5.3.1
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/SPONSORS.md +0 -1
- package/docs/Guides/Ecosystem.md +2 -0
- package/docs/Reference/Decorators.md +199 -0
- package/docs/Reference/Errors.md +2 -0
- package/fastify.js +3 -2
- package/lib/decorate.js +18 -3
- package/lib/errors.js +4 -0
- package/lib/reply.js +17 -2
- package/lib/request.js +28 -2
- package/lib/validation.js +11 -1
- package/package.json +3 -3
- package/test/build/error-serializer.test.js +1 -2
- package/test/custom-parser.0.test.js +160 -129
- package/test/custom-parser.1.test.js +77 -63
- package/test/custom-parser.4.test.js +55 -38
- package/test/custom-querystring-parser.test.js +46 -28
- package/test/decorator.test.js +174 -4
- package/test/fastify-instance.test.js +12 -2
- package/test/hooks.on-listen.test.js +17 -14
- package/test/internals/errors.test.js +14 -1
- package/test/listen.2.test.js +4 -1
- package/test/logger/instantiation.test.js +89 -96
- package/test/logger/logging.test.js +116 -120
- package/test/logger/options.test.js +97 -99
- package/test/logger/request.test.js +66 -66
- package/test/schema-validation.test.js +120 -0
- package/test/server.test.js +175 -0
- package/test/stream.4.test.js +38 -33
- package/test/toolkit.js +31 -0
- package/test/types/instance.test-d.ts +3 -0
- package/test/types/reply.test-d.ts +1 -0
- package/test/types/request.test-d.ts +4 -0
- package/test/types/type-provider.test-d.ts +40 -0
- package/test/upgrade.test.js +3 -6
- package/types/instance.d.ts +2 -0
- package/types/reply.d.ts +1 -0
- package/types/request.d.ts +2 -0
- package/types/type-provider.d.ts +12 -3
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const fs = require('node:fs')
|
|
4
|
-
const
|
|
5
|
-
const test = t.test
|
|
4
|
+
const { test } = require('node:test')
|
|
6
5
|
const sget = require('simple-get').concat
|
|
7
6
|
const Fastify = require('../fastify')
|
|
8
7
|
const jsonParser = require('fast-json-body')
|
|
9
8
|
const { getServerUrl, plainTextParser } = require('./helper')
|
|
9
|
+
const { waitForCb } = require('./toolkit')
|
|
10
10
|
|
|
11
11
|
process.removeAllListeners('warning')
|
|
12
12
|
|
|
13
13
|
test('contentTypeParser method should exist', t => {
|
|
14
14
|
t.plan(1)
|
|
15
15
|
const fastify = Fastify()
|
|
16
|
-
t.ok(fastify.addContentTypeParser)
|
|
16
|
+
t.assert.ok(fastify.addContentTypeParser)
|
|
17
17
|
})
|
|
18
18
|
|
|
19
|
-
test('contentTypeParser should add a custom parser', t => {
|
|
19
|
+
test('contentTypeParser should add a custom parser', (t, mainTestDone) => {
|
|
20
20
|
t.plan(3)
|
|
21
21
|
const fastify = Fastify()
|
|
22
22
|
|
|
@@ -35,11 +35,13 @@ test('contentTypeParser should add a custom parser', t => {
|
|
|
35
35
|
})
|
|
36
36
|
|
|
37
37
|
fastify.listen({ port: 0 }, err => {
|
|
38
|
-
t.
|
|
38
|
+
t.assert.ifError(err)
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
const completion = waitForCb({ steps: 2 })
|
|
41
41
|
|
|
42
|
-
t.
|
|
42
|
+
t.after(() => fastify.close())
|
|
43
|
+
|
|
44
|
+
t.test('in POST', (t, testDone) => {
|
|
43
45
|
t.plan(3)
|
|
44
46
|
|
|
45
47
|
sget({
|
|
@@ -50,13 +52,15 @@ test('contentTypeParser should add a custom parser', t => {
|
|
|
50
52
|
'Content-Type': 'application/jsoff'
|
|
51
53
|
}
|
|
52
54
|
}, (err, response, body) => {
|
|
53
|
-
t.
|
|
54
|
-
t.
|
|
55
|
-
t.
|
|
55
|
+
t.assert.ifError(err)
|
|
56
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
57
|
+
t.assert.strictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
|
|
58
|
+
testDone()
|
|
59
|
+
completion.stepIn()
|
|
56
60
|
})
|
|
57
61
|
})
|
|
58
62
|
|
|
59
|
-
t.test('in OPTIONS', t => {
|
|
63
|
+
t.test('in OPTIONS', (t, testDone) => {
|
|
60
64
|
t.plan(3)
|
|
61
65
|
|
|
62
66
|
sget({
|
|
@@ -67,15 +71,18 @@ test('contentTypeParser should add a custom parser', t => {
|
|
|
67
71
|
'Content-Type': 'application/jsoff'
|
|
68
72
|
}
|
|
69
73
|
}, (err, response, body) => {
|
|
70
|
-
t.
|
|
71
|
-
t.
|
|
72
|
-
t.
|
|
74
|
+
t.assert.ifError(err)
|
|
75
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
76
|
+
t.assert.strictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
|
|
77
|
+
testDone()
|
|
78
|
+
completion.stepIn()
|
|
73
79
|
})
|
|
74
80
|
})
|
|
81
|
+
completion.patience.then(mainTestDone)
|
|
75
82
|
})
|
|
76
83
|
})
|
|
77
84
|
|
|
78
|
-
test('contentTypeParser should handle multiple custom parsers', t => {
|
|
85
|
+
test('contentTypeParser should handle multiple custom parsers', (t, testDone) => {
|
|
79
86
|
t.plan(7)
|
|
80
87
|
const fastify = Fastify()
|
|
81
88
|
|
|
@@ -97,8 +104,10 @@ test('contentTypeParser should handle multiple custom parsers', t => {
|
|
|
97
104
|
fastify.addContentTypeParser('application/ffosj', customParser)
|
|
98
105
|
|
|
99
106
|
fastify.listen({ port: 0 }, err => {
|
|
100
|
-
t.
|
|
101
|
-
t.
|
|
107
|
+
t.assert.ifError(err)
|
|
108
|
+
t.after(() => { fastify.close() })
|
|
109
|
+
|
|
110
|
+
const completion = waitForCb({ steps: 2 })
|
|
102
111
|
|
|
103
112
|
sget({
|
|
104
113
|
method: 'POST',
|
|
@@ -108,9 +117,10 @@ test('contentTypeParser should handle multiple custom parsers', t => {
|
|
|
108
117
|
'Content-Type': 'application/jsoff'
|
|
109
118
|
}
|
|
110
119
|
}, (err, response, body) => {
|
|
111
|
-
t.
|
|
112
|
-
t.
|
|
113
|
-
t.
|
|
120
|
+
t.assert.ifError(err)
|
|
121
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
122
|
+
t.assert.strictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
|
|
123
|
+
completion.stepIn()
|
|
114
124
|
})
|
|
115
125
|
|
|
116
126
|
sget({
|
|
@@ -121,14 +131,16 @@ test('contentTypeParser should handle multiple custom parsers', t => {
|
|
|
121
131
|
'Content-Type': 'application/ffosj'
|
|
122
132
|
}
|
|
123
133
|
}, (err, response, body) => {
|
|
124
|
-
t.
|
|
125
|
-
t.
|
|
126
|
-
t.
|
|
134
|
+
t.assert.ifError(err)
|
|
135
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
136
|
+
t.assert.strictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
|
|
137
|
+
completion.stepIn()
|
|
127
138
|
})
|
|
139
|
+
completion.patience.then(testDone)
|
|
128
140
|
})
|
|
129
141
|
})
|
|
130
142
|
|
|
131
|
-
test('contentTypeParser should handle an array of custom contentTypes', t => {
|
|
143
|
+
test('contentTypeParser should handle an array of custom contentTypes', (t, testDone) => {
|
|
132
144
|
t.plan(7)
|
|
133
145
|
const fastify = Fastify()
|
|
134
146
|
|
|
@@ -149,8 +161,10 @@ test('contentTypeParser should handle an array of custom contentTypes', t => {
|
|
|
149
161
|
fastify.addContentTypeParser(['application/jsoff', 'application/ffosj'], customParser)
|
|
150
162
|
|
|
151
163
|
fastify.listen({ port: 0 }, err => {
|
|
152
|
-
t.
|
|
153
|
-
t.
|
|
164
|
+
t.assert.ifError(err)
|
|
165
|
+
t.after(() => { fastify.close() })
|
|
166
|
+
|
|
167
|
+
const completion = waitForCb({ steps: 2 })
|
|
154
168
|
|
|
155
169
|
sget({
|
|
156
170
|
method: 'POST',
|
|
@@ -160,9 +174,10 @@ test('contentTypeParser should handle an array of custom contentTypes', t => {
|
|
|
160
174
|
'Content-Type': 'application/jsoff'
|
|
161
175
|
}
|
|
162
176
|
}, (err, response, body) => {
|
|
163
|
-
t.
|
|
164
|
-
t.
|
|
165
|
-
t.
|
|
177
|
+
t.assert.ifError(err)
|
|
178
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
179
|
+
t.assert.strictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
|
|
180
|
+
completion.stepIn()
|
|
166
181
|
})
|
|
167
182
|
|
|
168
183
|
sget({
|
|
@@ -173,14 +188,16 @@ test('contentTypeParser should handle an array of custom contentTypes', t => {
|
|
|
173
188
|
'Content-Type': 'application/ffosj'
|
|
174
189
|
}
|
|
175
190
|
}, (err, response, body) => {
|
|
176
|
-
t.
|
|
177
|
-
t.
|
|
178
|
-
t.
|
|
191
|
+
t.assert.ifError(err)
|
|
192
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
193
|
+
t.assert.strictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
|
|
194
|
+
completion.stepIn()
|
|
179
195
|
})
|
|
196
|
+
completion.patience.then(testDone)
|
|
180
197
|
})
|
|
181
198
|
})
|
|
182
199
|
|
|
183
|
-
test('contentTypeParser should handle errors', t => {
|
|
200
|
+
test('contentTypeParser should handle errors', (t, testDone) => {
|
|
184
201
|
t.plan(3)
|
|
185
202
|
const fastify = Fastify()
|
|
186
203
|
|
|
@@ -193,7 +210,7 @@ test('contentTypeParser should handle errors', t => {
|
|
|
193
210
|
})
|
|
194
211
|
|
|
195
212
|
fastify.listen({ port: 0 }, err => {
|
|
196
|
-
t.
|
|
213
|
+
t.assert.ifError(err)
|
|
197
214
|
|
|
198
215
|
sget({
|
|
199
216
|
method: 'POST',
|
|
@@ -203,25 +220,26 @@ test('contentTypeParser should handle errors', t => {
|
|
|
203
220
|
'Content-Type': 'application/jsoff'
|
|
204
221
|
}
|
|
205
222
|
}, (err, response, body) => {
|
|
206
|
-
t.
|
|
207
|
-
t.
|
|
223
|
+
t.assert.ifError(err)
|
|
224
|
+
t.assert.strictEqual(response.statusCode, 500)
|
|
208
225
|
fastify.close()
|
|
226
|
+
testDone()
|
|
209
227
|
})
|
|
210
228
|
})
|
|
211
229
|
})
|
|
212
230
|
|
|
213
|
-
test('contentTypeParser should support encapsulation', t => {
|
|
231
|
+
test('contentTypeParser should support encapsulation', (t, testDone) => {
|
|
214
232
|
t.plan(6)
|
|
215
233
|
const fastify = Fastify()
|
|
216
234
|
|
|
217
235
|
fastify.register((instance, opts, done) => {
|
|
218
236
|
instance.addContentTypeParser('application/jsoff', () => {})
|
|
219
|
-
t.ok(instance.hasContentTypeParser('application/jsoff'))
|
|
237
|
+
t.assert.ok(instance.hasContentTypeParser('application/jsoff'))
|
|
220
238
|
|
|
221
239
|
instance.register((instance, opts, done) => {
|
|
222
240
|
instance.addContentTypeParser('application/ffosj', () => {})
|
|
223
|
-
t.ok(instance.hasContentTypeParser('application/jsoff'))
|
|
224
|
-
t.ok(instance.hasContentTypeParser('application/ffosj'))
|
|
241
|
+
t.assert.ok(instance.hasContentTypeParser('application/jsoff'))
|
|
242
|
+
t.assert.ok(instance.hasContentTypeParser('application/ffosj'))
|
|
225
243
|
done()
|
|
226
244
|
})
|
|
227
245
|
|
|
@@ -229,13 +247,14 @@ test('contentTypeParser should support encapsulation', t => {
|
|
|
229
247
|
})
|
|
230
248
|
|
|
231
249
|
fastify.ready(err => {
|
|
232
|
-
t.
|
|
233
|
-
t.
|
|
234
|
-
t.
|
|
250
|
+
t.assert.ifError(err)
|
|
251
|
+
t.assert.ok(!fastify.hasContentTypeParser('application/jsoff'))
|
|
252
|
+
t.assert.ok(!fastify.hasContentTypeParser('application/ffosj'))
|
|
253
|
+
testDone()
|
|
235
254
|
})
|
|
236
255
|
})
|
|
237
256
|
|
|
238
|
-
test('contentTypeParser should support encapsulation, second try', t => {
|
|
257
|
+
test('contentTypeParser should support encapsulation, second try', (t, testDone) => {
|
|
239
258
|
t.plan(4)
|
|
240
259
|
const fastify = Fastify()
|
|
241
260
|
|
|
@@ -254,7 +273,7 @@ test('contentTypeParser should support encapsulation, second try', t => {
|
|
|
254
273
|
})
|
|
255
274
|
|
|
256
275
|
fastify.listen({ port: 0 }, err => {
|
|
257
|
-
t.
|
|
276
|
+
t.assert.ifError(err)
|
|
258
277
|
|
|
259
278
|
sget({
|
|
260
279
|
method: 'POST',
|
|
@@ -264,15 +283,16 @@ test('contentTypeParser should support encapsulation, second try', t => {
|
|
|
264
283
|
'Content-Type': 'application/jsoff'
|
|
265
284
|
}
|
|
266
285
|
}, (err, response, body) => {
|
|
267
|
-
t.
|
|
268
|
-
t.
|
|
269
|
-
t.
|
|
286
|
+
t.assert.ifError(err)
|
|
287
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
288
|
+
t.assert.strictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
|
|
270
289
|
fastify.close()
|
|
290
|
+
testDone()
|
|
271
291
|
})
|
|
272
292
|
})
|
|
273
293
|
})
|
|
274
294
|
|
|
275
|
-
test('contentTypeParser shouldn\'t support request with undefined "Content-Type"', t => {
|
|
295
|
+
test('contentTypeParser shouldn\'t support request with undefined "Content-Type"', (t, testDone) => {
|
|
276
296
|
t.plan(3)
|
|
277
297
|
const fastify = Fastify()
|
|
278
298
|
|
|
@@ -287,7 +307,7 @@ test('contentTypeParser shouldn\'t support request with undefined "Content-Type"
|
|
|
287
307
|
})
|
|
288
308
|
|
|
289
309
|
fastify.listen({ port: 0 }, err => {
|
|
290
|
-
t.
|
|
310
|
+
t.assert.ifError(err)
|
|
291
311
|
|
|
292
312
|
sget({
|
|
293
313
|
method: 'POST',
|
|
@@ -297,9 +317,10 @@ test('contentTypeParser shouldn\'t support request with undefined "Content-Type"
|
|
|
297
317
|
// 'Content-Type': undefined
|
|
298
318
|
}
|
|
299
319
|
}, (err, response, body) => {
|
|
300
|
-
t.
|
|
301
|
-
t.
|
|
320
|
+
t.assert.ifError(err)
|
|
321
|
+
t.assert.strictEqual(response.statusCode, 415)
|
|
302
322
|
fastify.close()
|
|
323
|
+
testDone()
|
|
303
324
|
})
|
|
304
325
|
})
|
|
305
326
|
})
|
|
@@ -310,10 +331,10 @@ test('the content type should be a string or RegExp', t => {
|
|
|
310
331
|
|
|
311
332
|
try {
|
|
312
333
|
fastify.addContentTypeParser(null, () => {})
|
|
313
|
-
t.fail()
|
|
334
|
+
t.assert.fail()
|
|
314
335
|
} catch (err) {
|
|
315
|
-
t.
|
|
316
|
-
t.
|
|
336
|
+
t.assert.strictEqual(err.code, 'FST_ERR_CTP_INVALID_TYPE')
|
|
337
|
+
t.assert.strictEqual(err.message, 'The content type should be a string or a RegExp')
|
|
317
338
|
}
|
|
318
339
|
})
|
|
319
340
|
|
|
@@ -323,10 +344,10 @@ test('the content type cannot be an empty string', t => {
|
|
|
323
344
|
|
|
324
345
|
try {
|
|
325
346
|
fastify.addContentTypeParser('', () => {})
|
|
326
|
-
t.fail()
|
|
347
|
+
t.assert.fail()
|
|
327
348
|
} catch (err) {
|
|
328
|
-
t.
|
|
329
|
-
t.
|
|
349
|
+
t.assert.strictEqual(err.code, 'FST_ERR_CTP_EMPTY_TYPE')
|
|
350
|
+
t.assert.strictEqual(err.message, 'The content type cannot be an empty string')
|
|
330
351
|
}
|
|
331
352
|
})
|
|
332
353
|
|
|
@@ -336,14 +357,14 @@ test('the content type handler should be a function', t => {
|
|
|
336
357
|
|
|
337
358
|
try {
|
|
338
359
|
fastify.addContentTypeParser('aaa', null)
|
|
339
|
-
t.fail()
|
|
360
|
+
t.assert.fail()
|
|
340
361
|
} catch (err) {
|
|
341
|
-
t.
|
|
342
|
-
t.
|
|
362
|
+
t.assert.strictEqual(err.code, 'FST_ERR_CTP_INVALID_HANDLER')
|
|
363
|
+
t.assert.strictEqual(err.message, 'The content type handler should be a function')
|
|
343
364
|
}
|
|
344
365
|
})
|
|
345
366
|
|
|
346
|
-
test('catch all content type parser', t => {
|
|
367
|
+
test('catch all content type parser', (t, testDone) => {
|
|
347
368
|
t.plan(7)
|
|
348
369
|
const fastify = Fastify()
|
|
349
370
|
|
|
@@ -360,7 +381,7 @@ test('catch all content type parser', t => {
|
|
|
360
381
|
})
|
|
361
382
|
|
|
362
383
|
fastify.listen({ port: 0 }, err => {
|
|
363
|
-
t.
|
|
384
|
+
t.assert.ifError(err)
|
|
364
385
|
|
|
365
386
|
sget({
|
|
366
387
|
method: 'POST',
|
|
@@ -370,9 +391,9 @@ test('catch all content type parser', t => {
|
|
|
370
391
|
'Content-Type': 'application/jsoff'
|
|
371
392
|
}
|
|
372
393
|
}, (err, response, body) => {
|
|
373
|
-
t.
|
|
374
|
-
t.
|
|
375
|
-
t.
|
|
394
|
+
t.assert.ifError(err)
|
|
395
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
396
|
+
t.assert.strictEqual(body.toString(), 'hello')
|
|
376
397
|
|
|
377
398
|
sget({
|
|
378
399
|
method: 'POST',
|
|
@@ -382,16 +403,17 @@ test('catch all content type parser', t => {
|
|
|
382
403
|
'Content-Type': 'very-weird-content-type'
|
|
383
404
|
}
|
|
384
405
|
}, (err, response, body) => {
|
|
385
|
-
t.
|
|
386
|
-
t.
|
|
387
|
-
t.
|
|
406
|
+
t.assert.ifError(err)
|
|
407
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
408
|
+
t.assert.strictEqual(body.toString(), 'hello')
|
|
388
409
|
fastify.close()
|
|
410
|
+
testDone()
|
|
389
411
|
})
|
|
390
412
|
})
|
|
391
413
|
})
|
|
392
414
|
})
|
|
393
415
|
|
|
394
|
-
test('catch all content type parser should not interfere with other conte type parsers', t => {
|
|
416
|
+
test('catch all content type parser should not interfere with other conte type parsers', (t, testDone) => {
|
|
395
417
|
t.plan(7)
|
|
396
418
|
const fastify = Fastify()
|
|
397
419
|
|
|
@@ -414,7 +436,7 @@ test('catch all content type parser should not interfere with other conte type p
|
|
|
414
436
|
})
|
|
415
437
|
|
|
416
438
|
fastify.listen({ port: 0 }, err => {
|
|
417
|
-
t.
|
|
439
|
+
t.assert.ifError(err)
|
|
418
440
|
|
|
419
441
|
sget({
|
|
420
442
|
method: 'POST',
|
|
@@ -424,9 +446,9 @@ test('catch all content type parser should not interfere with other conte type p
|
|
|
424
446
|
'Content-Type': 'application/jsoff'
|
|
425
447
|
}
|
|
426
448
|
}, (err, response, body) => {
|
|
427
|
-
t.
|
|
428
|
-
t.
|
|
429
|
-
t.
|
|
449
|
+
t.assert.ifError(err)
|
|
450
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
451
|
+
t.assert.strictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
|
|
430
452
|
|
|
431
453
|
sget({
|
|
432
454
|
method: 'POST',
|
|
@@ -436,22 +458,23 @@ test('catch all content type parser should not interfere with other conte type p
|
|
|
436
458
|
'Content-Type': 'very-weird-content-type'
|
|
437
459
|
}
|
|
438
460
|
}, (err, response, body) => {
|
|
439
|
-
t.
|
|
440
|
-
t.
|
|
441
|
-
t.
|
|
461
|
+
t.assert.ifError(err)
|
|
462
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
463
|
+
t.assert.strictEqual(body.toString(), 'hello')
|
|
442
464
|
fastify.close()
|
|
465
|
+
testDone()
|
|
443
466
|
})
|
|
444
467
|
})
|
|
445
468
|
})
|
|
446
469
|
})
|
|
447
470
|
|
|
448
471
|
// Issue 492 https://github.com/fastify/fastify/issues/492
|
|
449
|
-
test('\'*\' catch undefined Content-Type requests', t => {
|
|
472
|
+
test('\'*\' catch undefined Content-Type requests', (t, testDone) => {
|
|
450
473
|
t.plan(4)
|
|
451
474
|
|
|
452
475
|
const fastify = Fastify()
|
|
453
476
|
|
|
454
|
-
t.
|
|
477
|
+
t.after(() => fastify.close())
|
|
455
478
|
|
|
456
479
|
fastify.addContentTypeParser('*', function (req, payload, done) {
|
|
457
480
|
let data = ''
|
|
@@ -467,7 +490,7 @@ test('\'*\' catch undefined Content-Type requests', t => {
|
|
|
467
490
|
})
|
|
468
491
|
|
|
469
492
|
fastify.listen({ port: 0 }, function (err) {
|
|
470
|
-
t.
|
|
493
|
+
t.assert.ifError(err)
|
|
471
494
|
|
|
472
495
|
const fileStream = fs.createReadStream(__filename)
|
|
473
496
|
|
|
@@ -476,37 +499,39 @@ test('\'*\' catch undefined Content-Type requests', t => {
|
|
|
476
499
|
url: getServerUrl(fastify) + '/',
|
|
477
500
|
body: fileStream
|
|
478
501
|
}, (err, response, body) => {
|
|
479
|
-
t.
|
|
480
|
-
t.
|
|
481
|
-
t.
|
|
502
|
+
t.assert.ifError(err)
|
|
503
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
504
|
+
t.assert.strictEqual(body + '', fs.readFileSync(__filename).toString())
|
|
505
|
+
testDone()
|
|
482
506
|
})
|
|
483
507
|
})
|
|
484
508
|
})
|
|
485
509
|
|
|
486
|
-
test('cannot add custom parser after binding', t => {
|
|
510
|
+
test('cannot add custom parser after binding', (t, testDone) => {
|
|
487
511
|
t.plan(2)
|
|
488
512
|
|
|
489
513
|
const fastify = Fastify()
|
|
490
514
|
|
|
491
|
-
t.
|
|
515
|
+
t.after(() => fastify.close())
|
|
492
516
|
|
|
493
517
|
fastify.post('/', (req, res) => {
|
|
494
518
|
res.type('text/plain').send(req.body)
|
|
495
519
|
})
|
|
496
520
|
|
|
497
521
|
fastify.listen({ port: 0 }, function (err) {
|
|
498
|
-
t.
|
|
522
|
+
t.assert.ifError(err)
|
|
499
523
|
|
|
500
524
|
try {
|
|
501
525
|
fastify.addContentTypeParser('*', () => {})
|
|
502
|
-
t.fail()
|
|
526
|
+
t.assert.fail()
|
|
503
527
|
} catch (e) {
|
|
504
|
-
t.
|
|
528
|
+
t.assert.ok(true)
|
|
529
|
+
testDone()
|
|
505
530
|
}
|
|
506
531
|
})
|
|
507
532
|
})
|
|
508
533
|
|
|
509
|
-
test('Can override the default json parser', t => {
|
|
534
|
+
test('Can override the default json parser', (t, testDone) => {
|
|
510
535
|
t.plan(5)
|
|
511
536
|
const fastify = Fastify()
|
|
512
537
|
|
|
@@ -515,14 +540,14 @@ test('Can override the default json parser', t => {
|
|
|
515
540
|
})
|
|
516
541
|
|
|
517
542
|
fastify.addContentTypeParser('application/json', function (req, payload, done) {
|
|
518
|
-
t.ok('called')
|
|
543
|
+
t.assert.ok('called')
|
|
519
544
|
jsonParser(payload, function (err, body) {
|
|
520
545
|
done(err, body)
|
|
521
546
|
})
|
|
522
547
|
})
|
|
523
548
|
|
|
524
549
|
fastify.listen({ port: 0 }, err => {
|
|
525
|
-
t.
|
|
550
|
+
t.assert.ifError(err)
|
|
526
551
|
|
|
527
552
|
sget({
|
|
528
553
|
method: 'POST',
|
|
@@ -532,15 +557,16 @@ test('Can override the default json parser', t => {
|
|
|
532
557
|
'Content-Type': 'application/json'
|
|
533
558
|
}
|
|
534
559
|
}, (err, response, body) => {
|
|
535
|
-
t.
|
|
536
|
-
t.
|
|
537
|
-
t.
|
|
560
|
+
t.assert.ifError(err)
|
|
561
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
562
|
+
t.assert.strictEqual(body.toString(), '{"hello":"world"}')
|
|
538
563
|
fastify.close()
|
|
564
|
+
testDone()
|
|
539
565
|
})
|
|
540
566
|
})
|
|
541
567
|
})
|
|
542
568
|
|
|
543
|
-
test('Can override the default plain text parser', t => {
|
|
569
|
+
test('Can override the default plain text parser', (t, testDone) => {
|
|
544
570
|
t.plan(5)
|
|
545
571
|
const fastify = Fastify()
|
|
546
572
|
|
|
@@ -549,14 +575,14 @@ test('Can override the default plain text parser', t => {
|
|
|
549
575
|
})
|
|
550
576
|
|
|
551
577
|
fastify.addContentTypeParser('text/plain', function (req, payload, done) {
|
|
552
|
-
t.ok('called')
|
|
578
|
+
t.assert.ok('called')
|
|
553
579
|
plainTextParser(payload, function (err, body) {
|
|
554
580
|
done(err, body)
|
|
555
581
|
})
|
|
556
582
|
})
|
|
557
583
|
|
|
558
584
|
fastify.listen({ port: 0 }, err => {
|
|
559
|
-
t.
|
|
585
|
+
t.assert.ifError(err)
|
|
560
586
|
|
|
561
587
|
sget({
|
|
562
588
|
method: 'POST',
|
|
@@ -566,21 +592,22 @@ test('Can override the default plain text parser', t => {
|
|
|
566
592
|
'Content-Type': 'text/plain'
|
|
567
593
|
}
|
|
568
594
|
}, (err, response, body) => {
|
|
569
|
-
t.
|
|
570
|
-
t.
|
|
571
|
-
t.
|
|
595
|
+
t.assert.ifError(err)
|
|
596
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
597
|
+
t.assert.strictEqual(body.toString(), 'hello world')
|
|
572
598
|
fastify.close()
|
|
599
|
+
testDone()
|
|
573
600
|
})
|
|
574
601
|
})
|
|
575
602
|
})
|
|
576
603
|
|
|
577
|
-
test('Can override the default json parser in a plugin', t => {
|
|
604
|
+
test('Can override the default json parser in a plugin', (t, testDone) => {
|
|
578
605
|
t.plan(5)
|
|
579
606
|
const fastify = Fastify()
|
|
580
607
|
|
|
581
608
|
fastify.register((instance, opts, done) => {
|
|
582
609
|
instance.addContentTypeParser('application/json', function (req, payload, done) {
|
|
583
|
-
t.ok('called')
|
|
610
|
+
t.assert.ok('called')
|
|
584
611
|
jsonParser(payload, function (err, body) {
|
|
585
612
|
done(err, body)
|
|
586
613
|
})
|
|
@@ -594,7 +621,7 @@ test('Can override the default json parser in a plugin', t => {
|
|
|
594
621
|
})
|
|
595
622
|
|
|
596
623
|
fastify.listen({ port: 0 }, err => {
|
|
597
|
-
t.
|
|
624
|
+
t.assert.ifError(err)
|
|
598
625
|
|
|
599
626
|
sget({
|
|
600
627
|
method: 'POST',
|
|
@@ -604,10 +631,11 @@ test('Can override the default json parser in a plugin', t => {
|
|
|
604
631
|
'Content-Type': 'application/json'
|
|
605
632
|
}
|
|
606
633
|
}, (err, response, body) => {
|
|
607
|
-
t.
|
|
608
|
-
t.
|
|
609
|
-
t.
|
|
634
|
+
t.assert.ifError(err)
|
|
635
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
636
|
+
t.assert.strictEqual(body.toString(), '{"hello":"world"}')
|
|
610
637
|
fastify.close()
|
|
638
|
+
testDone()
|
|
611
639
|
})
|
|
612
640
|
})
|
|
613
641
|
})
|
|
@@ -624,14 +652,14 @@ test('Can\'t override the json parser multiple times', t => {
|
|
|
624
652
|
|
|
625
653
|
try {
|
|
626
654
|
fastify.addContentTypeParser('application/json', function (req, payload, done) {
|
|
627
|
-
t.ok('called')
|
|
655
|
+
t.assert.ok('called')
|
|
628
656
|
jsonParser(payload, function (err, body) {
|
|
629
657
|
done(err, body)
|
|
630
658
|
})
|
|
631
659
|
})
|
|
632
660
|
} catch (err) {
|
|
633
|
-
t.
|
|
634
|
-
t.
|
|
661
|
+
t.assert.strictEqual(err.code, 'FST_ERR_CTP_ALREADY_PRESENT')
|
|
662
|
+
t.assert.strictEqual(err.message, 'Content type parser \'application/json\' already present.')
|
|
635
663
|
}
|
|
636
664
|
})
|
|
637
665
|
|
|
@@ -647,18 +675,18 @@ test('Can\'t override the plain text parser multiple times', t => {
|
|
|
647
675
|
|
|
648
676
|
try {
|
|
649
677
|
fastify.addContentTypeParser('text/plain', function (req, payload, done) {
|
|
650
|
-
t.ok('called')
|
|
678
|
+
t.assert.ok('called')
|
|
651
679
|
plainTextParser(payload, function (err, body) {
|
|
652
680
|
done(err, body)
|
|
653
681
|
})
|
|
654
682
|
})
|
|
655
683
|
} catch (err) {
|
|
656
|
-
t.
|
|
657
|
-
t.
|
|
684
|
+
t.assert.strictEqual(err.code, 'FST_ERR_CTP_ALREADY_PRESENT')
|
|
685
|
+
t.assert.strictEqual(err.message, 'Content type parser \'text/plain\' already present.')
|
|
658
686
|
}
|
|
659
687
|
})
|
|
660
688
|
|
|
661
|
-
test('Should get the body as string', t => {
|
|
689
|
+
test('Should get the body as string', (t, testDone) => {
|
|
662
690
|
t.plan(6)
|
|
663
691
|
const fastify = Fastify()
|
|
664
692
|
|
|
@@ -667,8 +695,8 @@ test('Should get the body as string', t => {
|
|
|
667
695
|
})
|
|
668
696
|
|
|
669
697
|
fastify.addContentTypeParser('application/json', { parseAs: 'string' }, function (req, body, done) {
|
|
670
|
-
t.ok('called')
|
|
671
|
-
t.ok(typeof body === 'string')
|
|
698
|
+
t.assert.ok('called')
|
|
699
|
+
t.assert.ok(typeof body === 'string')
|
|
672
700
|
try {
|
|
673
701
|
const json = JSON.parse(body)
|
|
674
702
|
done(null, json)
|
|
@@ -679,7 +707,7 @@ test('Should get the body as string', t => {
|
|
|
679
707
|
})
|
|
680
708
|
|
|
681
709
|
fastify.listen({ port: 0 }, err => {
|
|
682
|
-
t.
|
|
710
|
+
t.assert.ifError(err)
|
|
683
711
|
|
|
684
712
|
sget({
|
|
685
713
|
method: 'POST',
|
|
@@ -689,15 +717,16 @@ test('Should get the body as string', t => {
|
|
|
689
717
|
'Content-Type': 'application/json'
|
|
690
718
|
}
|
|
691
719
|
}, (err, response, body) => {
|
|
692
|
-
t.
|
|
693
|
-
t.
|
|
694
|
-
t.
|
|
720
|
+
t.assert.ifError(err)
|
|
721
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
722
|
+
t.assert.strictEqual(body.toString(), '{"hello":"world"}')
|
|
695
723
|
fastify.close()
|
|
724
|
+
testDone()
|
|
696
725
|
})
|
|
697
726
|
})
|
|
698
727
|
})
|
|
699
728
|
|
|
700
|
-
test('Should return defined body with no custom parser defined and content type = \'text/plain\'', t => {
|
|
729
|
+
test('Should return defined body with no custom parser defined and content type = \'text/plain\'', (t, testDone) => {
|
|
701
730
|
t.plan(4)
|
|
702
731
|
const fastify = Fastify()
|
|
703
732
|
|
|
@@ -706,7 +735,7 @@ test('Should return defined body with no custom parser defined and content type
|
|
|
706
735
|
})
|
|
707
736
|
|
|
708
737
|
fastify.listen({ port: 0 }, err => {
|
|
709
|
-
t.
|
|
738
|
+
t.assert.ifError(err)
|
|
710
739
|
|
|
711
740
|
sget({
|
|
712
741
|
method: 'POST',
|
|
@@ -716,15 +745,16 @@ test('Should return defined body with no custom parser defined and content type
|
|
|
716
745
|
'Content-Type': 'text/plain'
|
|
717
746
|
}
|
|
718
747
|
}, (err, response, body) => {
|
|
719
|
-
t.
|
|
720
|
-
t.
|
|
721
|
-
t.
|
|
748
|
+
t.assert.ifError(err)
|
|
749
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
750
|
+
t.assert.strictEqual(body.toString(), 'hello world')
|
|
722
751
|
fastify.close()
|
|
752
|
+
testDone()
|
|
723
753
|
})
|
|
724
754
|
})
|
|
725
755
|
})
|
|
726
756
|
|
|
727
|
-
test('Should have typeof body object with no custom parser defined, no body defined and content type = \'text/plain\'', t => {
|
|
757
|
+
test('Should have typeof body object with no custom parser defined, no body defined and content type = \'text/plain\'', (t, testDone) => {
|
|
728
758
|
t.plan(4)
|
|
729
759
|
const fastify = Fastify()
|
|
730
760
|
|
|
@@ -733,7 +763,7 @@ test('Should have typeof body object with no custom parser defined, no body defi
|
|
|
733
763
|
})
|
|
734
764
|
|
|
735
765
|
fastify.listen({ port: 0 }, err => {
|
|
736
|
-
t.
|
|
766
|
+
t.assert.ifError(err)
|
|
737
767
|
|
|
738
768
|
sget({
|
|
739
769
|
method: 'POST',
|
|
@@ -742,10 +772,11 @@ test('Should have typeof body object with no custom parser defined, no body defi
|
|
|
742
772
|
'Content-Type': 'text/plain'
|
|
743
773
|
}
|
|
744
774
|
}, (err, response, body) => {
|
|
745
|
-
t.
|
|
746
|
-
t.
|
|
747
|
-
t.
|
|
775
|
+
t.assert.ifError(err)
|
|
776
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
777
|
+
t.assert.strictEqual(typeof body, 'object')
|
|
748
778
|
fastify.close()
|
|
779
|
+
testDone()
|
|
749
780
|
})
|
|
750
781
|
})
|
|
751
782
|
})
|