diff --git a/test/emit-warning.test.js b/test/emit-warning.test.js index fd82ef8fe8..4d44b04a50 100644 --- a/test/emit-warning.test.js +++ b/test/emit-warning.test.js @@ -3,6 +3,7 @@ const sget = require('simple-get').concat const { test } = require('tap') const Fastify = require('..') +const semver = require('semver') process.removeAllListeners('warning') @@ -129,3 +130,33 @@ test('Should emit a warning when using payload less preParsing hook', t => { }) }) }) + +if (semver.gte(process.versions.node, '13.0.0')) { + test('Should emit a warning when accessing request.connection instead of request.socket on Node process greater than 13.0.0', t => { + t.plan(4) + + process.on('warning', onWarning) + function onWarning (warning) { + t.strictEqual(warning.name, 'FastifyDeprecation') + t.strictEqual(warning.code, 'FSTDEP005') + t.strictEqual(warning.message, 'You are accessing the deprecated "request.connection" property. Use "request.socket" instead.') + + // removed listener before light-my-request emit second warning + process.removeListener('warning', onWarning) + } + + const fastify = Fastify() + + fastify.get('/', (request, reply) => { + reply.send(request.connection) + }) + + fastify.inject({ + method: 'GET', + path: '/' + }, (err, res) => { + t.error(err) + process.removeListener('warning', onWarning) + }) + }) +}