Aktueller Stand

This commit is contained in:
2026-01-22 19:05:45 +01:00
parent 85dee61a4d
commit e280e4eadb
1967 changed files with 397327 additions and 74093 deletions

View File

@@ -2,43 +2,43 @@
const net = require('node:net')
const http = require('node:http')
const { test } = require('tap')
const { test } = require('node:test')
const Fastify = require('..')
const { Client } = require('undici')
const semver = require('semver')
const split = require('split2')
const { sleep } = require('./helper')
test('close callback', t => {
test('close callback', (t, testDone) => {
t.plan(7)
const fastify = Fastify()
fastify.addHook('onClose', onClose)
function onClose (instance, done) {
t.type(fastify, this)
t.type(fastify, instance)
t.equal(fastify, this)
t.equal(fastify, instance)
t.assert.ok(typeof fastify === typeof this)
t.assert.ok(typeof fastify === typeof instance)
t.assert.strictEqual(fastify, this)
t.assert.strictEqual(fastify, instance)
done()
}
fastify.listen({ port: 0 }, err => {
t.error(err)
t.assert.ifError(err)
fastify.close((err) => {
t.error(err)
t.ok('close callback')
t.assert.ifError(err)
t.assert.ok('close callback')
testDone()
})
})
})
test('inside register', t => {
test('inside register', (t, done) => {
t.plan(5)
const fastify = Fastify()
fastify.register(function (f, opts, done) {
f.addHook('onClose', onClose)
function onClose (instance, done) {
t.ok(instance.prototype === fastify.prototype)
t.equal(instance, f)
t.assert.ok(instance.prototype === fastify.prototype)
t.assert.strictEqual(instance, f)
done()
}
@@ -46,23 +46,24 @@ test('inside register', t => {
})
fastify.listen({ port: 0 }, err => {
t.error(err)
t.assert.ifError(err)
fastify.close((err) => {
t.error(err)
t.ok('close callback')
t.assert.ifError(err)
t.assert.ok('close callback')
done()
})
})
})
test('close order', t => {
test('close order', (t, done) => {
t.plan(5)
const fastify = Fastify()
const order = [1, 2, 3]
fastify.register(function (f, opts, done) {
f.addHook('onClose', (instance, done) => {
t.equal(order.shift(), 1)
t.assert.strictEqual(order.shift(), 1)
done()
})
@@ -70,16 +71,17 @@ test('close order', t => {
})
fastify.addHook('onClose', (instance, done) => {
t.equal(order.shift(), 2)
t.assert.strictEqual(order.shift(), 2)
done()
})
fastify.listen({ port: 0 }, err => {
t.error(err)
t.assert.ifError(err)
fastify.close((err) => {
t.error(err)
t.equal(order.shift(), 3)
t.assert.ifError(err)
t.assert.strictEqual(order.shift(), 3)
done()
})
})
})
@@ -91,37 +93,38 @@ test('close order - async', async t => {
fastify.register(function (f, opts, done) {
f.addHook('onClose', async instance => {
t.equal(order.shift(), 1)
t.assert.strictEqual(order.shift(), 1)
})
done()
})
fastify.addHook('onClose', () => {
t.equal(order.shift(), 2)
t.assert.strictEqual(order.shift(), 2)
})
await fastify.listen({ port: 0 })
await fastify.close()
t.equal(order.shift(), 3)
t.assert.strictEqual(order.shift(), 3)
})
test('should not throw an error if the server is not listening', t => {
test('should not throw an error if the server is not listening', (t, done) => {
t.plan(2)
const fastify = Fastify()
fastify.addHook('onClose', onClose)
function onClose (instance, done) {
t.type(fastify, instance)
t.assert.ok(instance.prototype === fastify.prototype)
done()
}
fastify.close((err) => {
t.error(err)
t.assert.ifError(err)
done()
})
})
test('onClose should keep the context', t => {
test('onClose should keep the context', (t, done) => {
t.plan(4)
const fastify = Fastify()
fastify.register(plugin)
@@ -129,11 +132,11 @@ test('onClose should keep the context', t => {
function plugin (instance, opts, done) {
instance.decorate('test', true)
instance.addHook('onClose', onClose)
t.ok(instance.prototype === fastify.prototype)
t.assert.ok(instance.prototype === fastify.prototype)
function onClose (i, done) {
t.ok(i.test)
t.equal(i, instance)
t.assert.ok(i.test)
t.assert.strictEqual(i, instance)
done()
}
@@ -141,11 +144,12 @@ test('onClose should keep the context', t => {
}
fastify.close((err) => {
t.error(err)
t.assert.ifError(err)
done()
})
})
test('Should return error while closing (promise) - injection', t => {
test('Should return error while closing (promise) - injection', (t, done) => {
t.plan(4)
const fastify = Fastify()
@@ -159,8 +163,8 @@ test('Should return error while closing (promise) - injection', t => {
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 200)
fastify.close()
process.nextTick(() => {
@@ -168,14 +172,15 @@ test('Should return error while closing (promise) - injection', t => {
method: 'GET',
url: '/'
}).catch(err => {
t.ok(err)
t.equal(err.code, 'FST_ERR_REOPENED_CLOSE_SERVER')
t.assert.ok(err)
t.assert.strictEqual(err.code, 'FST_ERR_REOPENED_CLOSE_SERVER')
done()
})
}, 100)
})
})
test('Should return error while closing (callback) - injection', t => {
test('Should return error while closing (callback) - injection', (t, done) => {
t.plan(4)
const fastify = Fastify()
@@ -191,8 +196,8 @@ test('Should return error while closing (callback) - injection', t => {
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 200)
fastify.close()
setTimeout(() => {
@@ -200,53 +205,15 @@ test('Should return error while closing (callback) - injection', t => {
method: 'GET',
url: '/'
}, (err, res) => {
t.ok(err)
t.equal(err.code, 'FST_ERR_REOPENED_CLOSE_SERVER')
t.assert.ok(err)
t.assert.strictEqual(err.code, 'FST_ERR_REOPENED_CLOSE_SERVER')
done()
})
}, 100)
})
})
const isNodeVersionGte1819 = semver.gte(process.version, '18.19.0')
test('Current opened connection should continue to work after closing and return "connection: close" header - return503OnClosing: false, skip Node >= v18.19.x', { skip: isNodeVersionGte1819 }, t => {
const fastify = Fastify({
return503OnClosing: false,
forceCloseConnections: false
})
fastify.get('/', (req, reply) => {
fastify.close()
reply.send({ hello: 'world' })
})
fastify.listen({ port: 0 }, err => {
t.error(err)
const port = fastify.server.address().port
const client = net.createConnection({ port }, () => {
client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
client.once('data', data => {
t.match(data.toString(), /Connection:\s*keep-alive/i)
t.match(data.toString(), /200 OK/i)
client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
client.once('data', data => {
t.match(data.toString(), /Connection:\s*close/i)
t.match(data.toString(), /200 OK/i)
// Test that fastify closes the TCP connection
client.once('close', () => {
t.end()
})
})
})
})
})
})
test('Current opened connection should NOT continue to work after closing and return "connection: close" header - return503OnClosing: false, skip Node < v18.19.x', { skip: !isNodeVersionGte1819 }, t => {
test('Current opened connection should NOT continue to work after closing and return "connection: close" header - return503OnClosing: false', (t, done) => {
t.plan(4)
const fastify = Fastify({
return503OnClosing: false,
@@ -259,7 +226,7 @@ test('Current opened connection should NOT continue to work after closing and re
})
fastify.listen({ port: 0 }, err => {
t.error(err)
t.assert.ifError(err)
const port = fastify.server.address().port
const client = net.createConnection({ port }, () => {
@@ -272,12 +239,13 @@ test('Current opened connection should NOT continue to work after closing and re
})
client.on('close', function () {
t.pass('close')
t.assert.ok(true)
done()
})
client.once('data', data => {
t.match(data.toString(), /Connection:\s*keep-alive/i)
t.match(data.toString(), /200 OK/i)
t.assert.match(data.toString(), /Connection:\s*keep-alive/i)
t.assert.match(data.toString(), /200 OK/i)
client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
})
@@ -285,7 +253,7 @@ test('Current opened connection should NOT continue to work after closing and re
})
})
test('Current opened connection should not accept new incoming connections', t => {
test('Current opened connection should not accept new incoming connections', (t, done) => {
t.plan(3)
const fastify = Fastify({ forceCloseConnections: false })
fastify.get('/', (req, reply) => {
@@ -295,19 +263,20 @@ test('Current opened connection should not accept new incoming connections', t =
}, 250)
})
fastify.listen({ port: 0 }, err => {
t.error(err)
fastify.listen({ port: 0 }, async err => {
t.assert.ifError(err)
const instance = new Client('http://localhost:' + fastify.server.address().port)
instance.request({ path: '/', method: 'GET' }).then(data => {
t.equal(data.statusCode, 200)
})
instance.request({ path: '/', method: 'GET' }).then(data => {
t.equal(data.statusCode, 503)
})
let response = await instance.request({ path: '/', method: 'GET' })
t.assert.strictEqual(response.statusCode, 200)
response = await instance.request({ path: '/', method: 'GET' })
t.assert.strictEqual(response.statusCode, 503)
done()
})
})
test('rejected incoming connections should be logged', t => {
test('rejected incoming connections should be logged', (t, done) => {
t.plan(2)
const stream = split(JSON.parse)
const fastify = Fastify({
@@ -330,13 +299,14 @@ test('rejected incoming connections should be logged', t => {
})
fastify.listen({ port: 0 }, err => {
t.error(err)
t.assert.ifError(err)
const instance = new Client('http://localhost:' + fastify.server.address().port)
// initial request to trigger close
instance.request({ path: '/', method: 'GET' })
// subsequent request should be rejected
instance.request({ path: '/', method: 'GET' }).then(() => {
t.ok(messages.find(message => message.msg.includes('request aborted')))
t.assert.ok(messages.find(message => message.msg.includes('request aborted')))
done()
})
})
})
@@ -351,8 +321,8 @@ test('Cannot be reopened the closed server without listen callback', async t =>
try {
await fastify.listen({ port: 0 })
} catch (err) {
t.ok(err)
t.equal(err.code, 'FST_ERR_REOPENED_CLOSE_SERVER')
t.assert.ok(err)
t.assert.strictEqual(err.code, 'FST_ERR_REOPENED_CLOSE_SERVER')
}
})
@@ -368,15 +338,15 @@ test('Cannot be reopened the closed server has listen callback', async t => {
reject(err)
})
}).catch(err => {
t.equal(err.code, 'FST_ERR_REOPENED_CLOSE_SERVER')
t.ok(err)
t.assert.strictEqual(err.code, 'FST_ERR_REOPENED_CLOSE_SERVER')
t.assert.ok(err)
})
})
const server = http.createServer()
const noSupport = typeof server.closeAllConnections !== 'function'
test('shutsdown while keep-alive connections are active (non-async, native)', { skip: noSupport }, t => {
test('shutsdown while keep-alive connections are active (non-async, native)', { skip: noSupport }, (t, done) => {
t.plan(5)
const timeoutTime = 2 * 60 * 1000
@@ -390,31 +360,32 @@ test('shutsdown while keep-alive connections are active (non-async, native)', {
})
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
t.assert.ifError(err)
const client = new Client(
'http://localhost:' + fastify.server.address().port,
{ keepAliveTimeout: 1 * 60 * 1000 }
)
client.request({ path: '/', method: 'GET' }, (err, response) => {
t.error(err)
t.equal(client.closed, false)
t.assert.ifError(err)
t.assert.strictEqual(client.closed, false)
fastify.close((err) => {
t.error(err)
t.assert.ifError(err)
// Due to the nature of the way we reap these keep-alive connections,
// there hasn't been enough time before the server fully closed in order
// for the client to have seen the socket get destroyed. The mere fact
// that we have reached this callback is enough indication that the
// feature being tested works as designed.
t.equal(client.closed, false)
t.assert.strictEqual(client.closed, false)
done()
})
})
})
})
test('shutsdown while keep-alive connections are active (non-async, idle, native)', { skip: noSupport }, t => {
test('shutsdown while keep-alive connections are active (non-async, idle, native)', { skip: noSupport }, (t, done) => {
t.plan(5)
const timeoutTime = 2 * 60 * 1000
@@ -428,25 +399,27 @@ test('shutsdown while keep-alive connections are active (non-async, idle, native
})
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
t.assert.ifError(err)
const client = new Client(
'http://localhost:' + fastify.server.address().port,
{ keepAliveTimeout: 1 * 60 * 1000 }
)
client.request({ path: '/', method: 'GET' }, (err, response) => {
t.error(err)
t.equal(client.closed, false)
t.assert.ifError(err)
t.assert.strictEqual(client.closed, false)
fastify.close((err) => {
t.error(err)
t.assert.ifError(err)
// Due to the nature of the way we reap these keep-alive connections,
// there hasn't been enough time before the server fully closed in order
// for the client to have seen the socket get destroyed. The mere fact
// that we have reached this callback is enough indication that the
// feature being tested works as designed.
t.equal(client.closed, false)
t.assert.strictEqual(client.closed, false)
done()
})
})
})
@@ -474,9 +447,9 @@ test('triggers on-close hook in the right order with multiple bindings', async t
setTimeout(() => {
fastify.close(err => {
order.push(3)
t.match(order, expectedOrder)
t.assert.deepEqual(order, expectedOrder)
if (err) t.error(err)
if (err) t.assert.ifError(err)
else resolve()
})
}, 2000)
@@ -519,20 +492,20 @@ test('triggers on-close hook in the right order with multiple bindings (forceClo
})
client.request({ path: '/', method: 'GET' })
.then((res) => res.body.json(), err => t.error(err))
.then((res) => res.body.json(), err => t.assert.ifError(err))
.then(json => {
t.match(json, expectedPayload, 'should payload match')
t.notOk(client.closed, 'should client not be closed')
}, err => t.error(err))
t.assert.deepEqual(json, expectedPayload, 'should payload match')
t.assert.ok(!client.closed, 'should client not be closed')
}, err => t.assert.ifError(err))
}
await new Promise((resolve, reject) => {
setTimeout(() => {
fastify.close(err => {
order.push(2)
t.match(order, expectedOrder)
t.assert.deepEqual(order, expectedOrder)
if (err) t.error(err)
if (err) t.assert.ifError(err)
else resolve()
})
}, 2000)
@@ -575,27 +548,27 @@ test('triggers on-close hook in the right order with multiple bindings (forceClo
})
client.request({ path: '/', method: 'GET' })
.then((res) => res.body.json(), err => t.error(err))
.then((res) => res.body.json(), err => t.assert.ifError(err))
.then(json => {
t.match(json, expectedPayload, 'should payload match')
t.notOk(client.closed, 'should client not be closed')
}, err => t.error(err))
t.assert.deepEqual(json, expectedPayload, 'should payload match')
t.assert.ok(!client.closed, 'should client not be closed')
}, err => t.assert.ifError(err))
}
await new Promise((resolve, reject) => {
setTimeout(() => {
fastify.close(err => {
order.push(2)
t.match(order, expectedOrder)
t.assert.deepEqual(order, expectedOrder)
if (err) t.error(err)
if (err) t.assert.ifError(err)
else resolve()
})
}, 2000)
})
})
test('shutsdown while keep-alive connections are active (non-async, custom)', t => {
test('shutsdown while keep-alive connections are active (non-async, custom)', (t, done) => {
t.plan(5)
const timeoutTime = 2 * 60 * 1000
@@ -618,53 +591,56 @@ test('shutsdown while keep-alive connections are active (non-async, custom)', t
})
fastify.listen({ port: 0 }, (err, address) => {
t.error(err)
t.assert.ifError(err)
const client = new Client(
'http://localhost:' + fastify.server.address().port,
{ keepAliveTimeout: 1 * 60 * 1000 }
)
client.request({ path: '/', method: 'GET' }, (err, response) => {
t.error(err)
t.equal(client.closed, false)
t.assert.ifError(err)
t.assert.strictEqual(client.closed, false)
fastify.close((err) => {
t.error(err)
t.assert.ifError(err)
// Due to the nature of the way we reap these keep-alive connections,
// there hasn't been enough time before the server fully closed in order
// for the client to have seen the socket get destroyed. The mere fact
// that we have reached this callback is enough indication that the
// feature being tested works as designed.
t.equal(client.closed, false)
t.assert.strictEqual(client.closed, false)
done()
})
})
})
})
test('preClose callback', t => {
test('preClose callback', (t, done) => {
t.plan(5)
const fastify = Fastify()
fastify.addHook('onClose', onClose)
let preCloseCalled = false
function onClose (instance, done) {
t.equal(preCloseCalled, true)
t.assert.strictEqual(preCloseCalled, true)
done()
}
fastify.addHook('preClose', preClose)
function preClose (done) {
t.type(this, fastify)
t.assert.ok(typeof this === typeof fastify)
preCloseCalled = true
done()
}
fastify.listen({ port: 0 }, err => {
t.error(err)
t.assert.ifError(err)
fastify.close((err) => {
t.error(err)
t.ok('close callback')
t.assert.ifError(err)
t.assert.ok('close callback')
done()
})
})
})
@@ -675,13 +651,13 @@ test('preClose async', async t => {
fastify.addHook('onClose', onClose)
let preCloseCalled = false
async function onClose () {
t.equal(preCloseCalled, true)
t.assert.strictEqual(preCloseCalled, true)
}
fastify.addHook('preClose', preClose)
async function preClose () {
preCloseCalled = true
t.type(this, fastify)
t.assert.ok(typeof this === typeof fastify)
}
await fastify.listen({ port: 0 })
@@ -689,13 +665,13 @@ test('preClose async', async t => {
await fastify.close()
})
test('preClose execution order', t => {
test('preClose execution order', (t, done) => {
t.plan(4)
const fastify = Fastify()
const order = []
fastify.addHook('onClose', onClose)
function onClose (instance, done) {
t.same(order, [1, 2, 3])
t.assert.deepStrictEqual(order, [1, 2, 3])
done()
}
@@ -719,11 +695,12 @@ test('preClose execution order', t => {
})
fastify.listen({ port: 0 }, err => {
t.error(err)
t.assert.ifError(err)
fastify.close((err) => {
t.error(err)
t.ok('close callback')
t.assert.ifError(err)
t.assert.ok('close callback')
done()
})
})
})