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,13 +1,13 @@
'use strict'
const stream = require('node:stream')
const { ReadableStream } = require('node:stream/web')
const split = require('split2')
const t = require('tap')
const test = t.test
const { test } = require('node:test')
const Fastify = require('..')
const proxyquire = require('proxyquire')
const createError = require('@fastify/error')
test("HEAD route should handle stream.on('error')", t => {
test("HEAD route should handle stream.on('error')", (t, done) => {
t.plan(6)
const resStream = stream.Readable.from('Hello with error!')
@@ -32,24 +32,73 @@ test("HEAD route should handle stream.on('error')", t => {
logStream.once('data', line => {
const { message, stack } = expectedError
t.same(line.err, { type: 'Error', message, stack })
t.equal(line.msg, 'Error on Stream found for HEAD route')
t.equal(line.level, 50)
t.assert.deepStrictEqual(line.err, { type: 'Error', message, stack })
t.assert.strictEqual(line.msg, 'Error on Stream found for HEAD route')
t.assert.strictEqual(line.level, 50)
})
fastify.inject({
method: 'HEAD',
url: '/more-coffee'
}, (error, res) => {
t.error(error)
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], undefined)
t.assert.ifError(error)
t.assert.strictEqual(res.statusCode, 200)
t.assert.strictEqual(res.headers['content-type'], undefined)
done()
})
})
test('HEAD route should be exposed by default', t => {
test('HEAD route should handle ReadableStream.cancel() error', (t, done) => {
t.plan(7)
const logStream = split(JSON.parse)
const expectedError = new Error('Cancel error!')
const fastify = Fastify({
logger: {
stream: logStream,
level: 'error'
}
})
fastify.route({
method: 'GET',
path: '/web-stream',
exposeHeadRoute: true,
handler: (req, reply) => {
const webStream = new ReadableStream({
start (controller) {
controller.enqueue('Hello from web stream!')
},
cancel (reason) {
t.assert.strictEqual(reason, 'Stream cancelled by HEAD route')
throw expectedError
}
})
return webStream
}
})
logStream.once('data', line => {
const { message, stack } = expectedError
t.assert.deepStrictEqual(line.err, { type: 'Error', message, stack })
t.assert.strictEqual(line.msg, 'Error on Stream found for HEAD route')
t.assert.strictEqual(line.level, 50)
})
fastify.inject({
method: 'HEAD',
url: '/web-stream'
}, (error, res) => {
t.assert.ifError(error)
t.assert.strictEqual(res.statusCode, 200)
t.assert.strictEqual(res.headers['content-type'], undefined)
done()
})
})
test('HEAD route should be exposed by default', async t => {
t.plan(5)
const resStream = stream.Readable.from('Hello with error!')
const resJson = { hello: 'world' }
const fastify = Fastify()
@@ -71,28 +120,24 @@ test('HEAD route should be exposed by default', t => {
}
})
fastify.inject({
let res = await fastify.inject({
method: 'HEAD',
url: '/without-flag'
}, (error, res) => {
t.error(error)
t.equal(res.statusCode, 200)
})
t.assert.strictEqual(res.statusCode, 200)
fastify.inject({
res = await fastify.inject({
method: 'HEAD',
url: '/with-flag'
}, (error, res) => {
t.error(error)
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'application/json; charset=utf-8')
t.equal(res.headers['content-length'], `${Buffer.byteLength(JSON.stringify(resJson))}`)
t.equal(res.body, '')
})
t.assert.strictEqual(res.statusCode, 200)
t.assert.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
t.assert.strictEqual(res.headers['content-length'], `${Buffer.byteLength(JSON.stringify(resJson))}`)
t.assert.strictEqual(res.body, '')
})
test('HEAD route should be exposed if route exposeHeadRoute is set', t => {
t.plan(7)
test('HEAD route should be exposed if route exposeHeadRoute is set', async t => {
t.plan(5)
const resBuffer = Buffer.from('I am a coffee!')
const resJson = { hello: 'world' }
@@ -115,27 +160,23 @@ test('HEAD route should be exposed if route exposeHeadRoute is set', t => {
}
})
fastify.inject({
let res = await fastify.inject({
method: 'HEAD',
url: '/one'
}, (error, res) => {
t.error(error)
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'application/octet-stream')
t.equal(res.headers['content-length'], `${resBuffer.byteLength}`)
t.equal(res.body, '')
})
t.assert.strictEqual(res.statusCode, 200)
t.assert.strictEqual(res.headers['content-type'], 'application/octet-stream')
t.assert.strictEqual(res.headers['content-length'], `${resBuffer.byteLength}`)
t.assert.strictEqual(res.body, '')
fastify.inject({
res = await fastify.inject({
method: 'HEAD',
url: '/two'
}, (error, res) => {
t.error(error)
t.equal(res.statusCode, 404)
})
t.assert.strictEqual(res.statusCode, 404)
})
test('Set a custom HEAD route before GET one without disabling exposeHeadRoutes (global)', t => {
test('Set a custom HEAD route before GET one without disabling exposeHeadRoutes (global)', (t, done) => {
t.plan(6)
const resBuffer = Buffer.from('I am a coffee!')
@@ -166,28 +207,20 @@ test('Set a custom HEAD route before GET one without disabling exposeHeadRoutes
method: 'HEAD',
url: '/one'
}, (error, res) => {
t.error(error)
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'application/pdf')
t.equal(res.headers['content-length'], `${resBuffer.byteLength}`)
t.equal(res.headers['x-custom-header'], 'some-custom-header')
t.equal(res.body, '')
t.assert.ifError(error)
t.assert.strictEqual(res.statusCode, 200)
t.assert.strictEqual(res.headers['content-type'], 'application/pdf')
t.assert.strictEqual(res.headers['content-length'], `${resBuffer.byteLength}`)
t.assert.strictEqual(res.headers['x-custom-header'], 'some-custom-header')
t.assert.strictEqual(res.body, '')
done()
})
})
test('Set a custom HEAD route before GET one without disabling exposeHeadRoutes (route)', t => {
t.plan(7)
test('Set a custom HEAD route before GET one without disabling exposeHeadRoutes (route)', (t, done) => {
t.plan(6)
function onWarning () {
t.pass('warning emitted')
}
const route = proxyquire('../lib/route', {
'./warnings': {
FSTDEP007: onWarning
}
})
const fastify = proxyquire('..', { './lib/route.js': route })()
const fastify = Fastify()
const resBuffer = Buffer.from('I am a coffee!')
@@ -215,16 +248,17 @@ test('Set a custom HEAD route before GET one without disabling exposeHeadRoutes
method: 'HEAD',
url: '/one'
}, (error, res) => {
t.error(error)
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'application/pdf')
t.equal(res.headers['content-length'], `${resBuffer.byteLength}`)
t.equal(res.headers['x-custom-header'], 'some-custom-header')
t.equal(res.body, '')
t.assert.ifError(error)
t.assert.strictEqual(res.statusCode, 200)
t.assert.strictEqual(res.headers['content-type'], 'application/pdf')
t.assert.strictEqual(res.headers['content-length'], `${resBuffer.byteLength}`)
t.assert.strictEqual(res.headers['x-custom-header'], 'some-custom-header')
t.assert.strictEqual(res.body, '')
done()
})
})
test('HEAD routes properly auto created for GET routes when prefixTrailingSlash: \'no-slash\'', t => {
test('HEAD routes properly auto created for GET routes when prefixTrailingSlash: \'no-slash\'', (t, done) => {
t.plan(2)
const fastify = Fastify()
@@ -244,8 +278,9 @@ test('HEAD routes properly auto created for GET routes when prefixTrailingSlash:
}, { prefix: '/prefix' })
fastify.inject({ url: '/prefix/prefix', method: 'HEAD' }, (err, res) => {
t.error(err)
t.equal(res.statusCode, 404)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 404)
done()
})
})
@@ -272,9 +307,9 @@ test('HEAD routes properly auto created for GET routes when prefixTrailingSlash:
const trailingSlashReply = await fastify.inject({ url: '/prefix/', method: 'HEAD' })
const noneTrailingReply = await fastify.inject({ url: '/prefix', method: 'HEAD' })
t.equal(doublePrefixReply.statusCode, 404)
t.equal(trailingSlashReply.statusCode, 200)
t.equal(noneTrailingReply.statusCode, 200)
t.assert.strictEqual(doublePrefixReply.statusCode, 404)
t.assert.strictEqual(trailingSlashReply.statusCode, 200)
t.assert.strictEqual(noneTrailingReply.statusCode, 200)
})
test('GET route with body schema should throw', t => {
@@ -282,7 +317,7 @@ test('GET route with body schema should throw', t => {
const fastify = Fastify()
t.throws(() => {
t.assert.throws(() => {
fastify.route({
method: 'GET',
path: '/get',
@@ -293,7 +328,7 @@ test('GET route with body schema should throw', t => {
reply.send({ hello: 'world' })
}
})
}, new Error('Body validation schema for GET:/get route is not supported!'))
}, createError('FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED', 'Body validation schema for GET:/get route is not supported!')())
})
test('HEAD route with body schema should throw', t => {
@@ -301,7 +336,7 @@ test('HEAD route with body schema should throw', t => {
const fastify = Fastify()
t.throws(() => {
t.assert.throws(() => {
fastify.route({
method: 'HEAD',
path: '/shouldThrow',
@@ -312,7 +347,7 @@ test('HEAD route with body schema should throw', t => {
reply.send({ hello: 'world' })
}
})
}, new Error('Body validation schema for HEAD:/shouldThrow route is not supported!'))
}, createError('FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED', 'Body validation schema for HEAD:/shouldThrow route is not supported!')())
})
test('[HEAD, GET] route with body schema should throw', t => {
@@ -320,7 +355,7 @@ test('[HEAD, GET] route with body schema should throw', t => {
const fastify = Fastify()
t.throws(() => {
t.assert.throws(() => {
fastify.route({
method: ['HEAD', 'GET'],
path: '/shouldThrowHead',
@@ -331,7 +366,7 @@ test('[HEAD, GET] route with body schema should throw', t => {
reply.send({ hello: 'world' })
}
})
}, new Error('Body validation schema for HEAD:/shouldThrowHead route is not supported!'))
}, createError('FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED', 'Body validation schema for HEAD:/shouldThrowHead route is not supported!')())
})
test('GET route with body schema should throw - shorthand', t => {
@@ -339,7 +374,7 @@ test('GET route with body schema should throw - shorthand', t => {
const fastify = Fastify()
t.throws(() => {
t.assert.throws(() => {
fastify.get('/shouldThrow', {
schema: {
body: {}
@@ -349,7 +384,7 @@ test('GET route with body schema should throw - shorthand', t => {
reply.send({ hello: 'world' })
}
)
}, new Error('Body validation schema for GET:/shouldThrow route is not supported!'))
}, createError('FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED', 'Body validation schema for GET:/shouldThrow route is not supported!')())
})
test('HEAD route with body schema should throw - shorthand', t => {
@@ -357,7 +392,7 @@ test('HEAD route with body schema should throw - shorthand', t => {
const fastify = Fastify()
t.throws(() => {
t.assert.throws(() => {
fastify.head('/shouldThrow2', {
schema: {
body: {}
@@ -367,5 +402,5 @@ test('HEAD route with body schema should throw - shorthand', t => {
reply.send({ hello: 'world' })
}
)
}, new Error('Body validation schema for HEAD:/shouldThrow2 route is not supported!'))
}, createError('FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED', 'Body validation schema for HEAD:/shouldThrow2 route is not supported!')())
})