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,70 +1,61 @@
'use strict'
const t = require('tap')
const test = t.test
const sget = require('simple-get').concat
const { test } = require('node:test')
const Fastify = require('../fastify')
const jsonParser = require('fast-json-body')
const { getServerUrl } = require('./helper')
process.removeAllListeners('warning')
test('Should have typeof body object with no custom parser defined, null body and content type = \'text/plain\'', t => {
t.plan(4)
test('Should have typeof body object with no custom parser defined, null body and content type = \'text/plain\'', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.post('/', (req, reply) => {
reply.send(req.body)
})
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => fastify.close())
sget({
method: 'POST',
url: getServerUrl(fastify),
body: null,
headers: {
'Content-Type': 'text/plain'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(typeof body, 'object')
fastify.close()
})
const result = await fetch(fastifyServer, {
method: 'POST',
body: null,
headers: {
'Content-Type': 'text/plain'
}
})
t.assert.ok(result.ok)
t.assert.strictEqual(result.status, 200)
t.assert.strictEqual(await result.text(), '')
})
test('Should have typeof body object with no custom parser defined, undefined body and content type = \'text/plain\'', t => {
t.plan(4)
test('Should have typeof body object with no custom parser defined, undefined body and content type = \'text/plain\'', async (t) => {
t.plan(3)
const fastify = Fastify()
fastify.post('/', (req, reply) => {
reply.send(req.body)
})
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => fastify.close())
sget({
method: 'POST',
url: getServerUrl(fastify),
body: undefined,
headers: {
'Content-Type': 'text/plain'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(typeof body, 'object')
fastify.close()
})
const result = await fetch(fastifyServer, {
method: 'POST',
body: undefined,
headers: {
'Content-Type': 'text/plain'
}
})
t.assert.ok(result.ok)
t.assert.strictEqual(result.status, 200)
t.assert.strictEqual(await result.text(), '')
})
test('Should get the body as string', t => {
t.plan(6)
test('Should get the body as string /1', async (t) => {
t.plan(4)
const fastify = Fastify()
fastify.post('/', (req, reply) => {
@@ -72,8 +63,8 @@ test('Should get the body as string', t => {
})
fastify.addContentTypeParser('text/plain', { parseAs: 'string' }, function (req, body, done) {
t.ok('called')
t.ok(typeof body === 'string')
t.assert.ok('called')
t.assert.ok(typeof body === 'string')
try {
const plainText = body
done(null, plainText)
@@ -83,27 +74,58 @@ test('Should get the body as string', t => {
}
})
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => fastify.close())
sget({
method: 'POST',
url: getServerUrl(fastify),
body: 'hello world',
headers: {
'Content-Type': 'text/plain'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), 'hello world')
fastify.close()
})
const result = await fetch(fastifyServer, {
method: 'POST',
body: 'hello world',
headers: {
'Content-Type': 'text/plain'
}
})
t.assert.strictEqual(result.status, 200)
t.assert.strictEqual(await result.text(), 'hello world')
})
test('Should get the body as buffer', t => {
t.plan(6)
test('Should get the body as string /2', async (t) => {
t.plan(4)
const fastify = Fastify()
fastify.post('/', (req, reply) => {
reply.send(req.body)
})
fastify.addContentTypeParser('text/plain/test', { parseAs: 'string' }, function (req, body, done) {
t.assert.ok('called')
t.assert.ok(typeof body === 'string')
try {
const plainText = body
done(null, plainText)
} catch (err) {
err.statusCode = 400
done(err, undefined)
}
})
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => fastify.close())
const result = await fetch(fastifyServer, {
method: 'POST',
body: 'hello world',
headers: {
'Content-Type': ' text/plain/test '
}
})
t.assert.strictEqual(result.status, 200)
t.assert.strictEqual(await result.text(), 'hello world')
})
test('Should get the body as buffer', async (t) => {
t.plan(4)
const fastify = Fastify()
fastify.post('/', (req, reply) => {
@@ -111,8 +133,8 @@ test('Should get the body as buffer', t => {
})
fastify.addContentTypeParser('application/json', { parseAs: 'buffer' }, function (req, body, done) {
t.ok('called')
t.ok(body instanceof Buffer)
t.assert.ok('called')
t.assert.ok(body instanceof Buffer)
try {
const json = JSON.parse(body)
done(null, json)
@@ -122,27 +144,23 @@ test('Should get the body as buffer', t => {
}
})
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => fastify.close())
sget({
method: 'POST',
url: getServerUrl(fastify),
body: '{"hello":"world"}',
headers: {
'Content-Type': 'application/json'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), '{"hello":"world"}')
fastify.close()
})
const result = await fetch(fastifyServer, {
method: 'POST',
body: '{"hello":"world"}',
headers: {
'Content-Type': 'application/json'
}
})
t.assert.strictEqual(result.status, 200)
t.assert.strictEqual(await result.text(), '{"hello":"world"}')
})
test('Should get the body as buffer', t => {
t.plan(6)
test('Should get the body as buffer', async (t) => {
t.plan(4)
const fastify = Fastify()
fastify.post('/', (req, reply) => {
@@ -150,8 +168,8 @@ test('Should get the body as buffer', t => {
})
fastify.addContentTypeParser('text/plain', { parseAs: 'buffer' }, function (req, body, done) {
t.ok('called')
t.ok(body instanceof Buffer)
t.assert.ok('called')
t.assert.ok(body instanceof Buffer)
try {
const plainText = body
done(null, plainText)
@@ -161,31 +179,27 @@ test('Should get the body as buffer', t => {
}
})
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => fastify.close())
sget({
method: 'POST',
url: getServerUrl(fastify),
body: 'hello world',
headers: {
'Content-Type': 'text/plain'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), 'hello world')
fastify.close()
})
const result = await fetch(fastifyServer, {
method: 'POST',
body: 'hello world',
headers: {
'Content-Type': 'text/plain'
}
})
t.assert.strictEqual(result.status, 200)
t.assert.strictEqual(await result.text(), 'hello world')
})
test('Should parse empty bodies as a string', t => {
t.plan(9)
test('Should parse empty bodies as a string', async (t) => {
t.plan(8)
const fastify = Fastify()
fastify.addContentTypeParser('text/plain', { parseAs: 'string' }, (req, body, done) => {
t.equal(body, '')
t.assert.strictEqual(body, '')
done(null, body)
})
@@ -197,41 +211,37 @@ test('Should parse empty bodies as a string', t => {
}
})
fastify.listen({ port: 0 }, err => {
t.error(err)
t.teardown(() => { fastify.close() })
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => fastify.close())
sget({
method: 'POST',
url: getServerUrl(fastify),
body: '',
headers: {
'Content-Type': 'text/plain'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), '')
})
sget({
method: 'DELETE',
url: getServerUrl(fastify),
body: '',
headers: {
'Content-Type': 'text/plain',
'Content-Length': '0'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), '')
})
const postResult = await fetch(fastifyServer, {
method: 'POST',
body: '',
headers: {
'Content-Type': 'text/plain'
}
})
t.assert.ok(postResult.ok)
t.assert.strictEqual(postResult.status, 200)
t.assert.strictEqual(await postResult.text(), '')
const deleteResult = await fetch(fastifyServer, {
method: 'DELETE',
body: '',
headers: {
'Content-Type': 'text/plain',
'Content-Length': '0'
}
})
t.assert.ok(deleteResult.ok)
t.assert.strictEqual(deleteResult.status, 200)
t.assert.strictEqual(await deleteResult.text(), '')
})
test('Should parse empty bodies as a buffer', t => {
t.plan(6)
test('Should parse empty bodies as a buffer', async (t) => {
t.plan(4)
const fastify = Fastify()
fastify.post('/', (req, reply) => {
@@ -239,32 +249,28 @@ test('Should parse empty bodies as a buffer', t => {
})
fastify.addContentTypeParser('text/plain', { parseAs: 'buffer' }, function (req, body, done) {
t.ok(body instanceof Buffer)
t.equal(body.length, 0)
t.assert.ok(body instanceof Buffer)
t.assert.strictEqual(body.length, 0)
done(null, body)
})
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => fastify.close())
sget({
method: 'POST',
url: getServerUrl(fastify),
body: '',
headers: {
'Content-Type': 'text/plain'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.length, 0)
fastify.close()
})
const result = await fetch(fastifyServer, {
method: 'POST',
body: '',
headers: {
'Content-Type': 'text/plain'
}
})
t.assert.strictEqual(result.status, 200)
t.assert.strictEqual((await result.arrayBuffer()).byteLength, 0)
})
test('The charset should not interfere with the content type handling', t => {
t.plan(5)
test('The charset should not interfere with the content type handling', async (t) => {
t.plan(4)
const fastify = Fastify()
fastify.post('/', (req, reply) => {
@@ -272,27 +278,24 @@ test('The charset should not interfere with the content type handling', t => {
})
fastify.addContentTypeParser('application/json', function (req, payload, done) {
t.ok('called')
t.assert.ok('called')
jsonParser(payload, function (err, body) {
done(err, body)
})
})
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => fastify.close())
sget({
method: 'POST',
url: getServerUrl(fastify),
body: '{"hello":"world"}',
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), '{"hello":"world"}')
fastify.close()
})
const result = await fetch(fastifyServer, {
method: 'POST',
body: '{"hello":"world"}',
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
})
t.assert.ok(result.ok)
t.assert.strictEqual(result.status, 200)
t.assert.strictEqual(await result.text(), '{"hello":"world"}')
})