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,20 +1,14 @@
'use strict'
const t = require('tap')
const test = t.test
const sget = require('simple-get').concat
const fs = require('node:fs')
const { test } = require('node:test')
const errors = require('http-errors')
const JSONStream = require('JSONStream')
const send = require('send')
const Readable = require('node:stream').Readable
const split = require('split2')
const semver = require('semver')
const Fastify = require('..')
const { kDisableRequestLogging } = require('../lib/symbols.js')
const { getServerUrl } = require('./helper')
test('Destroying streams prematurely should call abort method', t => {
test('Destroying streams prematurely should call abort method', (t, testDone) => {
t.plan(7)
let fastify = null
@@ -27,7 +21,7 @@ test('Destroying streams prematurely should call abort method', t => {
}
})
} catch (e) {
t.fail()
t.assert.fail()
}
const stream = require('node:stream')
const http = require('node:http')
@@ -35,13 +29,14 @@ test('Destroying streams prematurely should call abort method', t => {
// Test that "premature close" errors are logged with level warn
logStream.on('data', line => {
if (line.res) {
t.equal(line.msg, 'stream closed prematurely')
t.equal(line.level, 30)
t.assert.strictEqual(line.msg, 'stream closed prematurely')
t.assert.strictEqual(line.level, 30)
testDone()
}
})
fastify.get('/', function (request, reply) {
t.pass('Received request')
t.assert.ok('Received request')
let sent = false
const reallyLongStream = new stream.Readable({
@@ -54,30 +49,30 @@ test('Destroying streams prematurely should call abort method', t => {
})
reallyLongStream.destroy = undefined
reallyLongStream.close = undefined
reallyLongStream.abort = () => t.ok('called')
reallyLongStream.abort = () => t.assert.ok('called')
reply.send(reallyLongStream)
})
fastify.listen({ port: 0 }, err => {
t.error(err)
t.teardown(() => { fastify.close() })
t.assert.ifError(err)
t.after(() => { fastify.close() })
const port = fastify.server.address().port
http.get(`http://localhost:${port}`, function (response) {
t.equal(response.statusCode, 200)
t.assert.strictEqual(response.statusCode, 200)
response.on('readable', function () {
response.destroy()
})
// Node bug? Node never emits 'close' here.
response.on('aborted', function () {
t.pass('Response closed')
t.assert.ok('Response closed')
})
})
})
})
test('Destroying streams prematurely, log is disabled', t => {
test('Destroying streams prematurely, log is disabled', (t, testDone) => {
t.plan(4)
let fastify = null
@@ -86,7 +81,7 @@ test('Destroying streams prematurely, log is disabled', t => {
logger: false
})
} catch (e) {
t.fail()
t.assert.fail()
}
const stream = require('node:stream')
const http = require('node:http')
@@ -104,31 +99,34 @@ test('Destroying streams prematurely, log is disabled', t => {
}
})
reallyLongStream.destroy = true
reallyLongStream.close = () => t.ok('called')
reallyLongStream.close = () => {
t.assert.ok('called')
testDone()
}
reply.send(reallyLongStream)
})
fastify.listen({ port: 0 }, err => {
t.error(err)
t.teardown(() => { fastify.close() })
t.assert.ifError(err)
t.after(() => { fastify.close() })
const port = fastify.server.address().port
http.get(`http://localhost:${port}`, function (response) {
t.equal(response.statusCode, 200)
t.assert.strictEqual(response.statusCode, 200)
response.on('readable', function () {
response.destroy()
})
// Node bug? Node never emits 'close' here.
response.on('aborted', function () {
t.pass('Response closed')
t.assert.ok('Response closed')
})
})
})
})
test('should respond with a stream1', t => {
t.plan(5)
test('should respond with a stream1', async (t) => {
t.plan(4)
const fastify = Fastify()
fastify.get('/', function (req, reply) {
@@ -138,26 +136,24 @@ test('should respond with a stream1', t => {
stream.end({ a: 42 })
})
fastify.listen({ port: 0 }, err => {
t.error(err)
t.teardown(() => { fastify.close() })
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => fastify.close())
sget(`http://localhost:${fastify.server.address().port}`, function (err, response, body) {
t.error(err)
t.equal(response.headers['content-type'], 'application/json')
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), [{ hello: 'world' }, { a: 42 }])
})
})
const response = await fetch(fastifyServer)
t.assert.ok(response.ok)
t.assert.strictEqual(response.headers.get('content-type'), 'application/json')
t.assert.strictEqual(response.status, 200)
const body = await response.text()
t.assert.deepStrictEqual(JSON.parse(body), [{ hello: 'world' }, { a: 42 }])
})
test('return a 404 if the stream emits a 404 error', t => {
t.plan(5)
test('return a 404 if the stream emits a 404 error', async (t) => {
t.plan(4)
const fastify = Fastify()
fastify.get('/', function (request, reply) {
t.pass('Received request')
t.assert.ok('Received request')
const reallyLongStream = new Readable({
read: function () {
@@ -170,54 +166,11 @@ test('return a 404 if the stream emits a 404 error', t => {
reply.send(reallyLongStream)
})
fastify.listen({ port: 0 }, err => {
t.error(err)
t.teardown(() => { fastify.close() })
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => fastify.close())
const port = fastify.server.address().port
sget(`http://localhost:${port}`, function (err, response) {
t.error(err)
t.equal(response.headers['content-type'], 'application/json; charset=utf-8')
t.equal(response.statusCode, 404)
})
})
})
test('should support send module 200 and 404', { skip: semver.gte(process.versions.node, '17.0.0') }, t => {
t.plan(8)
const fastify = Fastify()
fastify.get('/', function (req, reply) {
const stream = send(req.raw, __filename)
reply.code(200).send(stream)
})
fastify.get('/error', function (req, reply) {
const stream = send(req.raw, 'non-existing-file')
reply.code(200).send(stream)
})
fastify.listen({ port: 0 }, err => {
t.error(err)
t.teardown(() => { fastify.close() })
const url = getServerUrl(fastify)
sget(url, function (err, response, data) {
t.error(err)
t.equal(response.headers['content-type'], 'application/javascript; charset=UTF-8')
t.equal(response.statusCode, 200)
fs.readFile(__filename, (err, expected) => {
t.error(err)
t.equal(expected.toString(), data.toString())
})
})
sget(url + '/error', function (err, response) {
t.error(err)
t.equal(response.statusCode, 404)
})
})
const response = await fetch(fastifyServer)
t.assert.ok(!response.ok)
t.assert.strictEqual(response.headers.get('content-type'), 'application/json; charset=utf-8')
t.assert.strictEqual(response.status, 404)
})