Aktueller Stand
This commit is contained in:
143
backend/node_modules/fastify/test/skip-reply-send.test.js
generated
vendored
143
backend/node_modules/fastify/test/skip-reply-send.test.js
generated
vendored
@@ -1,6 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('tap')
|
||||
const { test, describe } = require('node:test')
|
||||
const split = require('split2')
|
||||
const net = require('node:net')
|
||||
const Fastify = require('../fastify')
|
||||
@@ -19,7 +19,7 @@ const lifecycleHooks = [
|
||||
'onError'
|
||||
]
|
||||
|
||||
test('skip automatic reply.send() with reply.sent = true and a body', (t) => {
|
||||
test('skip automatic reply.send() with reply.hijack and a body', async (t) => {
|
||||
const stream = split(JSON.parse)
|
||||
const app = Fastify({
|
||||
logger: {
|
||||
@@ -28,27 +28,27 @@ test('skip automatic reply.send() with reply.sent = true and a body', (t) => {
|
||||
})
|
||||
|
||||
stream.on('data', (line) => {
|
||||
t.not(line.level, 40) // there are no errors
|
||||
t.not(line.level, 50) // there are no errors
|
||||
t.assert.notStrictEqual(line.level, 40) // there are no errors
|
||||
t.assert.notStrictEqual(line.level, 50) // there are no errors
|
||||
})
|
||||
|
||||
app.get('/', (req, reply) => {
|
||||
reply.sent = true
|
||||
reply.hijack()
|
||||
reply.raw.end('hello world')
|
||||
|
||||
return Promise.resolve('this will be skipped')
|
||||
})
|
||||
|
||||
return app.inject({
|
||||
await app.inject({
|
||||
method: 'GET',
|
||||
url: '/'
|
||||
}).then((res) => {
|
||||
t.equal(res.statusCode, 200)
|
||||
t.equal(res.body, 'hello world')
|
||||
t.assert.strictEqual(res.statusCode, 200)
|
||||
t.assert.strictEqual(res.body, 'hello world')
|
||||
})
|
||||
})
|
||||
|
||||
test('skip automatic reply.send() with reply.sent = true and no body', (t) => {
|
||||
test('skip automatic reply.send() with reply.hijack and no body', async (t) => {
|
||||
const stream = split(JSON.parse)
|
||||
const app = Fastify({
|
||||
logger: {
|
||||
@@ -57,27 +57,27 @@ test('skip automatic reply.send() with reply.sent = true and no body', (t) => {
|
||||
})
|
||||
|
||||
stream.on('data', (line) => {
|
||||
t.not(line.level, 40) // there are no error
|
||||
t.not(line.level, 50) // there are no error
|
||||
t.assert.notStrictEqual(line.level, 40) // there are no error
|
||||
t.assert.notStrictEqual(line.level, 50) // there are no error
|
||||
})
|
||||
|
||||
app.get('/', (req, reply) => {
|
||||
reply.sent = true
|
||||
reply.hijack()
|
||||
reply.raw.end('hello world')
|
||||
|
||||
return Promise.resolve()
|
||||
})
|
||||
|
||||
return app.inject({
|
||||
await app.inject({
|
||||
method: 'GET',
|
||||
url: '/'
|
||||
}).then((res) => {
|
||||
t.equal(res.statusCode, 200)
|
||||
t.equal(res.body, 'hello world')
|
||||
t.assert.strictEqual(res.statusCode, 200)
|
||||
t.assert.strictEqual(res.body, 'hello world')
|
||||
})
|
||||
})
|
||||
|
||||
test('skip automatic reply.send() with reply.sent = true and an error', (t) => {
|
||||
test('skip automatic reply.send() with reply.hijack and an error', async (t) => {
|
||||
const stream = split(JSON.parse)
|
||||
const app = Fastify({
|
||||
logger: {
|
||||
@@ -90,25 +90,25 @@ test('skip automatic reply.send() with reply.sent = true and an error', (t) => {
|
||||
stream.on('data', (line) => {
|
||||
if (line.level === 50) {
|
||||
errorSeen = true
|
||||
t.equal(line.err.message, 'kaboom')
|
||||
t.equal(line.msg, 'Promise errored, but reply.sent = true was set')
|
||||
t.assert.strictEqual(line.err.message, 'kaboom')
|
||||
t.assert.strictEqual(line.msg, 'Promise errored, but reply.sent = true was set')
|
||||
}
|
||||
})
|
||||
|
||||
app.get('/', (req, reply) => {
|
||||
reply.sent = true
|
||||
reply.hijack()
|
||||
reply.raw.end('hello world')
|
||||
|
||||
return Promise.reject(new Error('kaboom'))
|
||||
})
|
||||
|
||||
return app.inject({
|
||||
await app.inject({
|
||||
method: 'GET',
|
||||
url: '/'
|
||||
}).then((res) => {
|
||||
t.equal(errorSeen, true)
|
||||
t.equal(res.statusCode, 200)
|
||||
t.equal(res.body, 'hello world')
|
||||
t.assert.strictEqual(errorSeen, true)
|
||||
t.assert.strictEqual(res.statusCode, 200)
|
||||
t.assert.strictEqual(res.body, 'hello world')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -117,11 +117,8 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
||||
const previousHooks = lifecycleHooks.slice(0, idx)
|
||||
const nextHooks = lifecycleHooks.slice(idx + 1)
|
||||
|
||||
test(`Hijacking inside ${hookOrHandler} skips all the following hooks and handler execution`, t => {
|
||||
t.plan(4)
|
||||
const test = t.test
|
||||
|
||||
test('Sending a response using reply.raw => onResponse hook is called', t => {
|
||||
describe(`Hijacking inside ${hookOrHandler} skips all the following hooks and handler execution`, () => {
|
||||
test('Sending a response using reply.raw => onResponse hook is called', async (t) => {
|
||||
const stream = split(JSON.parse)
|
||||
const app = Fastify({
|
||||
logger: {
|
||||
@@ -130,11 +127,11 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
||||
})
|
||||
|
||||
stream.on('data', (line) => {
|
||||
t.not(line.level, 40) // there are no errors
|
||||
t.not(line.level, 50) // there are no errors
|
||||
t.assert.notStrictEqual(line.level, 40) // there are no errors
|
||||
t.assert.notStrictEqual(line.level, 50) // there are no errors
|
||||
})
|
||||
|
||||
previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.pass(`${h} should be called`)))
|
||||
previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.assert.ok(`${h} should be called`)))
|
||||
|
||||
if (hookOrHandler === 'handler') {
|
||||
app.get('/', (req, reply) => {
|
||||
@@ -146,41 +143,41 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
||||
reply.hijack()
|
||||
reply.raw.end(`hello from ${hookOrHandler}`)
|
||||
})
|
||||
app.get('/', (req, reply) => t.fail('Handler should not be called'))
|
||||
app.get('/', (req, reply) => t.assert.fail('Handler should not be called'))
|
||||
}
|
||||
|
||||
nextHooks.forEach(h => {
|
||||
if (h === 'onResponse') {
|
||||
app.addHook(h, async (req, reply) => t.pass(`${h} should be called`))
|
||||
app.addHook(h, async (req, reply) => t.assert.ok(`${h} should be called`))
|
||||
} else {
|
||||
app.addHook(h, async (req, reply) => t.fail(`${h} should not be called`))
|
||||
app.addHook(h, async (req, reply) => t.assert.fail(`${h} should not be called`))
|
||||
}
|
||||
})
|
||||
|
||||
return app.inject({
|
||||
await app.inject({
|
||||
method: 'GET',
|
||||
url: '/'
|
||||
}).then((res) => {
|
||||
t.equal(res.statusCode, 200)
|
||||
t.equal(res.body, `hello from ${hookOrHandler}`)
|
||||
t.assert.strictEqual(res.statusCode, 200)
|
||||
t.assert.strictEqual(res.body, `hello from ${hookOrHandler}`)
|
||||
})
|
||||
})
|
||||
|
||||
test('Sending a response using req.socket => onResponse not called', t => {
|
||||
test('Sending a response using req.socket => onResponse not called', (t, testDone) => {
|
||||
const stream = split(JSON.parse)
|
||||
const app = Fastify({
|
||||
logger: {
|
||||
stream
|
||||
}
|
||||
})
|
||||
t.teardown(() => app.close())
|
||||
t.after(() => app.close())
|
||||
|
||||
stream.on('data', (line) => {
|
||||
t.not(line.level, 40) // there are no errors
|
||||
t.not(line.level, 50) // there are no errors
|
||||
t.assert.notStrictEqual(line.level, 40) // there are no errors
|
||||
t.assert.notStrictEqual(line.level, 50) // there are no errors
|
||||
})
|
||||
|
||||
previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.pass(`${h} should be called`)))
|
||||
previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.assert.ok(`${h} should be called`)))
|
||||
|
||||
if (hookOrHandler === 'handler') {
|
||||
app.get('/', (req, reply) => {
|
||||
@@ -196,13 +193,13 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
||||
req.socket.write(`hello from ${hookOrHandler}`)
|
||||
req.socket.end()
|
||||
})
|
||||
app.get('/', (req, reply) => t.fail('Handler should not be called'))
|
||||
app.get('/', (req, reply) => t.assert.fail('Handler should not be called'))
|
||||
}
|
||||
|
||||
nextHooks.forEach(h => app.addHook(h, async (req, reply) => t.fail(`${h} should not be called`)))
|
||||
nextHooks.forEach(h => app.addHook(h, async (req, reply) => t.assert.fail(`${h} should not be called`)))
|
||||
|
||||
app.listen({ port: 0 }, err => {
|
||||
t.error(err)
|
||||
t.assert.ifError(err)
|
||||
const client = net.createConnection({ port: (app.server.address()).port }, () => {
|
||||
client.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
|
||||
|
||||
@@ -213,64 +210,63 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
||||
})
|
||||
|
||||
client.on('end', function () {
|
||||
t.match(chunks, new RegExp(`hello from ${hookOrHandler}`, 'i'))
|
||||
t.end()
|
||||
t.assert.match(chunks, new RegExp(`hello from ${hookOrHandler}`, 'i'))
|
||||
testDone()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Throwing an error does not trigger any hooks', t => {
|
||||
test('Throwing an error does not trigger any hooks', async (t) => {
|
||||
const stream = split(JSON.parse)
|
||||
const app = Fastify({
|
||||
logger: {
|
||||
stream
|
||||
}
|
||||
})
|
||||
t.teardown(() => app.close())
|
||||
t.after(() => app.close())
|
||||
|
||||
let errorSeen = false
|
||||
stream.on('data', (line) => {
|
||||
if (hookOrHandler === 'handler') {
|
||||
if (line.level === 40) {
|
||||
errorSeen = true
|
||||
t.equal(line.err.code, 'FST_ERR_REP_ALREADY_SENT')
|
||||
t.assert.strictEqual(line.err.code, 'FST_ERR_REP_ALREADY_SENT')
|
||||
}
|
||||
} else {
|
||||
t.not(line.level, 40) // there are no errors
|
||||
t.not(line.level, 50) // there are no errors
|
||||
t.assert.notStrictEqual(line.level, 40) // there are no errors
|
||||
t.assert.notStrictEqual(line.level, 50) // there are no errors
|
||||
}
|
||||
})
|
||||
|
||||
previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.pass(`${h} should be called`)))
|
||||
previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.assert.ok(`${h} should be called`)))
|
||||
|
||||
if (hookOrHandler === 'handler') {
|
||||
app.get('/', (req, reply) => {
|
||||
reply.hijack()
|
||||
throw new Error('This wil be skipped')
|
||||
throw new Error('This will be skipped')
|
||||
})
|
||||
} else {
|
||||
app.addHook(hookOrHandler, async (req, reply) => {
|
||||
reply.hijack()
|
||||
throw new Error('This wil be skipped')
|
||||
throw new Error('This will be skipped')
|
||||
})
|
||||
app.get('/', (req, reply) => t.fail('Handler should not be called'))
|
||||
app.get('/', (req, reply) => t.assert.fail('Handler should not be called'))
|
||||
}
|
||||
|
||||
nextHooks.forEach(h => app.addHook(h, async (req, reply) => t.fail(`${h} should not be called`)))
|
||||
nextHooks.forEach(h => app.addHook(h, async (req, reply) => t.assert.fail(`${h} should not be called`)))
|
||||
|
||||
return Promise.race([
|
||||
await Promise.race([
|
||||
app.inject({ method: 'GET', url: '/' }),
|
||||
new Promise((resolve, reject) => setTimeout(resolve, 1000))
|
||||
]).then((err, res) => {
|
||||
t.error(err)
|
||||
if (hookOrHandler === 'handler') {
|
||||
t.equal(errorSeen, true)
|
||||
}
|
||||
})
|
||||
])
|
||||
|
||||
if (hookOrHandler === 'handler') {
|
||||
t.assert.strictEqual(errorSeen, true)
|
||||
}
|
||||
})
|
||||
|
||||
test('Calling reply.send() after hijacking logs a warning', t => {
|
||||
test('Calling reply.send() after hijacking logs a warning', async (t) => {
|
||||
const stream = split(JSON.parse)
|
||||
const app = Fastify({
|
||||
logger: {
|
||||
@@ -283,11 +279,11 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
||||
stream.on('data', (line) => {
|
||||
if (line.level === 40) {
|
||||
errorSeen = true
|
||||
t.equal(line.err.code, 'FST_ERR_REP_ALREADY_SENT')
|
||||
t.assert.strictEqual(line.err.code, 'FST_ERR_REP_ALREADY_SENT')
|
||||
}
|
||||
})
|
||||
|
||||
previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.pass(`${h} should be called`)))
|
||||
previousHooks.forEach(h => app.addHook(h, async (req, reply) => t.assert.ok(`${h} should be called`)))
|
||||
|
||||
if (hookOrHandler === 'handler') {
|
||||
app.get('/', (req, reply) => {
|
||||
@@ -299,18 +295,17 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
|
||||
reply.hijack()
|
||||
return reply.send('hello from reply.send()')
|
||||
})
|
||||
app.get('/', (req, reply) => t.fail('Handler should not be called'))
|
||||
app.get('/', (req, reply) => t.assert.fail('Handler should not be called'))
|
||||
}
|
||||
|
||||
nextHooks.forEach(h => app.addHook(h, async (req, reply) => t.fail(`${h} should not be called`)))
|
||||
nextHooks.forEach(h => app.addHook(h, async (req, reply) => t.assert.fail(`${h} should not be called`)))
|
||||
|
||||
return Promise.race([
|
||||
await Promise.race([
|
||||
app.inject({ method: 'GET', url: '/' }),
|
||||
new Promise((resolve, reject) => setTimeout(resolve, 1000))
|
||||
]).then((err, res) => {
|
||||
t.error(err)
|
||||
t.equal(errorSeen, true)
|
||||
})
|
||||
])
|
||||
|
||||
t.assert.strictEqual(errorSeen, true)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user