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,19 +1,15 @@
'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 prefer string content types over RegExp ones', t => {
t.plan(7)
test('should prefer string content types over RegExp ones', async (t) => {
t.plan(6)
const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
t.after(() => { fastify.close() })
fastify.post('/', (req, reply) => {
reply.send(req.body)
})
@@ -32,42 +28,38 @@ test('should prefer string content types over RegExp ones', t => {
})
})
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
sget({
method: 'POST',
url: getServerUrl(fastify),
body: '{"k1":"myValue", "k2": "myValue"}',
headers: {
'Content-Type': 'application/json'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), JSON.stringify({ k1: 'myValue', k2: 'myValue' }))
})
sget({
method: 'POST',
url: getServerUrl(fastify),
body: 'javascript',
headers: {
'Content-Type': 'application/javascript'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), 'javascript')
})
const result1 = await fetch(fastifyServer, {
method: 'POST',
body: '{"k1":"myValue", "k2": "myValue"}',
headers: {
'Content-Type': 'application/json'
}
})
t.assert.ok(result1.ok)
t.assert.strictEqual(result1.status, 200)
t.assert.equal(await result1.text(), JSON.stringify({ k1: 'myValue', k2: 'myValue' }))
const result2 = await fetch(fastifyServer, {
method: 'POST',
body: 'javascript',
headers: {
'Content-Type': 'application/javascript'
}
})
t.assert.ok(result2.ok)
t.assert.strictEqual(result2.status, 200)
t.assert.equal(await result2.text(), 'javascript')
})
test('removeContentTypeParser should support arrays of content types to remove', t => {
t.plan(8)
test('removeContentTypeParser should support arrays of content types to remove', async (t) => {
t.plan(7)
const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())
fastify.addContentTypeParser('application/xml', function (req, payload, done) {
payload.on('data', () => {})
@@ -89,52 +81,48 @@ test('removeContentTypeParser should support arrays of content types to remove',
reply.send(req.body)
})
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
sget({
method: 'POST',
url: getServerUrl(fastify),
body: '<?xml version="1.0">',
headers: {
'Content-Type': 'application/xml'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), 'xml')
})
sget({
method: 'POST',
url: getServerUrl(fastify),
body: '',
headers: {
'Content-Type': 'image/png'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 415)
})
sget({
method: 'POST',
url: getServerUrl(fastify),
body: '{test: "test"}',
headers: {
'Content-Type': 'application/json'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 415)
})
const result1 = await fetch(fastifyServer, {
method: 'POST',
body: '<?xml version="1.0">',
headers: {
'Content-Type': 'application/xml'
}
})
t.assert.ok(result1.ok)
t.assert.strictEqual(result1.status, 200)
t.assert.equal(await result1.text(), 'xml')
const result2 = await fetch(fastifyServer, {
method: 'POST',
body: '',
headers: {
'Content-Type': 'image/png'
}
})
t.assert.ok(!result2.ok)
t.assert.strictEqual(result2.status, 415)
const result3 = await fetch(fastifyServer, {
method: 'POST',
body: '{test: "test"}',
headers: {
'Content-Type': 'application/json'
}
})
t.assert.ok(!result3.ok)
t.assert.strictEqual(result3.status, 415)
})
test('removeContentTypeParser should support encapsulation', t => {
t.plan(6)
test('removeContentTypeParser should support encapsulation', async (t) => {
t.plan(5)
const fastify = Fastify()
t.after(() => fastify.close())
fastify.addContentTypeParser('application/xml', function (req, payload, done) {
payload.on('data', () => {})
@@ -157,41 +145,37 @@ test('removeContentTypeParser should support encapsulation', t => {
done()
})
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
sget({
method: 'POST',
url: getServerUrl(fastify) + '/encapsulated',
body: '<?xml version="1.0">',
headers: {
'Content-Type': 'application/xml'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 415)
})
sget({
method: 'POST',
url: getServerUrl(fastify),
body: '<?xml version="1.0">',
headers: {
'Content-Type': 'application/xml'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), 'xml')
fastify.close()
})
const result1 = await fetch(fastifyServer + '/encapsulated', {
method: 'POST',
body: '<?xml version="1.0">',
headers: {
'Content-Type': 'application/xml'
}
})
t.assert.ok(!result1.ok)
t.assert.strictEqual(result1.status, 415)
const result2 = await fetch(fastifyServer, {
method: 'POST',
body: '<?xml version="1.0">',
headers: {
'Content-Type': 'application/xml'
}
})
t.assert.ok(result2.ok)
t.assert.strictEqual(result2.status, 200)
t.assert.equal(await result2.text(), 'xml')
})
test('removeAllContentTypeParsers should support encapsulation', t => {
t.plan(6)
test('removeAllContentTypeParsers should support encapsulation', async (t) => {
t.plan(5)
const fastify = Fastify()
t.after(() => fastify.close())
fastify.post('/', (req, reply) => {
reply.send(req.body)
@@ -207,33 +191,28 @@ test('removeAllContentTypeParsers should support encapsulation', t => {
done()
})
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
sget({
method: 'POST',
url: getServerUrl(fastify) + '/encapsulated',
body: '{}',
headers: {
'Content-Type': 'application/json'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 415)
})
sget({
method: 'POST',
url: getServerUrl(fastify),
body: '{"test":1}',
headers: {
'Content-Type': 'application/json'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(JSON.parse(body.toString()).test, 1)
fastify.close()
})
const result1 = await fetch(fastifyServer + '/encapsulated', {
method: 'POST',
body: '{}',
headers: {
'Content-Type': 'application/json'
}
})
t.assert.ok(!result1.ok)
t.assert.strictEqual(result1.status, 415)
const result2 = await fetch(fastifyServer, {
method: 'POST',
body: '{"test":1}',
headers: {
'Content-Type': 'application/json'
}
})
t.assert.ok(result2.ok)
t.assert.strictEqual(result2.status, 200)
t.assert.equal(JSON.parse(await result2.text()).test, 1)
})