fastify 3.7.0 → 3.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/Ecosystem.md +6 -1
- package/docs/Hooks.md +0 -5
- package/docs/Recommendations.md +17 -0
- package/docs/Reply.md +2 -2
- package/docs/Request.md +1 -1
- package/docs/Routes.md +19 -1
- package/docs/Server.md +2 -0
- package/docs/Style-Guide.md +180 -0
- package/docs/TypeScript.md +359 -359
- package/examples/parser.js +1 -1
- package/fastify.js +8 -8
- package/lib/contentTypeParser.js +12 -11
- package/lib/context.js +4 -3
- package/lib/decorate.js +1 -1
- package/lib/fourOhFour.js +4 -4
- package/lib/handleRequest.js +5 -5
- package/lib/hooks.js +4 -4
- package/lib/logger.js +6 -6
- package/lib/pluginUtils.js +1 -1
- package/lib/reply.js +24 -21
- package/lib/reqIdGenFactory.js +2 -2
- package/lib/request.js +8 -5
- package/lib/route.js +9 -8
- package/lib/schemas.js +1 -1
- package/lib/server.js +5 -5
- package/lib/validation.js +8 -8
- package/package.json +8 -8
- package/test/404s.test.js +15 -15
- package/test/async-await.test.js +7 -7
- package/test/custom-parser-async.test.js +2 -2
- package/test/custom-parser.test.js +8 -8
- package/test/helper.js +1 -1
- package/test/hooks.test.js +6 -6
- package/test/http2/head.test.js +1 -1
- package/test/http2/plain.test.js +1 -1
- package/test/http2/secure-with-fallback.test.js +1 -1
- package/test/http2/secure.test.js +1 -1
- package/test/http2/unknown-http-method.test.js +1 -1
- package/test/https/https.test.js +2 -1
- package/test/inject.test.js +2 -2
- package/test/internals/all.test.js +1 -1
- package/test/internals/hookRunner.test.js +1 -1
- package/test/internals/logger.test.js +1 -1
- package/test/internals/reply.test.js +62 -7
- package/test/internals/request.test.js +23 -0
- package/test/listen.test.js +12 -0
- package/test/logger.test.js +10 -10
- package/test/nullable-validation.test.js +108 -0
- package/test/pretty-print.test.js +9 -14
- package/test/reply-error.test.js +2 -2
- package/test/route-hooks.test.js +10 -10
- package/test/stream.test.js +11 -11
- package/test/types/fastify.test-d.ts +1 -2
- package/test/types/logger.test-d.ts +1 -1
- package/test/types/route.test-d.ts +5 -0
- package/test/versioned-routes.test.js +55 -0
- package/types/.eslintrc.json +4 -1
- package/types/content-type-parser.d.ts +0 -15
- package/types/hooks.d.ts +138 -16
- package/types/instance.d.ts +81 -2
- package/types/logger.d.ts +1 -1
- package/types/plugin.d.ts +5 -7
- package/types/register.d.ts +8 -8
- package/types/route.d.ts +38 -58
- package/types/schema.d.ts +4 -4
- package/types/serverFactory.d.ts +9 -9
package/test/404s.test.js
CHANGED
|
@@ -1073,7 +1073,7 @@ test('404 inside onSend', t => {
|
|
|
1073
1073
|
|
|
1074
1074
|
const fastify = Fastify()
|
|
1075
1075
|
|
|
1076
|
-
|
|
1076
|
+
let called = false
|
|
1077
1077
|
|
|
1078
1078
|
fastify.get('/', function (req, reply) {
|
|
1079
1079
|
reply.send({ hello: 'world' })
|
|
@@ -1215,7 +1215,7 @@ test('preHandler option for setNotFoundHandler', t => {
|
|
|
1215
1215
|
payload: { hello: 'world' }
|
|
1216
1216
|
}, (err, res) => {
|
|
1217
1217
|
t.error(err)
|
|
1218
|
-
|
|
1218
|
+
const payload = JSON.parse(res.payload)
|
|
1219
1219
|
t.deepEqual(payload, { preHandler: true, hello: 'world' })
|
|
1220
1220
|
})
|
|
1221
1221
|
})
|
|
@@ -1244,7 +1244,7 @@ test('preHandler option for setNotFoundHandler', t => {
|
|
|
1244
1244
|
payload: { hello: 'world' }
|
|
1245
1245
|
}, (err, res) => {
|
|
1246
1246
|
t.error(err)
|
|
1247
|
-
|
|
1247
|
+
const payload = JSON.parse(res.payload)
|
|
1248
1248
|
t.deepEqual(payload, { preHandler: true, hello: 'world' })
|
|
1249
1249
|
})
|
|
1250
1250
|
})
|
|
@@ -1278,7 +1278,7 @@ test('preHandler option for setNotFoundHandler', t => {
|
|
|
1278
1278
|
payload: { hello: 'world' }
|
|
1279
1279
|
}, (err, res) => {
|
|
1280
1280
|
t.error(err)
|
|
1281
|
-
|
|
1281
|
+
const payload = JSON.parse(res.payload)
|
|
1282
1282
|
t.deepEqual(payload, { preHandler1: true, preHandler2: true, hello: 'world' })
|
|
1283
1283
|
})
|
|
1284
1284
|
})
|
|
@@ -1307,7 +1307,7 @@ test('preHandler option for setNotFoundHandler', t => {
|
|
|
1307
1307
|
payload: { hello: 'world' }
|
|
1308
1308
|
}, (err, res) => {
|
|
1309
1309
|
t.error(err)
|
|
1310
|
-
|
|
1310
|
+
const payload = JSON.parse(res.payload)
|
|
1311
1311
|
t.deepEqual(payload, { check: 'ab', hello: 'world' })
|
|
1312
1312
|
})
|
|
1313
1313
|
})
|
|
@@ -1339,7 +1339,7 @@ test('preHandler option for setNotFoundHandler', t => {
|
|
|
1339
1339
|
payload: { hello: 'world' }
|
|
1340
1340
|
}, (err, res) => {
|
|
1341
1341
|
t.error(err)
|
|
1342
|
-
|
|
1342
|
+
const payload = JSON.parse(res.payload)
|
|
1343
1343
|
t.deepEqual(payload, { hello: 'earth' })
|
|
1344
1344
|
})
|
|
1345
1345
|
|
|
@@ -1349,7 +1349,7 @@ test('preHandler option for setNotFoundHandler', t => {
|
|
|
1349
1349
|
payload: { hello: 'world' }
|
|
1350
1350
|
}, (err, res) => {
|
|
1351
1351
|
t.error(err)
|
|
1352
|
-
|
|
1352
|
+
const payload = JSON.parse(res.payload)
|
|
1353
1353
|
t.deepEqual(payload, { hello: 'world' })
|
|
1354
1354
|
})
|
|
1355
1355
|
})
|
|
@@ -1372,7 +1372,7 @@ test('preHandler option for setNotFoundHandler', t => {
|
|
|
1372
1372
|
payload: { hello: 'world' }
|
|
1373
1373
|
}, (err, res) => {
|
|
1374
1374
|
t.error(err)
|
|
1375
|
-
|
|
1375
|
+
const payload = JSON.parse(res.payload)
|
|
1376
1376
|
t.equal(res.statusCode, 500)
|
|
1377
1377
|
t.deepEqual(payload, {
|
|
1378
1378
|
message: 'kaboom',
|
|
@@ -1401,7 +1401,7 @@ test('preHandler option for setNotFoundHandler', t => {
|
|
|
1401
1401
|
payload: { hello: 'world' }
|
|
1402
1402
|
}, (err, res) => {
|
|
1403
1403
|
t.error(err)
|
|
1404
|
-
|
|
1404
|
+
const payload = JSON.parse(res.payload)
|
|
1405
1405
|
t.equal(res.statusCode, 401)
|
|
1406
1406
|
t.deepEqual(payload, {
|
|
1407
1407
|
message: 'go away',
|
|
@@ -1436,7 +1436,7 @@ test('preHandler option for setNotFoundHandler', t => {
|
|
|
1436
1436
|
payload: { hello: 'world' }
|
|
1437
1437
|
}, (err, res) => {
|
|
1438
1438
|
t.error(err)
|
|
1439
|
-
|
|
1439
|
+
const payload = JSON.parse(res.payload)
|
|
1440
1440
|
t.deepEqual(payload, { preHandler: 'ab', hello: 'world' })
|
|
1441
1441
|
})
|
|
1442
1442
|
})
|
|
@@ -1473,7 +1473,7 @@ test('preHandler option for setNotFoundHandler', t => {
|
|
|
1473
1473
|
payload: { hello: 'world' }
|
|
1474
1474
|
}, (err, res) => {
|
|
1475
1475
|
t.error(err)
|
|
1476
|
-
|
|
1476
|
+
const payload = JSON.parse(res.payload)
|
|
1477
1477
|
t.deepEqual(payload, { check: 'ab', hello: 'world' })
|
|
1478
1478
|
})
|
|
1479
1479
|
|
|
@@ -1483,7 +1483,7 @@ test('preHandler option for setNotFoundHandler', t => {
|
|
|
1483
1483
|
payload: { hello: 'world' }
|
|
1484
1484
|
}, (err, res) => {
|
|
1485
1485
|
t.error(err)
|
|
1486
|
-
|
|
1486
|
+
const payload = JSON.parse(res.payload)
|
|
1487
1487
|
t.deepEqual(payload, { check: 'a', hello: 'world' })
|
|
1488
1488
|
})
|
|
1489
1489
|
})
|
|
@@ -1511,7 +1511,7 @@ test('preHandler option for setNotFoundHandler', t => {
|
|
|
1511
1511
|
payload: { hello: 'world' }
|
|
1512
1512
|
}, (err, res) => {
|
|
1513
1513
|
t.error(err)
|
|
1514
|
-
|
|
1514
|
+
const payload = JSON.parse(res.payload)
|
|
1515
1515
|
t.deepEqual(payload, { foo: 43, hello: 'world' })
|
|
1516
1516
|
})
|
|
1517
1517
|
})
|
|
@@ -1627,7 +1627,7 @@ test('preValidation option', t => {
|
|
|
1627
1627
|
payload: { hello: 'world' }
|
|
1628
1628
|
}, (err, res) => {
|
|
1629
1629
|
t.error(err)
|
|
1630
|
-
|
|
1630
|
+
const payload = JSON.parse(res.payload)
|
|
1631
1631
|
t.deepEqual(payload, { hello: 'world' })
|
|
1632
1632
|
})
|
|
1633
1633
|
})
|
|
@@ -1657,7 +1657,7 @@ t.test('preValidation option could accept an array of functions', t => {
|
|
|
1657
1657
|
payload: { hello: 'world' }
|
|
1658
1658
|
}, (err, res) => {
|
|
1659
1659
|
t.error(err)
|
|
1660
|
-
|
|
1660
|
+
const payload = JSON.parse(res.payload)
|
|
1661
1661
|
t.deepEqual(payload, { hello: 'world' })
|
|
1662
1662
|
})
|
|
1663
1663
|
})
|
package/test/async-await.test.js
CHANGED
|
@@ -402,8 +402,8 @@ test('does not call reply.send() twice if 204 reponse is already sent', t => {
|
|
|
402
402
|
test('error is logged because promise was fulfilled with undefined', t => {
|
|
403
403
|
t.plan(3)
|
|
404
404
|
|
|
405
|
-
|
|
406
|
-
|
|
405
|
+
let fastify = null
|
|
406
|
+
const stream = split(JSON.parse)
|
|
407
407
|
try {
|
|
408
408
|
fastify = Fastify({
|
|
409
409
|
logger: {
|
|
@@ -442,8 +442,8 @@ test('error is logged because promise was fulfilled with undefined', t => {
|
|
|
442
442
|
test('error is not logged because promise was fulfilled with undefined but statusCode 204 was set', t => {
|
|
443
443
|
t.plan(3)
|
|
444
444
|
|
|
445
|
-
|
|
446
|
-
|
|
445
|
+
let fastify = null
|
|
446
|
+
const stream = split(JSON.parse)
|
|
447
447
|
try {
|
|
448
448
|
fastify = Fastify({
|
|
449
449
|
logger: {
|
|
@@ -482,9 +482,9 @@ test('error is not logged because promise was fulfilled with undefined but statu
|
|
|
482
482
|
test('error is not logged because promise was fulfilled with undefined but response was sent before promise resolution', t => {
|
|
483
483
|
t.plan(4)
|
|
484
484
|
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
485
|
+
let fastify = null
|
|
486
|
+
const stream = split(JSON.parse)
|
|
487
|
+
const payload = { hello: 'world' }
|
|
488
488
|
try {
|
|
489
489
|
fastify = Fastify({
|
|
490
490
|
logger: {
|
|
@@ -20,7 +20,7 @@ test('contentTypeParser should add a custom async parser', t => {
|
|
|
20
20
|
})
|
|
21
21
|
|
|
22
22
|
fastify.addContentTypeParser('application/jsoff', async function (req, payload) {
|
|
23
|
-
|
|
23
|
+
const res = await new Promise((resolve, reject) => resolve(payload))
|
|
24
24
|
return res
|
|
25
25
|
})
|
|
26
26
|
|
|
@@ -84,7 +84,7 @@ test('contentTypeParser should add a custom async parser - deprecated syntax', t
|
|
|
84
84
|
})
|
|
85
85
|
|
|
86
86
|
fastify.addContentTypeParser('application/jsoff', async function (req) {
|
|
87
|
-
|
|
87
|
+
const res = await new Promise((resolve, reject) => resolve(req))
|
|
88
88
|
return res
|
|
89
89
|
})
|
|
90
90
|
|
|
@@ -9,7 +9,7 @@ const Fastify = require('..')
|
|
|
9
9
|
const jsonParser = require('fast-json-body')
|
|
10
10
|
|
|
11
11
|
function plainTextParser (request, callback) {
|
|
12
|
-
|
|
12
|
+
let body = ''
|
|
13
13
|
request.setEncoding('utf8')
|
|
14
14
|
request.on('error', onError)
|
|
15
15
|
request.on('data', onData)
|
|
@@ -436,7 +436,7 @@ test('catch all content type parser', t => {
|
|
|
436
436
|
})
|
|
437
437
|
|
|
438
438
|
fastify.addContentTypeParser('*', function (req, payload, done) {
|
|
439
|
-
|
|
439
|
+
let data = ''
|
|
440
440
|
payload.on('data', chunk => { data += chunk })
|
|
441
441
|
payload.on('end', () => {
|
|
442
442
|
done(null, data)
|
|
@@ -484,7 +484,7 @@ test('catch all content type parser should not interfere with other conte type p
|
|
|
484
484
|
})
|
|
485
485
|
|
|
486
486
|
fastify.addContentTypeParser('*', function (req, payload, done) {
|
|
487
|
-
|
|
487
|
+
let data = ''
|
|
488
488
|
payload.on('data', chunk => { data += chunk })
|
|
489
489
|
payload.on('end', () => {
|
|
490
490
|
done(null, data)
|
|
@@ -538,7 +538,7 @@ test('\'*\' catch undefined Content-Type requests', t => {
|
|
|
538
538
|
t.tearDown(fastify.close.bind(fastify))
|
|
539
539
|
|
|
540
540
|
fastify.addContentTypeParser('*', function (req, payload, done) {
|
|
541
|
-
|
|
541
|
+
let data = ''
|
|
542
542
|
payload.on('data', chunk => { data += chunk })
|
|
543
543
|
payload.on('end', () => {
|
|
544
544
|
done(null, data)
|
|
@@ -754,7 +754,7 @@ test('Should get the body as string', t => {
|
|
|
754
754
|
t.ok('called')
|
|
755
755
|
t.ok(typeof body === 'string')
|
|
756
756
|
try {
|
|
757
|
-
|
|
757
|
+
const json = JSON.parse(body)
|
|
758
758
|
done(null, json)
|
|
759
759
|
} catch (err) {
|
|
760
760
|
err.statusCode = 400
|
|
@@ -900,7 +900,7 @@ test('Should get the body as string', t => {
|
|
|
900
900
|
t.ok('called')
|
|
901
901
|
t.ok(typeof body === 'string')
|
|
902
902
|
try {
|
|
903
|
-
|
|
903
|
+
const plainText = body
|
|
904
904
|
done(null, plainText)
|
|
905
905
|
} catch (err) {
|
|
906
906
|
err.statusCode = 400
|
|
@@ -939,7 +939,7 @@ test('Should get the body as buffer', t => {
|
|
|
939
939
|
t.ok('called')
|
|
940
940
|
t.ok(body instanceof Buffer)
|
|
941
941
|
try {
|
|
942
|
-
|
|
942
|
+
const json = JSON.parse(body)
|
|
943
943
|
done(null, json)
|
|
944
944
|
} catch (err) {
|
|
945
945
|
err.statusCode = 400
|
|
@@ -978,7 +978,7 @@ test('Should get the body as buffer', t => {
|
|
|
978
978
|
t.ok('called')
|
|
979
979
|
t.ok(body instanceof Buffer)
|
|
980
980
|
try {
|
|
981
|
-
|
|
981
|
+
const plainText = body
|
|
982
982
|
done(null, plainText)
|
|
983
983
|
} catch (err) {
|
|
984
984
|
err.statusCode = 400
|
package/test/helper.js
CHANGED
|
@@ -282,7 +282,7 @@ module.exports.payloadMethod = function (method, t, isSetErrorHandler = false) {
|
|
|
282
282
|
|
|
283
283
|
// Node errors for OPTIONS requests with a stream body and no Content-Length header
|
|
284
284
|
if (upMethod !== 'OPTIONS') {
|
|
285
|
-
|
|
285
|
+
let chunk = Buffer.alloc(1024 * 1024 + 1, 0)
|
|
286
286
|
const largeStream = new stream.Readable({
|
|
287
287
|
read () {
|
|
288
288
|
this.push(chunk)
|
package/test/hooks.test.js
CHANGED
|
@@ -197,7 +197,7 @@ test('onRequest hook should support encapsulation / 1', t => {
|
|
|
197
197
|
test('onRequest hook should support encapsulation / 2', t => {
|
|
198
198
|
t.plan(3)
|
|
199
199
|
const fastify = Fastify()
|
|
200
|
-
|
|
200
|
+
let pluginInstance
|
|
201
201
|
|
|
202
202
|
fastify.addHook('onRequest', () => {})
|
|
203
203
|
|
|
@@ -813,7 +813,7 @@ test('onResponse hook should support encapsulation / 1', t => {
|
|
|
813
813
|
test('onResponse hook should support encapsulation / 2', t => {
|
|
814
814
|
t.plan(3)
|
|
815
815
|
const fastify = Fastify()
|
|
816
|
-
|
|
816
|
+
let pluginInstance
|
|
817
817
|
|
|
818
818
|
fastify.addHook('onResponse', () => {})
|
|
819
819
|
|
|
@@ -890,7 +890,7 @@ test('onResponse hook should support encapsulation / 3', t => {
|
|
|
890
890
|
test('onSend hook should support encapsulation / 1', t => {
|
|
891
891
|
t.plan(3)
|
|
892
892
|
const fastify = Fastify()
|
|
893
|
-
|
|
893
|
+
let pluginInstance
|
|
894
894
|
|
|
895
895
|
fastify.addHook('onSend', () => {})
|
|
896
896
|
|
|
@@ -1015,7 +1015,7 @@ test('onSend hook is called after payload is serialized and headers are set', t
|
|
|
1015
1015
|
})
|
|
1016
1016
|
|
|
1017
1017
|
fastify.register((instance, opts, done) => {
|
|
1018
|
-
|
|
1018
|
+
let chunk = 'stream payload'
|
|
1019
1019
|
const thePayload = new stream.Readable({
|
|
1020
1020
|
read () {
|
|
1021
1021
|
this.push(chunk)
|
|
@@ -2163,7 +2163,7 @@ test('preValidation hook should support encapsulation / 1', t => {
|
|
|
2163
2163
|
test('preValidation hook should support encapsulation / 2', t => {
|
|
2164
2164
|
t.plan(3)
|
|
2165
2165
|
const fastify = Fastify()
|
|
2166
|
-
|
|
2166
|
+
let pluginInstance
|
|
2167
2167
|
|
|
2168
2168
|
fastify.addHook('preValidation', () => {})
|
|
2169
2169
|
|
|
@@ -2582,7 +2582,7 @@ test('preParsing hook should support encapsulation / 1', t => {
|
|
|
2582
2582
|
test('preParsing hook should support encapsulation / 2', t => {
|
|
2583
2583
|
t.plan(3)
|
|
2584
2584
|
const fastify = Fastify()
|
|
2585
|
-
|
|
2585
|
+
let pluginInstance
|
|
2586
2586
|
|
|
2587
2587
|
fastify.addHook('preParsing', function a () {})
|
|
2588
2588
|
|
package/test/http2/head.test.js
CHANGED
package/test/http2/plain.test.js
CHANGED
package/test/https/https.test.js
CHANGED
|
@@ -7,8 +7,9 @@ const fs = require('fs')
|
|
|
7
7
|
const path = require('path')
|
|
8
8
|
const Fastify = require('../..')
|
|
9
9
|
|
|
10
|
+
let fastify
|
|
10
11
|
try {
|
|
11
|
-
|
|
12
|
+
fastify = Fastify({
|
|
12
13
|
https: {
|
|
13
14
|
key: fs.readFileSync(path.join(__dirname, 'fastify.key')),
|
|
14
15
|
cert: fs.readFileSync(path.join(__dirname, 'fastify.cert'))
|
package/test/inject.test.js
CHANGED
|
@@ -329,7 +329,7 @@ test('inject a multipart request using form-body', t => {
|
|
|
329
329
|
const fastify = Fastify()
|
|
330
330
|
|
|
331
331
|
fastify.addContentTypeParser('*', function (req, payload, done) {
|
|
332
|
-
|
|
332
|
+
let body = ''
|
|
333
333
|
payload.on('data', d => {
|
|
334
334
|
body += d
|
|
335
335
|
})
|
|
@@ -362,7 +362,7 @@ function getStream () {
|
|
|
362
362
|
}
|
|
363
363
|
util.inherits(Read, Stream.Readable)
|
|
364
364
|
const word = '{"hello":"world"}'
|
|
365
|
-
|
|
365
|
+
let i = 0
|
|
366
366
|
|
|
367
367
|
Read.prototype._read = function (size) {
|
|
368
368
|
this.push(word[i] ? word[i++] : null)
|
|
@@ -28,7 +28,7 @@ test('fastify.all should add all the methods to the same url', t => {
|
|
|
28
28
|
|
|
29
29
|
fastify.inject(options, (err, res) => {
|
|
30
30
|
t.error(err)
|
|
31
|
-
|
|
31
|
+
const payload = JSON.parse(res.payload)
|
|
32
32
|
t.deepEqual(payload, { method: method })
|
|
33
33
|
})
|
|
34
34
|
}
|
|
@@ -139,7 +139,7 @@ test('hookRunner - In case of error should skip to done (with promises)', t => {
|
|
|
139
139
|
test('hookRunner - Be able to exit before its natural end', t => {
|
|
140
140
|
t.plan(4)
|
|
141
141
|
|
|
142
|
-
|
|
142
|
+
let shouldStop = false
|
|
143
143
|
hookRunner([fn1, fn2, fn3], iterator, 'a', 'b', done)
|
|
144
144
|
|
|
145
145
|
function iterator (fn, a, b, next) {
|
|
@@ -79,7 +79,7 @@ test('reply.send throw with circular JSON', t => {
|
|
|
79
79
|
}
|
|
80
80
|
const reply = new Reply(response, { context: { onSend: [] } })
|
|
81
81
|
t.throws(() => {
|
|
82
|
-
|
|
82
|
+
const obj = {}
|
|
83
83
|
obj.obj = obj
|
|
84
84
|
reply.send(JSON.stringify(obj))
|
|
85
85
|
}, 'Converting circular structure to JSON')
|
|
@@ -430,9 +430,9 @@ test('stream with content type should not send application/octet-stream', t => {
|
|
|
430
430
|
const fs = require('fs')
|
|
431
431
|
const path = require('path')
|
|
432
432
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
433
|
+
const streamPath = path.join(__dirname, '..', '..', 'package.json')
|
|
434
|
+
const stream = fs.createReadStream(streamPath)
|
|
435
|
+
const buf = fs.readFileSync(streamPath)
|
|
436
436
|
|
|
437
437
|
fastify.get('/', function (req, reply) {
|
|
438
438
|
reply.header('Content-Type', 'text/plain').send(stream)
|
|
@@ -459,9 +459,9 @@ test('stream using reply.raw.writeHead should return customize headers', t => {
|
|
|
459
459
|
const fs = require('fs')
|
|
460
460
|
const path = require('path')
|
|
461
461
|
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
462
|
+
const streamPath = path.join(__dirname, '..', '..', 'package.json')
|
|
463
|
+
const stream = fs.createReadStream(streamPath)
|
|
464
|
+
const buf = fs.readFileSync(streamPath)
|
|
465
465
|
|
|
466
466
|
fastify.get('/', function (req, reply) {
|
|
467
467
|
reply.log.warn = function mockWarn (message) {
|
|
@@ -781,6 +781,61 @@ test('undefined payload should be sent as-is', t => {
|
|
|
781
781
|
})
|
|
782
782
|
})
|
|
783
783
|
|
|
784
|
+
test('for HEAD method, no body should be sent but content-length should be', t => {
|
|
785
|
+
t.plan(11)
|
|
786
|
+
|
|
787
|
+
const fastify = require('../..')()
|
|
788
|
+
const contentType = 'application/json; charset=utf-8'
|
|
789
|
+
const bodySize = JSON.stringify({ foo: 'bar' }).length
|
|
790
|
+
|
|
791
|
+
fastify.head('/', {
|
|
792
|
+
onSend: function (request, reply, payload, next) {
|
|
793
|
+
t.strictEqual(payload, undefined)
|
|
794
|
+
next()
|
|
795
|
+
}
|
|
796
|
+
}, function (req, reply) {
|
|
797
|
+
reply.header('content-length', bodySize)
|
|
798
|
+
reply.header('content-type', contentType)
|
|
799
|
+
reply.code(200).send()
|
|
800
|
+
})
|
|
801
|
+
|
|
802
|
+
fastify.head('/with/null', {
|
|
803
|
+
onSend: function (request, reply, payload, next) {
|
|
804
|
+
t.strictEqual(payload, 'null')
|
|
805
|
+
next()
|
|
806
|
+
}
|
|
807
|
+
}, function (req, reply) {
|
|
808
|
+
reply.header('content-length', bodySize)
|
|
809
|
+
reply.header('content-type', contentType)
|
|
810
|
+
reply.code(200).send(null)
|
|
811
|
+
})
|
|
812
|
+
|
|
813
|
+
fastify.listen(0, err => {
|
|
814
|
+
t.error(err)
|
|
815
|
+
fastify.server.unref()
|
|
816
|
+
|
|
817
|
+
sget({
|
|
818
|
+
method: 'HEAD',
|
|
819
|
+
url: `http://localhost:${fastify.server.address().port}`
|
|
820
|
+
}, (err, response, body) => {
|
|
821
|
+
t.error(err)
|
|
822
|
+
t.strictEqual(response.headers['content-type'], contentType)
|
|
823
|
+
t.strictEqual(response.headers['content-length'], bodySize.toString())
|
|
824
|
+
t.strictEqual(body.length, 0)
|
|
825
|
+
})
|
|
826
|
+
|
|
827
|
+
sget({
|
|
828
|
+
method: 'HEAD',
|
|
829
|
+
url: `http://localhost:${fastify.server.address().port}/with/null`
|
|
830
|
+
}, (err, response, body) => {
|
|
831
|
+
t.error(err)
|
|
832
|
+
t.strictEqual(response.headers['content-type'], contentType)
|
|
833
|
+
t.strictEqual(response.headers['content-length'], bodySize.toString())
|
|
834
|
+
t.strictEqual(body.length, 0)
|
|
835
|
+
})
|
|
836
|
+
})
|
|
837
|
+
})
|
|
838
|
+
|
|
784
839
|
test('reply.send(new NotFound()) should not invoke the 404 handler', t => {
|
|
785
840
|
t.plan(9)
|
|
786
841
|
|
|
@@ -157,3 +157,26 @@ test('Request with trust proxy - x-forwarded-host header has precedence over hos
|
|
|
157
157
|
t.type(request, TpRequest)
|
|
158
158
|
t.strictEqual(request.hostname, 'example.com')
|
|
159
159
|
})
|
|
160
|
+
|
|
161
|
+
test('Request with trust proxy - handles multiple entries in x-forwarded-host/proto', t => {
|
|
162
|
+
t.plan(3)
|
|
163
|
+
const headers = {
|
|
164
|
+
'x-forwarded-host': 'example2.com, example.com',
|
|
165
|
+
'x-forwarded-proto': 'http, https'
|
|
166
|
+
}
|
|
167
|
+
const req = {
|
|
168
|
+
method: 'GET',
|
|
169
|
+
url: '/',
|
|
170
|
+
// Some dependencies (proxy-addr, forwarded) still depend on the deprecated
|
|
171
|
+
// .connection property, we use .socket. Include both to satisfy everyone.
|
|
172
|
+
socket: { remoteAddress: 'ip' },
|
|
173
|
+
connection: { remoteAddress: 'ip' },
|
|
174
|
+
headers
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const TpRequest = Request.buildRequest(Request, true)
|
|
178
|
+
const request = new TpRequest('id', 'params', req, 'query', 'log')
|
|
179
|
+
t.type(request, TpRequest)
|
|
180
|
+
t.strictEqual(request.hostname, 'example.com')
|
|
181
|
+
t.strictEqual(request.protocol, 'https')
|
|
182
|
+
})
|
package/test/listen.test.js
CHANGED
|
@@ -62,6 +62,18 @@ test('listen accepts options and a callback', t => {
|
|
|
62
62
|
})
|
|
63
63
|
})
|
|
64
64
|
|
|
65
|
+
test('listen accepts options, backlog and a callback', t => {
|
|
66
|
+
t.plan(1)
|
|
67
|
+
const fastify = Fastify()
|
|
68
|
+
t.tearDown(fastify.close.bind(fastify))
|
|
69
|
+
fastify.listen({
|
|
70
|
+
port: 0,
|
|
71
|
+
host: 'localhost'
|
|
72
|
+
}, 511, (err) => {
|
|
73
|
+
t.error(err)
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
|
|
65
77
|
test('listen accepts a port, address and a callback with (err, address)', t => {
|
|
66
78
|
t.plan(2)
|
|
67
79
|
const fastify = Fastify()
|
package/test/logger.test.js
CHANGED
|
@@ -32,8 +32,8 @@ tearDown(() => {
|
|
|
32
32
|
|
|
33
33
|
test('defaults to info level', t => {
|
|
34
34
|
t.plan(13)
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
let fastify = null
|
|
36
|
+
const stream = split(JSON.parse)
|
|
37
37
|
try {
|
|
38
38
|
fastify = Fastify({
|
|
39
39
|
logger: {
|
|
@@ -80,8 +80,8 @@ test('defaults to info level', t => {
|
|
|
80
80
|
|
|
81
81
|
test('test log stream', t => {
|
|
82
82
|
t.plan(12)
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
let fastify = null
|
|
84
|
+
const stream = split(JSON.parse)
|
|
85
85
|
try {
|
|
86
86
|
fastify = Fastify({
|
|
87
87
|
logger: {
|
|
@@ -127,8 +127,8 @@ test('test log stream', t => {
|
|
|
127
127
|
|
|
128
128
|
test('test error log stream', t => {
|
|
129
129
|
t.plan(11)
|
|
130
|
-
|
|
131
|
-
|
|
130
|
+
let fastify = null
|
|
131
|
+
const stream = split(JSON.parse)
|
|
132
132
|
try {
|
|
133
133
|
fastify = Fastify({
|
|
134
134
|
logger: {
|
|
@@ -248,8 +248,8 @@ test('can use external logger instance with custom serializer', t => {
|
|
|
248
248
|
|
|
249
249
|
test('expose the logger', t => {
|
|
250
250
|
t.plan(2)
|
|
251
|
-
|
|
252
|
-
|
|
251
|
+
let fastify = null
|
|
252
|
+
const stream = split(JSON.parse)
|
|
253
253
|
try {
|
|
254
254
|
fastify = Fastify({
|
|
255
255
|
logger: {
|
|
@@ -1190,8 +1190,8 @@ test('Do not wrap IPv4 address', t => {
|
|
|
1190
1190
|
|
|
1191
1191
|
test('file option', t => {
|
|
1192
1192
|
t.plan(13)
|
|
1193
|
-
|
|
1194
|
-
|
|
1193
|
+
let fastify = null
|
|
1194
|
+
const dest = file()
|
|
1195
1195
|
|
|
1196
1196
|
fastify = Fastify({
|
|
1197
1197
|
logger: {
|