fastify 4.23.2 → 4.24.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/README.md +1 -1
- package/docs/Guides/Ecosystem.md +6 -0
- package/docs/Reference/Hooks.md +1 -0
- package/docs/Reference/Plugins.md +1 -1
- package/docs/Reference/Reply.md +4 -3
- package/docs/Reference/Request.md +3 -2
- package/docs/Reference/Server.md +31 -3
- package/docs/Reference/Type-Providers.md +2 -2
- package/docs/Reference/TypeScript.md +21 -7
- package/fastify.d.ts +2 -2
- package/fastify.js +8 -1
- package/lib/contentTypeParser.js +1 -1
- package/lib/reply.js +20 -3
- package/lib/reqIdGenFactory.js +15 -9
- package/lib/request.js +1 -1
- package/lib/route.js +28 -7
- package/lib/schemas.js +3 -3
- package/lib/warnings.js +3 -1
- package/package.json +33 -33
- package/test/404s.test.js +31 -39
- package/test/async-await.test.js +1 -1
- package/test/async-dispose.test.js +21 -0
- package/test/build-certificate.js +90 -1
- package/test/close-pipelining.test.js +5 -5
- package/test/close.test.js +1 -5
- package/test/constrained-routes.test.js +127 -3
- package/test/custom-http-server.test.js +94 -91
- package/test/custom-parser.0.test.js +21 -47
- package/test/custom-parser.1.test.js +10 -732
- package/test/custom-parser.2.test.js +102 -0
- package/test/custom-parser.3.test.js +245 -0
- package/test/custom-parser.4.test.js +239 -0
- package/test/custom-parser.5.test.js +149 -0
- package/test/head.test.js +204 -0
- package/test/helper.js +30 -8
- package/test/hooks-async.test.js +163 -13
- package/test/hooks.on-listen.test.js +7 -6
- package/test/hooks.test.js +4 -15
- package/test/http2/closing.test.js +7 -15
- package/test/https/custom-https-server.test.js +43 -40
- package/test/input-validation.js +3 -3
- package/test/internals/reply.test.js +33 -4
- package/test/listen.1.test.js +101 -0
- package/test/listen.2.test.js +103 -0
- package/test/listen.3.test.js +87 -0
- package/test/listen.4.test.js +164 -0
- package/test/listen.deprecated.test.js +3 -9
- package/test/logger/instantiation.test.js +347 -0
- package/test/logger/logger-test-utils.js +47 -0
- package/test/logger/logging.test.js +406 -0
- package/test/logger/options.test.js +500 -0
- package/test/logger/request.test.js +292 -0
- package/test/logger/response.test.js +184 -0
- package/test/plugin.1.test.js +249 -0
- package/test/plugin.2.test.js +328 -0
- package/test/plugin.3.test.js +311 -0
- package/test/plugin.4.test.js +416 -0
- package/test/reply-code.test.js +64 -0
- package/test/reply-trailers.test.js +1 -2
- package/test/route.1.test.js +309 -0
- package/test/route.2.test.js +99 -0
- package/test/route.3.test.js +205 -0
- package/test/route.4.test.js +131 -0
- package/test/route.5.test.js +230 -0
- package/test/route.6.test.js +306 -0
- package/test/route.7.test.js +370 -0
- package/test/route.8.test.js +142 -0
- package/test/stream.1.test.js +108 -0
- package/test/stream.2.test.js +119 -0
- package/test/stream.3.test.js +192 -0
- package/test/stream.4.test.js +223 -0
- package/test/stream.5.test.js +194 -0
- package/test/trust-proxy.test.js +2 -4
- package/test/types/reply.test-d.ts +3 -3
- package/test/types/request.test-d.ts +9 -9
- package/test/types/type-provider.test-d.ts +89 -0
- package/test/types/using.test-d.ts +14 -0
- package/test/upgrade.test.js +3 -3
- package/types/context.d.ts +9 -2
- package/types/instance.d.ts +4 -1
- package/types/plugin.d.ts +2 -1
- package/types/reply.d.ts +2 -2
- package/types/request.d.ts +3 -3
- package/types/route.d.ts +5 -5
- package/test/listen.test.js +0 -427
- package/test/plugin.test.js +0 -1275
- package/test/route.test.js +0 -1762
- package/test/serial/logger.0.test.js +0 -866
- package/test/serial/logger.1.test.js +0 -862
- package/test/stream.test.js +0 -816
- /package/test/{serial → logger}/tap-parallel-not-ok +0 -0
|
@@ -4,34 +4,8 @@ const t = require('tap')
|
|
|
4
4
|
const test = t.test
|
|
5
5
|
const sget = require('simple-get').concat
|
|
6
6
|
const Fastify = require('../fastify')
|
|
7
|
-
|
|
8
7
|
const jsonParser = require('fast-json-body')
|
|
9
|
-
|
|
10
|
-
function plainTextParser (request, callback) {
|
|
11
|
-
let body = ''
|
|
12
|
-
request.setEncoding('utf8')
|
|
13
|
-
request.on('error', onError)
|
|
14
|
-
request.on('data', onData)
|
|
15
|
-
request.on('end', onEnd)
|
|
16
|
-
function onError (err) {
|
|
17
|
-
callback(err, null)
|
|
18
|
-
}
|
|
19
|
-
function onData (chunk) {
|
|
20
|
-
body += chunk
|
|
21
|
-
}
|
|
22
|
-
function onEnd () {
|
|
23
|
-
callback(null, body)
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function getUrl (app) {
|
|
28
|
-
const { address, port } = app.server.address()
|
|
29
|
-
if (address === '::1') {
|
|
30
|
-
return `http://[${address}]:${port}`
|
|
31
|
-
} else {
|
|
32
|
-
return `http://${address}:${port}`
|
|
33
|
-
}
|
|
34
|
-
}
|
|
8
|
+
const { getServerUrl } = require('./helper')
|
|
35
9
|
|
|
36
10
|
process.removeAllListeners('warning')
|
|
37
11
|
|
|
@@ -48,7 +22,7 @@ test('Should have typeof body object with no custom parser defined, null body an
|
|
|
48
22
|
|
|
49
23
|
sget({
|
|
50
24
|
method: 'POST',
|
|
51
|
-
url:
|
|
25
|
+
url: getServerUrl(fastify),
|
|
52
26
|
body: null,
|
|
53
27
|
headers: {
|
|
54
28
|
'Content-Type': 'text/plain'
|
|
@@ -75,7 +49,7 @@ test('Should have typeof body object with no custom parser defined, undefined bo
|
|
|
75
49
|
|
|
76
50
|
sget({
|
|
77
51
|
method: 'POST',
|
|
78
|
-
url:
|
|
52
|
+
url: getServerUrl(fastify),
|
|
79
53
|
body: undefined,
|
|
80
54
|
headers: {
|
|
81
55
|
'Content-Type': 'text/plain'
|
|
@@ -114,7 +88,7 @@ test('Should get the body as string', t => {
|
|
|
114
88
|
|
|
115
89
|
sget({
|
|
116
90
|
method: 'POST',
|
|
117
|
-
url:
|
|
91
|
+
url: getServerUrl(fastify),
|
|
118
92
|
body: 'hello world',
|
|
119
93
|
headers: {
|
|
120
94
|
'Content-Type': 'text/plain'
|
|
@@ -153,7 +127,7 @@ test('Should get the body as buffer', t => {
|
|
|
153
127
|
|
|
154
128
|
sget({
|
|
155
129
|
method: 'POST',
|
|
156
|
-
url:
|
|
130
|
+
url: getServerUrl(fastify),
|
|
157
131
|
body: '{"hello":"world"}',
|
|
158
132
|
headers: {
|
|
159
133
|
'Content-Type': 'application/json'
|
|
@@ -192,7 +166,7 @@ test('Should get the body as buffer', t => {
|
|
|
192
166
|
|
|
193
167
|
sget({
|
|
194
168
|
method: 'POST',
|
|
195
|
-
url:
|
|
169
|
+
url: getServerUrl(fastify),
|
|
196
170
|
body: 'hello world',
|
|
197
171
|
headers: {
|
|
198
172
|
'Content-Type': 'text/plain'
|
|
@@ -229,7 +203,7 @@ test('Should parse empty bodies as a string', t => {
|
|
|
229
203
|
|
|
230
204
|
sget({
|
|
231
205
|
method: 'POST',
|
|
232
|
-
url:
|
|
206
|
+
url: getServerUrl(fastify),
|
|
233
207
|
body: '',
|
|
234
208
|
headers: {
|
|
235
209
|
'Content-Type': 'text/plain'
|
|
@@ -242,7 +216,7 @@ test('Should parse empty bodies as a string', t => {
|
|
|
242
216
|
|
|
243
217
|
sget({
|
|
244
218
|
method: 'DELETE',
|
|
245
|
-
url:
|
|
219
|
+
url: getServerUrl(fastify),
|
|
246
220
|
body: '',
|
|
247
221
|
headers: {
|
|
248
222
|
'Content-Type': 'text/plain',
|
|
@@ -275,7 +249,7 @@ test('Should parse empty bodies as a buffer', t => {
|
|
|
275
249
|
|
|
276
250
|
sget({
|
|
277
251
|
method: 'POST',
|
|
278
|
-
url:
|
|
252
|
+
url: getServerUrl(fastify),
|
|
279
253
|
body: '',
|
|
280
254
|
headers: {
|
|
281
255
|
'Content-Type': 'text/plain'
|
|
@@ -309,7 +283,7 @@ test('The charset should not interfere with the content type handling', t => {
|
|
|
309
283
|
|
|
310
284
|
sget({
|
|
311
285
|
method: 'POST',
|
|
312
|
-
url:
|
|
286
|
+
url: getServerUrl(fastify),
|
|
313
287
|
body: '{"hello":"world"}',
|
|
314
288
|
headers: {
|
|
315
289
|
'Content-Type': 'application/json; charset=utf-8'
|
|
@@ -322,699 +296,3 @@ test('The charset should not interfere with the content type handling', t => {
|
|
|
322
296
|
})
|
|
323
297
|
})
|
|
324
298
|
})
|
|
325
|
-
|
|
326
|
-
test('Wrong parseAs parameter', t => {
|
|
327
|
-
t.plan(2)
|
|
328
|
-
const fastify = Fastify()
|
|
329
|
-
|
|
330
|
-
try {
|
|
331
|
-
fastify.addContentTypeParser('application/json', { parseAs: 'fireworks' }, () => {})
|
|
332
|
-
t.fail('should throw')
|
|
333
|
-
} catch (err) {
|
|
334
|
-
t.equal(err.code, 'FST_ERR_CTP_INVALID_PARSE_TYPE')
|
|
335
|
-
t.equal(err.message, "The body parser can only parse your data as 'string' or 'buffer', you asked 'fireworks' which is not supported.")
|
|
336
|
-
}
|
|
337
|
-
})
|
|
338
|
-
|
|
339
|
-
test('Should allow defining the bodyLimit per parser', t => {
|
|
340
|
-
t.plan(3)
|
|
341
|
-
const fastify = Fastify()
|
|
342
|
-
t.teardown(() => fastify.close())
|
|
343
|
-
|
|
344
|
-
fastify.post('/', (req, reply) => {
|
|
345
|
-
reply.send(req.body)
|
|
346
|
-
})
|
|
347
|
-
|
|
348
|
-
fastify.addContentTypeParser(
|
|
349
|
-
'x/foo',
|
|
350
|
-
{ parseAs: 'string', bodyLimit: 5 },
|
|
351
|
-
function (req, body, done) {
|
|
352
|
-
t.fail('should not be invoked')
|
|
353
|
-
done()
|
|
354
|
-
}
|
|
355
|
-
)
|
|
356
|
-
|
|
357
|
-
fastify.listen({ port: 0 }, err => {
|
|
358
|
-
t.error(err)
|
|
359
|
-
|
|
360
|
-
sget({
|
|
361
|
-
method: 'POST',
|
|
362
|
-
url: getUrl(fastify),
|
|
363
|
-
body: '1234567890',
|
|
364
|
-
headers: {
|
|
365
|
-
'Content-Type': 'x/foo'
|
|
366
|
-
}
|
|
367
|
-
}, (err, response, body) => {
|
|
368
|
-
t.error(err)
|
|
369
|
-
t.strictSame(JSON.parse(body.toString()), {
|
|
370
|
-
statusCode: 413,
|
|
371
|
-
code: 'FST_ERR_CTP_BODY_TOO_LARGE',
|
|
372
|
-
error: 'Payload Too Large',
|
|
373
|
-
message: 'Request body is too large'
|
|
374
|
-
})
|
|
375
|
-
fastify.close()
|
|
376
|
-
})
|
|
377
|
-
})
|
|
378
|
-
})
|
|
379
|
-
|
|
380
|
-
test('route bodyLimit should take precedence over a custom parser bodyLimit', t => {
|
|
381
|
-
t.plan(3)
|
|
382
|
-
const fastify = Fastify()
|
|
383
|
-
t.teardown(() => fastify.close())
|
|
384
|
-
|
|
385
|
-
fastify.post('/', { bodyLimit: 5 }, (request, reply) => {
|
|
386
|
-
reply.send(request.body)
|
|
387
|
-
})
|
|
388
|
-
|
|
389
|
-
fastify.addContentTypeParser(
|
|
390
|
-
'x/foo',
|
|
391
|
-
{ parseAs: 'string', bodyLimit: 100 },
|
|
392
|
-
function (req, body, done) {
|
|
393
|
-
t.fail('should not be invoked')
|
|
394
|
-
done()
|
|
395
|
-
}
|
|
396
|
-
)
|
|
397
|
-
|
|
398
|
-
fastify.listen({ port: 0 }, err => {
|
|
399
|
-
t.error(err)
|
|
400
|
-
|
|
401
|
-
sget({
|
|
402
|
-
method: 'POST',
|
|
403
|
-
url: getUrl(fastify),
|
|
404
|
-
body: '1234567890',
|
|
405
|
-
headers: { 'Content-Type': 'x/foo' }
|
|
406
|
-
}, (err, response, body) => {
|
|
407
|
-
t.error(err)
|
|
408
|
-
t.strictSame(JSON.parse(body.toString()), {
|
|
409
|
-
statusCode: 413,
|
|
410
|
-
code: 'FST_ERR_CTP_BODY_TOO_LARGE',
|
|
411
|
-
error: 'Payload Too Large',
|
|
412
|
-
message: 'Request body is too large'
|
|
413
|
-
})
|
|
414
|
-
fastify.close()
|
|
415
|
-
})
|
|
416
|
-
})
|
|
417
|
-
})
|
|
418
|
-
|
|
419
|
-
test('should be able to use default parser for extra content type', t => {
|
|
420
|
-
t.plan(4)
|
|
421
|
-
const fastify = Fastify()
|
|
422
|
-
t.teardown(() => fastify.close())
|
|
423
|
-
|
|
424
|
-
fastify.post('/', (request, reply) => {
|
|
425
|
-
reply.send(request.body)
|
|
426
|
-
})
|
|
427
|
-
|
|
428
|
-
fastify.addContentTypeParser('text/json', { parseAs: 'string' }, fastify.getDefaultJsonParser('ignore', 'ignore'))
|
|
429
|
-
|
|
430
|
-
fastify.listen({ port: 0 }, err => {
|
|
431
|
-
t.error(err)
|
|
432
|
-
|
|
433
|
-
sget({
|
|
434
|
-
method: 'POST',
|
|
435
|
-
url: getUrl(fastify),
|
|
436
|
-
body: '{"hello":"world"}',
|
|
437
|
-
headers: {
|
|
438
|
-
'Content-Type': 'text/json'
|
|
439
|
-
}
|
|
440
|
-
}, (err, response, body) => {
|
|
441
|
-
t.error(err)
|
|
442
|
-
t.equal(response.statusCode, 200)
|
|
443
|
-
t.strictSame(JSON.parse(body.toString()), { hello: 'world' })
|
|
444
|
-
fastify.close()
|
|
445
|
-
})
|
|
446
|
-
})
|
|
447
|
-
})
|
|
448
|
-
|
|
449
|
-
test('contentTypeParser should add a custom parser with RegExp value', t => {
|
|
450
|
-
t.plan(3)
|
|
451
|
-
|
|
452
|
-
const fastify = Fastify()
|
|
453
|
-
|
|
454
|
-
fastify.post('/', (req, reply) => {
|
|
455
|
-
reply.send(req.body)
|
|
456
|
-
})
|
|
457
|
-
|
|
458
|
-
fastify.options('/', (req, reply) => {
|
|
459
|
-
reply.send(req.body)
|
|
460
|
-
})
|
|
461
|
-
|
|
462
|
-
fastify.addContentTypeParser(/.*\+json$/, function (req, payload, done) {
|
|
463
|
-
jsonParser(payload, function (err, body) {
|
|
464
|
-
done(err, body)
|
|
465
|
-
})
|
|
466
|
-
})
|
|
467
|
-
|
|
468
|
-
fastify.listen({ port: 0 }, err => {
|
|
469
|
-
t.error(err)
|
|
470
|
-
|
|
471
|
-
t.teardown(() => fastify.close())
|
|
472
|
-
|
|
473
|
-
t.test('in POST', t => {
|
|
474
|
-
t.plan(3)
|
|
475
|
-
|
|
476
|
-
sget({
|
|
477
|
-
method: 'POST',
|
|
478
|
-
url: getUrl(fastify),
|
|
479
|
-
body: '{"hello":"world"}',
|
|
480
|
-
headers: {
|
|
481
|
-
'Content-Type': 'application/vnd.test+json'
|
|
482
|
-
}
|
|
483
|
-
}, (err, response, body) => {
|
|
484
|
-
t.error(err)
|
|
485
|
-
t.equal(response.statusCode, 200)
|
|
486
|
-
t.same(body.toString(), JSON.stringify({ hello: 'world' }))
|
|
487
|
-
})
|
|
488
|
-
})
|
|
489
|
-
|
|
490
|
-
t.test('in OPTIONS', t => {
|
|
491
|
-
t.plan(3)
|
|
492
|
-
|
|
493
|
-
sget({
|
|
494
|
-
method: 'OPTIONS',
|
|
495
|
-
url: getUrl(fastify),
|
|
496
|
-
body: '{"hello":"world"}',
|
|
497
|
-
headers: {
|
|
498
|
-
'Content-Type': 'weird/content-type+json'
|
|
499
|
-
}
|
|
500
|
-
}, (err, response, body) => {
|
|
501
|
-
t.error(err)
|
|
502
|
-
t.equal(response.statusCode, 200)
|
|
503
|
-
t.same(body.toString(), JSON.stringify({ hello: 'world' }))
|
|
504
|
-
})
|
|
505
|
-
})
|
|
506
|
-
})
|
|
507
|
-
})
|
|
508
|
-
|
|
509
|
-
test('contentTypeParser should add multiple custom parsers with RegExp values', async t => {
|
|
510
|
-
t.plan(6)
|
|
511
|
-
const fastify = Fastify()
|
|
512
|
-
t.teardown(fastify.close.bind(fastify))
|
|
513
|
-
|
|
514
|
-
fastify.post('/', (req, reply) => {
|
|
515
|
-
reply.send(req.body)
|
|
516
|
-
})
|
|
517
|
-
|
|
518
|
-
fastify.addContentTypeParser(/.*\+json$/, function (req, payload, done) {
|
|
519
|
-
jsonParser(payload, function (err, body) {
|
|
520
|
-
done(err, body)
|
|
521
|
-
})
|
|
522
|
-
})
|
|
523
|
-
|
|
524
|
-
fastify.addContentTypeParser(/.*\+xml$/, function (req, payload, done) {
|
|
525
|
-
done(null, 'xml')
|
|
526
|
-
})
|
|
527
|
-
|
|
528
|
-
fastify.addContentTypeParser(/.*\+myExtension$/i, function (req, payload, done) {
|
|
529
|
-
let data = ''
|
|
530
|
-
payload.on('data', chunk => { data += chunk })
|
|
531
|
-
payload.on('end', () => {
|
|
532
|
-
done(null, data + 'myExtension')
|
|
533
|
-
})
|
|
534
|
-
})
|
|
535
|
-
|
|
536
|
-
await fastify.ready()
|
|
537
|
-
|
|
538
|
-
{
|
|
539
|
-
const response = await fastify.inject({
|
|
540
|
-
method: 'POST',
|
|
541
|
-
url: '/',
|
|
542
|
-
body: '{"hello":"world"}',
|
|
543
|
-
headers: {
|
|
544
|
-
'Content-Type': 'application/vnd.hello+json'
|
|
545
|
-
}
|
|
546
|
-
})
|
|
547
|
-
t.equal(response.statusCode, 200)
|
|
548
|
-
t.same(response.payload.toString(), '{"hello":"world"}')
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
{
|
|
552
|
-
const response = await fastify.inject({
|
|
553
|
-
method: 'POST',
|
|
554
|
-
url: '/',
|
|
555
|
-
body: '{"hello":"world"}',
|
|
556
|
-
headers: {
|
|
557
|
-
'Content-Type': 'application/test+xml'
|
|
558
|
-
}
|
|
559
|
-
})
|
|
560
|
-
t.equal(response.statusCode, 200)
|
|
561
|
-
t.same(response.payload.toString(), 'xml')
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
await fastify.inject({
|
|
565
|
-
method: 'POST',
|
|
566
|
-
path: '/',
|
|
567
|
-
payload: 'abcdefg',
|
|
568
|
-
headers: {
|
|
569
|
-
'Content-Type': 'application/+myExtension'
|
|
570
|
-
}
|
|
571
|
-
}).then((response) => {
|
|
572
|
-
t.equal(response.statusCode, 200)
|
|
573
|
-
t.same(response.payload.toString(), 'abcdefgmyExtension')
|
|
574
|
-
}).catch((err) => {
|
|
575
|
-
t.error(err)
|
|
576
|
-
})
|
|
577
|
-
})
|
|
578
|
-
|
|
579
|
-
test('catch all content type parser should not interfere with content type parser', t => {
|
|
580
|
-
t.plan(10)
|
|
581
|
-
const fastify = Fastify()
|
|
582
|
-
t.teardown(fastify.close.bind(fastify))
|
|
583
|
-
|
|
584
|
-
fastify.post('/', (req, reply) => {
|
|
585
|
-
reply.send(req.body)
|
|
586
|
-
})
|
|
587
|
-
|
|
588
|
-
fastify.addContentTypeParser('*', function (req, payload, done) {
|
|
589
|
-
let data = ''
|
|
590
|
-
payload.on('data', chunk => { data += chunk })
|
|
591
|
-
payload.on('end', () => {
|
|
592
|
-
done(null, data)
|
|
593
|
-
})
|
|
594
|
-
})
|
|
595
|
-
|
|
596
|
-
fastify.addContentTypeParser(/^application\/.*/, function (req, payload, done) {
|
|
597
|
-
jsonParser(payload, function (err, body) {
|
|
598
|
-
done(err, body)
|
|
599
|
-
})
|
|
600
|
-
})
|
|
601
|
-
|
|
602
|
-
fastify.addContentTypeParser('text/html', function (req, payload, done) {
|
|
603
|
-
let data = ''
|
|
604
|
-
payload.on('data', chunk => { data += chunk })
|
|
605
|
-
payload.on('end', () => {
|
|
606
|
-
done(null, data + 'html')
|
|
607
|
-
})
|
|
608
|
-
})
|
|
609
|
-
|
|
610
|
-
fastify.listen({ port: 0 }, err => {
|
|
611
|
-
t.error(err)
|
|
612
|
-
|
|
613
|
-
sget({
|
|
614
|
-
method: 'POST',
|
|
615
|
-
url: getUrl(fastify),
|
|
616
|
-
body: '{"myKey":"myValue"}',
|
|
617
|
-
headers: {
|
|
618
|
-
'Content-Type': 'application/json'
|
|
619
|
-
}
|
|
620
|
-
}, (err, response, body) => {
|
|
621
|
-
t.error(err)
|
|
622
|
-
t.equal(response.statusCode, 200)
|
|
623
|
-
t.same(body.toString(), JSON.stringify({ myKey: 'myValue' }))
|
|
624
|
-
})
|
|
625
|
-
|
|
626
|
-
sget({
|
|
627
|
-
method: 'POST',
|
|
628
|
-
url: getUrl(fastify),
|
|
629
|
-
body: 'body',
|
|
630
|
-
headers: {
|
|
631
|
-
'Content-Type': 'very-weird-content-type'
|
|
632
|
-
}
|
|
633
|
-
}, (err, response, body) => {
|
|
634
|
-
t.error(err)
|
|
635
|
-
t.equal(response.statusCode, 200)
|
|
636
|
-
t.same(body.toString(), 'body')
|
|
637
|
-
})
|
|
638
|
-
|
|
639
|
-
sget({
|
|
640
|
-
method: 'POST',
|
|
641
|
-
url: getUrl(fastify),
|
|
642
|
-
body: 'my text',
|
|
643
|
-
headers: {
|
|
644
|
-
'Content-Type': 'text/html'
|
|
645
|
-
}
|
|
646
|
-
}, (err, response, body) => {
|
|
647
|
-
t.error(err)
|
|
648
|
-
t.equal(response.statusCode, 200)
|
|
649
|
-
t.same(body.toString(), 'my texthtml')
|
|
650
|
-
})
|
|
651
|
-
})
|
|
652
|
-
})
|
|
653
|
-
|
|
654
|
-
test('should prefer string content types over RegExp ones', t => {
|
|
655
|
-
t.plan(7)
|
|
656
|
-
const fastify = Fastify()
|
|
657
|
-
t.teardown(fastify.close.bind(fastify))
|
|
658
|
-
|
|
659
|
-
fastify.post('/', (req, reply) => {
|
|
660
|
-
reply.send(req.body)
|
|
661
|
-
})
|
|
662
|
-
|
|
663
|
-
fastify.addContentTypeParser(/^application\/.*/, function (req, payload, done) {
|
|
664
|
-
let data = ''
|
|
665
|
-
payload.on('data', chunk => { data += chunk })
|
|
666
|
-
payload.on('end', () => {
|
|
667
|
-
done(null, data)
|
|
668
|
-
})
|
|
669
|
-
})
|
|
670
|
-
|
|
671
|
-
fastify.addContentTypeParser('application/json', function (req, payload, done) {
|
|
672
|
-
jsonParser(payload, function (err, body) {
|
|
673
|
-
done(err, body)
|
|
674
|
-
})
|
|
675
|
-
})
|
|
676
|
-
|
|
677
|
-
fastify.listen({ port: 0 }, err => {
|
|
678
|
-
t.error(err)
|
|
679
|
-
|
|
680
|
-
sget({
|
|
681
|
-
method: 'POST',
|
|
682
|
-
url: getUrl(fastify),
|
|
683
|
-
body: '{"k1":"myValue", "k2": "myValue"}',
|
|
684
|
-
headers: {
|
|
685
|
-
'Content-Type': 'application/json'
|
|
686
|
-
}
|
|
687
|
-
}, (err, response, body) => {
|
|
688
|
-
t.error(err)
|
|
689
|
-
t.equal(response.statusCode, 200)
|
|
690
|
-
t.same(body.toString(), JSON.stringify({ k1: 'myValue', k2: 'myValue' }))
|
|
691
|
-
})
|
|
692
|
-
|
|
693
|
-
sget({
|
|
694
|
-
method: 'POST',
|
|
695
|
-
url: getUrl(fastify),
|
|
696
|
-
body: 'javascript',
|
|
697
|
-
headers: {
|
|
698
|
-
'Content-Type': 'application/javascript'
|
|
699
|
-
}
|
|
700
|
-
}, (err, response, body) => {
|
|
701
|
-
t.error(err)
|
|
702
|
-
t.equal(response.statusCode, 200)
|
|
703
|
-
t.same(body.toString(), 'javascript')
|
|
704
|
-
})
|
|
705
|
-
})
|
|
706
|
-
})
|
|
707
|
-
|
|
708
|
-
test('removeContentTypeParser should support arrays of content types to remove', t => {
|
|
709
|
-
t.plan(8)
|
|
710
|
-
|
|
711
|
-
const fastify = Fastify()
|
|
712
|
-
t.teardown(fastify.close.bind(fastify))
|
|
713
|
-
|
|
714
|
-
fastify.addContentTypeParser('application/xml', function (req, payload, done) {
|
|
715
|
-
payload.on('data', () => {})
|
|
716
|
-
payload.on('end', () => {
|
|
717
|
-
done(null, 'xml')
|
|
718
|
-
})
|
|
719
|
-
})
|
|
720
|
-
|
|
721
|
-
fastify.addContentTypeParser(/^image\/.*/, function (req, payload, done) {
|
|
722
|
-
payload.on('data', () => {})
|
|
723
|
-
payload.on('end', () => {
|
|
724
|
-
done(null, 'image')
|
|
725
|
-
})
|
|
726
|
-
})
|
|
727
|
-
|
|
728
|
-
fastify.removeContentTypeParser([/^image\/.*/, 'application/json'])
|
|
729
|
-
|
|
730
|
-
fastify.post('/', (req, reply) => {
|
|
731
|
-
reply.send(req.body)
|
|
732
|
-
})
|
|
733
|
-
|
|
734
|
-
fastify.listen({ port: 0 }, err => {
|
|
735
|
-
t.error(err)
|
|
736
|
-
|
|
737
|
-
sget({
|
|
738
|
-
method: 'POST',
|
|
739
|
-
url: getUrl(fastify),
|
|
740
|
-
body: '<?xml version="1.0">',
|
|
741
|
-
headers: {
|
|
742
|
-
'Content-Type': 'application/xml'
|
|
743
|
-
}
|
|
744
|
-
}, (err, response, body) => {
|
|
745
|
-
t.error(err)
|
|
746
|
-
t.equal(response.statusCode, 200)
|
|
747
|
-
t.same(body.toString(), 'xml')
|
|
748
|
-
})
|
|
749
|
-
|
|
750
|
-
sget({
|
|
751
|
-
method: 'POST',
|
|
752
|
-
url: getUrl(fastify),
|
|
753
|
-
body: '',
|
|
754
|
-
headers: {
|
|
755
|
-
'Content-Type': 'image/png'
|
|
756
|
-
}
|
|
757
|
-
}, (err, response, body) => {
|
|
758
|
-
t.error(err)
|
|
759
|
-
t.equal(response.statusCode, 415)
|
|
760
|
-
})
|
|
761
|
-
|
|
762
|
-
sget({
|
|
763
|
-
method: 'POST',
|
|
764
|
-
url: getUrl(fastify),
|
|
765
|
-
body: '{test: "test"}',
|
|
766
|
-
headers: {
|
|
767
|
-
'Content-Type': 'application/json'
|
|
768
|
-
}
|
|
769
|
-
}, (err, response, body) => {
|
|
770
|
-
t.error(err)
|
|
771
|
-
t.equal(response.statusCode, 415)
|
|
772
|
-
})
|
|
773
|
-
})
|
|
774
|
-
})
|
|
775
|
-
|
|
776
|
-
test('removeContentTypeParser should support encapsulation', t => {
|
|
777
|
-
t.plan(6)
|
|
778
|
-
|
|
779
|
-
const fastify = Fastify()
|
|
780
|
-
|
|
781
|
-
fastify.addContentTypeParser('application/xml', function (req, payload, done) {
|
|
782
|
-
payload.on('data', () => {})
|
|
783
|
-
payload.on('end', () => {
|
|
784
|
-
done(null, 'xml')
|
|
785
|
-
})
|
|
786
|
-
})
|
|
787
|
-
|
|
788
|
-
fastify.post('/', (req, reply) => {
|
|
789
|
-
reply.send(req.body)
|
|
790
|
-
})
|
|
791
|
-
|
|
792
|
-
fastify.register(function (instance, options, done) {
|
|
793
|
-
instance.removeContentTypeParser('application/xml')
|
|
794
|
-
|
|
795
|
-
instance.post('/encapsulated', (req, reply) => {
|
|
796
|
-
reply.send(req.body)
|
|
797
|
-
})
|
|
798
|
-
|
|
799
|
-
done()
|
|
800
|
-
})
|
|
801
|
-
|
|
802
|
-
fastify.listen({ port: 0 }, err => {
|
|
803
|
-
t.error(err)
|
|
804
|
-
|
|
805
|
-
sget({
|
|
806
|
-
method: 'POST',
|
|
807
|
-
url: getUrl(fastify) + '/encapsulated',
|
|
808
|
-
body: '<?xml version="1.0">',
|
|
809
|
-
headers: {
|
|
810
|
-
'Content-Type': 'application/xml'
|
|
811
|
-
}
|
|
812
|
-
}, (err, response, body) => {
|
|
813
|
-
t.error(err)
|
|
814
|
-
t.equal(response.statusCode, 415)
|
|
815
|
-
})
|
|
816
|
-
|
|
817
|
-
sget({
|
|
818
|
-
method: 'POST',
|
|
819
|
-
url: getUrl(fastify),
|
|
820
|
-
body: '<?xml version="1.0">',
|
|
821
|
-
headers: {
|
|
822
|
-
'Content-Type': 'application/xml'
|
|
823
|
-
}
|
|
824
|
-
}, (err, response, body) => {
|
|
825
|
-
t.error(err)
|
|
826
|
-
t.equal(response.statusCode, 200)
|
|
827
|
-
t.same(body.toString(), 'xml')
|
|
828
|
-
fastify.close()
|
|
829
|
-
})
|
|
830
|
-
})
|
|
831
|
-
})
|
|
832
|
-
|
|
833
|
-
test('removeAllContentTypeParsers should support encapsulation', t => {
|
|
834
|
-
t.plan(6)
|
|
835
|
-
|
|
836
|
-
const fastify = Fastify()
|
|
837
|
-
|
|
838
|
-
fastify.post('/', (req, reply) => {
|
|
839
|
-
reply.send(req.body)
|
|
840
|
-
})
|
|
841
|
-
|
|
842
|
-
fastify.register(function (instance, options, done) {
|
|
843
|
-
instance.removeAllContentTypeParsers()
|
|
844
|
-
|
|
845
|
-
instance.post('/encapsulated', (req, reply) => {
|
|
846
|
-
reply.send(req.body)
|
|
847
|
-
})
|
|
848
|
-
|
|
849
|
-
done()
|
|
850
|
-
})
|
|
851
|
-
|
|
852
|
-
fastify.listen({ port: 0 }, err => {
|
|
853
|
-
t.error(err)
|
|
854
|
-
|
|
855
|
-
sget({
|
|
856
|
-
method: 'POST',
|
|
857
|
-
url: getUrl(fastify) + '/encapsulated',
|
|
858
|
-
body: '{}',
|
|
859
|
-
headers: {
|
|
860
|
-
'Content-Type': 'application/json'
|
|
861
|
-
}
|
|
862
|
-
}, (err, response, body) => {
|
|
863
|
-
t.error(err)
|
|
864
|
-
t.equal(response.statusCode, 415)
|
|
865
|
-
})
|
|
866
|
-
|
|
867
|
-
sget({
|
|
868
|
-
method: 'POST',
|
|
869
|
-
url: getUrl(fastify),
|
|
870
|
-
body: '{"test":1}',
|
|
871
|
-
headers: {
|
|
872
|
-
'Content-Type': 'application/json'
|
|
873
|
-
}
|
|
874
|
-
}, (err, response, body) => {
|
|
875
|
-
t.error(err)
|
|
876
|
-
t.equal(response.statusCode, 200)
|
|
877
|
-
t.same(JSON.parse(body.toString()).test, 1)
|
|
878
|
-
fastify.close()
|
|
879
|
-
})
|
|
880
|
-
})
|
|
881
|
-
})
|
|
882
|
-
|
|
883
|
-
test('cannot remove all content type parsers after binding', t => {
|
|
884
|
-
t.plan(2)
|
|
885
|
-
|
|
886
|
-
const fastify = Fastify()
|
|
887
|
-
|
|
888
|
-
t.teardown(fastify.close.bind(fastify))
|
|
889
|
-
|
|
890
|
-
fastify.listen({ port: 0 }, function (err) {
|
|
891
|
-
t.error(err)
|
|
892
|
-
|
|
893
|
-
t.throws(() => fastify.removeAllContentTypeParsers())
|
|
894
|
-
})
|
|
895
|
-
})
|
|
896
|
-
|
|
897
|
-
test('cannot remove content type parsers after binding', t => {
|
|
898
|
-
t.plan(2)
|
|
899
|
-
|
|
900
|
-
const fastify = Fastify()
|
|
901
|
-
|
|
902
|
-
t.teardown(fastify.close.bind(fastify))
|
|
903
|
-
|
|
904
|
-
fastify.listen({ port: 0 }, function (err) {
|
|
905
|
-
t.error(err)
|
|
906
|
-
|
|
907
|
-
t.throws(() => fastify.removeContentTypeParser('application/json'))
|
|
908
|
-
})
|
|
909
|
-
})
|
|
910
|
-
|
|
911
|
-
test('should be able to override the default json parser after removeAllContentTypeParsers', t => {
|
|
912
|
-
t.plan(5)
|
|
913
|
-
|
|
914
|
-
const fastify = Fastify()
|
|
915
|
-
|
|
916
|
-
fastify.post('/', (req, reply) => {
|
|
917
|
-
reply.send(req.body)
|
|
918
|
-
})
|
|
919
|
-
|
|
920
|
-
fastify.removeAllContentTypeParsers()
|
|
921
|
-
|
|
922
|
-
fastify.addContentTypeParser('application/json', function (req, payload, done) {
|
|
923
|
-
t.ok('called')
|
|
924
|
-
jsonParser(payload, function (err, body) {
|
|
925
|
-
done(err, body)
|
|
926
|
-
})
|
|
927
|
-
})
|
|
928
|
-
|
|
929
|
-
fastify.listen({ port: 0 }, err => {
|
|
930
|
-
t.error(err)
|
|
931
|
-
|
|
932
|
-
sget({
|
|
933
|
-
method: 'POST',
|
|
934
|
-
url: getUrl(fastify),
|
|
935
|
-
body: '{"hello":"world"}',
|
|
936
|
-
headers: {
|
|
937
|
-
'Content-Type': 'application/json'
|
|
938
|
-
}
|
|
939
|
-
}, (err, response, body) => {
|
|
940
|
-
t.error(err)
|
|
941
|
-
t.equal(response.statusCode, 200)
|
|
942
|
-
t.same(body.toString(), JSON.stringify({ hello: 'world' }))
|
|
943
|
-
fastify.close()
|
|
944
|
-
})
|
|
945
|
-
})
|
|
946
|
-
})
|
|
947
|
-
|
|
948
|
-
test('should be able to override the default plain text parser after removeAllContentTypeParsers', t => {
|
|
949
|
-
t.plan(5)
|
|
950
|
-
|
|
951
|
-
const fastify = Fastify()
|
|
952
|
-
|
|
953
|
-
fastify.post('/', (req, reply) => {
|
|
954
|
-
reply.send(req.body)
|
|
955
|
-
})
|
|
956
|
-
|
|
957
|
-
fastify.removeAllContentTypeParsers()
|
|
958
|
-
|
|
959
|
-
fastify.addContentTypeParser('text/plain', function (req, payload, done) {
|
|
960
|
-
t.ok('called')
|
|
961
|
-
plainTextParser(payload, function (err, body) {
|
|
962
|
-
done(err, body)
|
|
963
|
-
})
|
|
964
|
-
})
|
|
965
|
-
|
|
966
|
-
fastify.listen({ port: 0 }, err => {
|
|
967
|
-
t.error(err)
|
|
968
|
-
|
|
969
|
-
sget({
|
|
970
|
-
method: 'POST',
|
|
971
|
-
url: getUrl(fastify),
|
|
972
|
-
body: 'hello world',
|
|
973
|
-
headers: {
|
|
974
|
-
'Content-Type': 'text/plain'
|
|
975
|
-
}
|
|
976
|
-
}, (err, response, body) => {
|
|
977
|
-
t.error(err)
|
|
978
|
-
t.equal(response.statusCode, 200)
|
|
979
|
-
t.equal(body.toString(), 'hello world')
|
|
980
|
-
fastify.close()
|
|
981
|
-
})
|
|
982
|
-
})
|
|
983
|
-
})
|
|
984
|
-
|
|
985
|
-
test('should be able to add a custom content type parser after removeAllContentTypeParsers', t => {
|
|
986
|
-
t.plan(5)
|
|
987
|
-
|
|
988
|
-
const fastify = Fastify()
|
|
989
|
-
|
|
990
|
-
fastify.post('/', (req, reply) => {
|
|
991
|
-
reply.send(req.body)
|
|
992
|
-
})
|
|
993
|
-
|
|
994
|
-
fastify.removeAllContentTypeParsers()
|
|
995
|
-
|
|
996
|
-
fastify.addContentTypeParser('application/jsoff', function (req, payload, done) {
|
|
997
|
-
t.ok('called')
|
|
998
|
-
jsonParser(payload, function (err, body) {
|
|
999
|
-
done(err, body)
|
|
1000
|
-
})
|
|
1001
|
-
})
|
|
1002
|
-
|
|
1003
|
-
fastify.listen({ port: 0 }, err => {
|
|
1004
|
-
t.error(err)
|
|
1005
|
-
|
|
1006
|
-
sget({
|
|
1007
|
-
method: 'POST',
|
|
1008
|
-
url: getUrl(fastify),
|
|
1009
|
-
body: '{"hello":"world"}',
|
|
1010
|
-
headers: {
|
|
1011
|
-
'Content-Type': 'application/jsoff'
|
|
1012
|
-
}
|
|
1013
|
-
}, (err, response, body) => {
|
|
1014
|
-
t.error(err)
|
|
1015
|
-
t.equal(response.statusCode, 200)
|
|
1016
|
-
t.same(body.toString(), JSON.stringify({ hello: 'world' }))
|
|
1017
|
-
fastify.close()
|
|
1018
|
-
})
|
|
1019
|
-
})
|
|
1020
|
-
})
|