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

@@ -1,17 +1,11 @@
'use strict'
const t = require('tap')
const test = t.test
const { test } = require('node:test')
const Fastify = require('../fastify')
const fs = require('node:fs')
const semver = require('semver')
const { Readable } = require('node:stream')
const { fetch: undiciFetch } = require('undici')
if (semver.lt(process.versions.node, '18.0.0')) {
t.skip('Response or ReadableStream not available, skipping test')
process.exit(0)
}
const http = require('node:http')
test('should response with a ReadableStream', async (t) => {
t.plan(2)
@@ -30,8 +24,8 @@ test('should response with a ReadableStream', async (t) => {
const expected = await fs.promises.readFile(__filename)
t.equal(statusCode, 200)
t.equal(expected.toString(), body.toString())
t.assert.strictEqual(statusCode, 200)
t.assert.strictEqual(expected.toString(), body.toString())
})
test('should response with a Response', async (t) => {
@@ -57,9 +51,9 @@ test('should response with a Response', async (t) => {
const expected = await fs.promises.readFile(__filename)
t.equal(statusCode, 200)
t.equal(expected.toString(), body.toString())
t.equal(headers.hello, 'world')
t.assert.strictEqual(statusCode, 200)
t.assert.strictEqual(expected.toString(), body.toString())
t.assert.strictEqual(headers.hello, 'world')
})
test('should response with a Response 204', async (t) => {
@@ -82,9 +76,9 @@ test('should response with a Response 204', async (t) => {
body
} = await fastify.inject({ method: 'GET', path: '/' })
t.equal(statusCode, 204)
t.equal(body, '')
t.equal(headers.hello, 'world')
t.assert.strictEqual(statusCode, 204)
t.assert.strictEqual(body, '')
t.assert.strictEqual(headers.hello, 'world')
})
test('should response with a Response 304', async (t) => {
@@ -107,9 +101,9 @@ test('should response with a Response 304', async (t) => {
body
} = await fastify.inject({ method: 'GET', path: '/' })
t.equal(statusCode, 304)
t.equal(body, '')
t.equal(headers.hello, 'world')
t.assert.strictEqual(statusCode, 304)
t.assert.strictEqual(body, '')
t.assert.strictEqual(headers.hello, 'world')
})
test('should response with a Response without body', async (t) => {
@@ -132,9 +126,9 @@ test('should response with a Response without body', async (t) => {
body
} = await fastify.inject({ method: 'GET', path: '/' })
t.equal(statusCode, 200)
t.equal(body, '')
t.equal(headers.hello, 'world')
t.assert.strictEqual(statusCode, 200)
t.assert.strictEqual(body, '')
t.assert.strictEqual(headers.hello, 'world')
})
test('able to use in onSend hook - ReadableStream', async (t) => {
@@ -148,7 +142,7 @@ test('able to use in onSend hook - ReadableStream', async (t) => {
})
fastify.addHook('onSend', (request, reply, payload, done) => {
t.equal(Object.prototype.toString.call(payload), '[object ReadableStream]')
t.assert.strictEqual(Object.prototype.toString.call(payload), '[object ReadableStream]')
done(null, new Response(payload, {
status: 200,
headers: {
@@ -165,9 +159,9 @@ test('able to use in onSend hook - ReadableStream', async (t) => {
const expected = await fs.promises.readFile(__filename)
t.equal(statusCode, 200)
t.equal(expected.toString(), body.toString())
t.equal(headers.hello, 'world')
t.assert.strictEqual(statusCode, 200)
t.assert.strictEqual(expected.toString(), body.toString())
t.assert.strictEqual(headers.hello, 'world')
})
test('able to use in onSend hook - Response', async (t) => {
@@ -186,7 +180,7 @@ test('able to use in onSend hook - Response', async (t) => {
})
fastify.addHook('onSend', (request, reply, payload, done) => {
t.equal(Object.prototype.toString.call(payload), '[object Response]')
t.assert.strictEqual(Object.prototype.toString.call(payload), '[object Response]')
done(null, new Response(payload.body, {
status: 200,
headers: payload.headers
@@ -201,9 +195,9 @@ test('able to use in onSend hook - Response', async (t) => {
const expected = await fs.promises.readFile(__filename)
t.equal(statusCode, 200)
t.equal(expected.toString(), body.toString())
t.equal(headers.hello, 'world')
t.assert.strictEqual(statusCode, 200)
t.assert.strictEqual(expected.toString(), body.toString())
t.assert.strictEqual(headers.hello, 'world')
})
test('Error when Response.bodyUsed', async (t) => {
@@ -222,31 +216,81 @@ test('Error when Response.bodyUsed', async (t) => {
}
})
const file = await response.text()
t.equal(expected.toString(), file)
t.equal(response.bodyUsed, true)
t.assert.strictEqual(expected.toString(), file)
t.assert.strictEqual(response.bodyUsed, true)
return reply.send(response)
})
const response = await fastify.inject({ method: 'GET', path: '/' })
t.equal(response.statusCode, 500)
t.assert.strictEqual(response.statusCode, 500)
const body = response.json()
t.equal(body.code, 'FST_ERR_REP_RESPONSE_BODY_CONSUMED')
t.assert.strictEqual(body.code, 'FST_ERR_REP_RESPONSE_BODY_CONSUMED')
})
test('Error when Response.body.locked', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.get('/', async function (request, reply) {
const stream = Readable.toWeb(fs.createReadStream(__filename))
const response = new Response(stream, {
status: 200,
headers: {
hello: 'world'
}
})
stream.getReader()
t.assert.strictEqual(stream.locked, true)
return reply.send(response)
})
const response = await fastify.inject({ method: 'GET', path: '/' })
t.assert.strictEqual(response.statusCode, 500)
const body = response.json()
t.assert.strictEqual(body.code, 'FST_ERR_REP_READABLE_STREAM_LOCKED')
})
test('Error when ReadableStream.locked', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.get('/', async function (request, reply) {
const stream = Readable.toWeb(fs.createReadStream(__filename))
stream.getReader()
t.assert.strictEqual(stream.locked, true)
return reply.send(stream)
})
const response = await fastify.inject({ method: 'GET', path: '/' })
t.assert.strictEqual(response.statusCode, 500)
const body = response.json()
t.assert.strictEqual(body.code, 'FST_ERR_REP_READABLE_STREAM_LOCKED')
})
test('allow to pipe with fetch', async (t) => {
t.plan(2)
const abortController = new AbortController()
const { signal } = abortController
const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
t.after(() => {
fastify.close()
abortController.abort()
})
fastify.get('/', function (request, reply) {
return fetch(`${fastify.listeningOrigin}/fetch`, {
method: 'GET'
method: 'GET',
signal
})
})
fastify.get('/fetch', function (request, reply) {
fastify.get('/fetch', function async (request, reply) {
reply.code(200).send({ ok: true })
})
@@ -254,19 +298,25 @@ test('allow to pipe with fetch', async (t) => {
const response = await fastify.inject({ method: 'GET', path: '/' })
t.equal(response.statusCode, 200)
t.same(response.json(), { ok: true })
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(response.json(), { ok: true })
})
test('allow to pipe with undici.fetch', async (t) => {
t.plan(2)
const abortController = new AbortController()
const { signal } = abortController
const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
t.after(() => {
fastify.close()
abortController.abort()
})
fastify.get('/', function (request, reply) {
return undiciFetch(`${fastify.listeningOrigin}/fetch`, {
method: 'GET'
method: 'GET',
signal
})
})
@@ -278,6 +328,141 @@ test('allow to pipe with undici.fetch', async (t) => {
const response = await fastify.inject({ method: 'GET', path: '/' })
t.equal(response.statusCode, 200)
t.same(response.json(), { ok: true })
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(response.json(), { ok: true })
})
test('WebStream error before headers sent should trigger error handler', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify.get('/', function (request, reply) {
const stream = new ReadableStream({
start (controller) {
controller.error(new Error('stream error'))
}
})
reply.send(stream)
})
const response = await fastify.inject({ method: 'GET', path: '/' })
t.assert.strictEqual(response.statusCode, 500)
t.assert.strictEqual(response.json().message, 'stream error')
})
test('WebStream error after headers sent should destroy response', (t, done) => {
t.plan(2)
const fastify = Fastify()
t.after(() => fastify.close())
fastify.get('/', function (request, reply) {
const stream = new ReadableStream({
start (controller) {
controller.enqueue('hello')
},
pull (controller) {
setTimeout(() => {
controller.error(new Error('stream error'))
}, 10)
}
})
reply.header('content-type', 'text/plain').send(stream)
})
fastify.listen({ port: 0 }, err => {
t.assert.ifError(err)
let finished = false
http.get(`http://localhost:${fastify.server.address().port}`, (res) => {
res.on('close', () => {
if (!finished) {
finished = true
t.assert.ok('response closed')
done()
}
})
res.resume()
})
})
})
test('WebStream should cancel reader when response is destroyed', (t, done) => {
t.plan(2)
const fastify = Fastify()
t.after(() => fastify.close())
let readerCancelled = false
fastify.get('/', function (request, reply) {
const stream = new ReadableStream({
start (controller) {
controller.enqueue('hello')
},
pull (controller) {
return new Promise(() => {})
},
cancel () {
readerCancelled = true
}
})
reply.header('content-type', 'text/plain').send(stream)
})
fastify.listen({ port: 0 }, err => {
t.assert.ifError(err)
const req = http.get(`http://localhost:${fastify.server.address().port}`, (res) => {
res.once('data', () => {
req.destroy()
setTimeout(() => {
t.assert.strictEqual(readerCancelled, true)
done()
}, 50)
})
})
})
})
test('WebStream should warn when headers already sent', async (t) => {
t.plan(2)
let warnCalled = false
const spyLogger = {
level: 'warn',
fatal: () => { },
error: () => { },
warn: (msg) => {
if (typeof msg === 'string' && msg.includes('use res.writeHead in stream mode')) {
warnCalled = true
}
},
info: () => { },
debug: () => { },
trace: () => { },
child: () => spyLogger
}
const fastify = Fastify({ loggerInstance: spyLogger })
t.after(() => fastify.close())
fastify.get('/', function (request, reply) {
reply.raw.writeHead(200, { 'content-type': 'text/plain' })
const stream = new ReadableStream({
start (controller) {
controller.enqueue('hello')
controller.close()
}
})
reply.send(stream)
})
await fastify.listen({ port: 0 })
const response = await fetch(`http://localhost:${fastify.server.address().port}/`)
t.assert.strictEqual(response.status, 200)
t.assert.strictEqual(warnCalled, true)
})