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,18 +1,15 @@
'use strict'
const t = require('tap')
const test = t.test
const sget = require('simple-get').concat
const Fastify = require('../fastify')
const { test } = require('node:test')
const Fastify = require('..')
const jsonParser = require('fast-json-body')
const { getServerUrl } = require('./helper')
process.removeAllListeners('warning')
test('should be able to use default parser for extra content type', t => {
t.plan(4)
test('should be able to use default parser for extra content type', async t => {
t.plan(3)
const fastify = Fastify()
t.teardown(() => fastify.close())
t.after(() => fastify.close())
fastify.post('/', (request, reply) => {
reply.send(request.body)
@@ -20,29 +17,23 @@ test('should be able to use default parser for extra content type', t => {
fastify.addContentTypeParser('text/json', { parseAs: 'string' }, fastify.getDefaultJsonParser('ignore', 'ignore'))
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
sget({
method: 'POST',
url: getServerUrl(fastify),
body: '{"hello":"world"}',
headers: {
'Content-Type': 'text/json'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.strictSame(JSON.parse(body.toString()), { hello: 'world' })
fastify.close()
})
const response = await fetch(fastifyServer, {
method: 'POST',
body: '{"hello":"world"}',
headers: {
'Content-Type': 'text/json'
}
})
t.assert.ok(response.ok)
t.assert.strictEqual(response.status, 200)
t.assert.deepStrictEqual(await response.json(), { hello: 'world' })
})
test('contentTypeParser should add a custom parser with RegExp value', t => {
t.plan(3)
test('contentTypeParser should add a custom parser with RegExp value', async (t) => {
const fastify = Fastify()
t.after(() => fastify.close())
fastify.post('/', (req, reply) => {
reply.send(req.body)
@@ -58,51 +49,45 @@ test('contentTypeParser should add a custom parser with RegExp value', t => {
})
})
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
t.teardown(() => fastify.close())
await t.test('in POST', async t => {
t.plan(3)
t.test('in POST', t => {
t.plan(3)
sget({
method: 'POST',
url: getServerUrl(fastify),
body: '{"hello":"world"}',
headers: {
'Content-Type': 'application/vnd.test+json'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), JSON.stringify({ hello: 'world' }))
})
const response = await fetch(fastifyServer, {
method: 'POST',
body: '{"hello":"world"}',
headers: {
'Content-Type': 'application/vnd.test+json'
}
})
t.assert.ok(response.ok)
t.assert.strictEqual(response.status, 200)
const body = await response.text()
t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
})
t.test('in OPTIONS', t => {
t.plan(3)
await t.test('in OPTIONS', async t => {
t.plan(3)
sget({
method: 'OPTIONS',
url: getServerUrl(fastify),
body: '{"hello":"world"}',
headers: {
'Content-Type': 'weird/content-type+json'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), JSON.stringify({ hello: 'world' }))
})
const response = await fetch(fastifyServer, {
method: 'OPTIONS',
body: '{"hello":"world"}',
headers: {
'Content-Type': 'weird/content-type+json'
}
})
t.assert.ok(response.ok)
t.assert.strictEqual(response.status, 200)
const body = await response.text()
t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
})
})
test('contentTypeParser should add multiple custom parsers with RegExp values', 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)
@@ -137,8 +122,8 @@ test('contentTypeParser should add multiple custom parsers with RegExp values',
'Content-Type': 'application/vnd.hello+json'
}
})
t.equal(response.statusCode, 200)
t.same(response.payload.toString(), '{"hello":"world"}')
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(response.payload.toString(), '{"hello":"world"}')
}
{
@@ -150,8 +135,8 @@ test('contentTypeParser should add multiple custom parsers with RegExp values',
'Content-Type': 'application/test+xml'
}
})
t.equal(response.statusCode, 200)
t.same(response.payload.toString(), 'xml')
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(response.payload.toString(), 'xml')
}
await fastify.inject({
@@ -162,17 +147,17 @@ test('contentTypeParser should add multiple custom parsers with RegExp values',
'Content-Type': 'application/+myExtension'
}
}).then((response) => {
t.equal(response.statusCode, 200)
t.same(response.payload.toString(), 'abcdefgmyExtension')
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(response.payload.toString(), 'abcdefgmyExtension')
}).catch((err) => {
t.error(err)
t.assert.ifError(err)
})
})
test('catch all content type parser should not interfere with content type parser', t => {
t.plan(10)
test('catch all content type parser should not interfere with content type parser', async t => {
t.plan(9)
const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())
fastify.post('/', (req, reply) => {
reply.send(req.body)
@@ -200,46 +185,24 @@ test('catch all content type parser should not interfere with content type parse
})
})
fastify.listen({ port: 0 }, err => {
t.error(err)
const fastifyServer = await fastify.listen({ port: 0 })
sget({
method: 'POST',
url: getServerUrl(fastify),
body: '{"myKey":"myValue"}',
headers: {
'Content-Type': 'application/json'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), JSON.stringify({ myKey: 'myValue' }))
})
const assertions = [
{ body: '{"myKey":"myValue"}', contentType: 'application/json', expected: JSON.stringify({ myKey: 'myValue' }) },
{ body: 'body', contentType: 'very-weird-content-type', expected: 'body' },
{ body: 'my text', contentType: 'text/html', expected: 'my texthtml' }
]
sget({
for (const { body, contentType, expected } of assertions) {
const response = await fetch(fastifyServer, {
method: 'POST',
url: getServerUrl(fastify),
body: 'body',
body,
headers: {
'Content-Type': 'very-weird-content-type'
'Content-Type': contentType
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), 'body')
})
sget({
method: 'POST',
url: getServerUrl(fastify),
body: 'my text',
headers: {
'Content-Type': 'text/html'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), 'my texthtml')
})
})
t.assert.ok(response.ok)
t.assert.strictEqual(response.status, 200)
t.assert.deepStrictEqual(await response.text(), expected)
}
})