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,8 +1,6 @@
'use strict'
const t = require('tap')
const test = t.test
const sget = require('simple-get').concat
const { test } = require('node:test')
const fastify = require('..')()
const opts = {
@@ -34,9 +32,9 @@ test('shorthand - output string', t => {
fastify.get('/string', opts, function (req, reply) {
reply.code(200).send({ hello: 'world' })
})
t.pass()
t.assert.ok(true)
} catch (e) {
t.fail()
t.assert.fail()
}
})
@@ -46,9 +44,9 @@ test('shorthand - output number', t => {
fastify.get('/number', opts, function (req, reply) {
reply.code(201).send({ hello: 55 })
})
t.pass()
t.assert.ok(true)
} catch (e) {
t.fail()
t.assert.fail()
}
})
@@ -59,9 +57,9 @@ test('wrong object for schema - output', t => {
// will send { }
reply.code(201).send({ hello: 'world' })
})
t.pass()
t.assert.ok(true)
} catch (e) {
t.fail()
t.assert.fail()
}
})
@@ -72,9 +70,9 @@ test('empty response', t => {
fastify.get('/empty', opts, function (req, reply) {
reply.code(204).send()
})
t.pass()
t.assert.ok(true)
} catch (e) {
t.fail()
t.assert.fail()
}
})
@@ -84,80 +82,59 @@ test('unlisted response code', t => {
fastify.get('/400', opts, function (req, reply) {
reply.code(400).send({ hello: 'DOOM' })
})
t.pass()
t.assert.ok(true)
} catch (e) {
t.fail()
t.assert.fail()
}
})
fastify.listen({ port: 0 }, err => {
t.error(err)
t.teardown(() => { fastify.close() })
test('start server and run tests', async (t) => {
const fastifyServer = await fastify.listen({ port: 0 })
t.after(() => fastify.close())
test('shorthand - string get ok', t => {
t.plan(4)
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/string'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(response.headers['content-length'], '' + body.length)
t.same(JSON.parse(body), { hello: 'world' })
await test('shorthand - string get ok', async (t) => {
const result = await fetch(fastifyServer + '/string')
t.assert.ok(result.ok)
t.assert.strictEqual(result.status, 200)
const body = await result.text()
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
})
await test('shorthand - number get ok', async (t) => {
const result = await fetch(fastifyServer + '/number')
t.assert.ok(result.ok)
t.assert.strictEqual(result.status, 201)
const body = await result.text()
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), { hello: 55 })
})
await test('shorthand - wrong-object-for-schema', async (t) => {
const result = await fetch(fastifyServer + '/wrong-object-for-schema')
t.assert.ok(!result.ok)
t.assert.strictEqual(result.status, 500)
const body = await result.text()
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), {
statusCode: 500,
error: 'Internal Server Error',
message: 'The value "world" cannot be converted to a number.'
})
})
test('shorthand - number get ok', t => {
t.plan(4)
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/number'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 201)
t.equal(response.headers['content-length'], '' + body.length)
t.same(JSON.parse(body), { hello: 55 })
})
await test('shorthand - empty', async (t) => {
const result = await fetch(fastifyServer + '/empty')
t.assert.ok(result.ok)
t.assert.strictEqual(result.status, 204)
})
test('shorthand - wrong-object-for-schema', t => {
t.plan(4)
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/wrong-object-for-schema'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 500)
t.equal(response.headers['content-length'], '' + body.length)
t.same(JSON.parse(body), {
statusCode: 500,
error: 'Internal Server Error',
message: 'The value "world" cannot be converted to a number.'
})
})
})
test('shorthand - empty', t => {
t.plan(2)
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/empty'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 204)
})
})
test('shorthand - 400', t => {
t.plan(4)
sget({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/400'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 400)
t.equal(response.headers['content-length'], '' + body.length)
t.same(JSON.parse(body), { hello: 'DOOM' })
})
await test('shorthand - 400', async (t) => {
const result = await fetch(fastifyServer + '/400')
t.assert.ok(!result.ok)
t.assert.strictEqual(result.status, 400)
const body = await result.text()
t.assert.strictEqual(result.headers.get('content-length'), '' + body.length)
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'DOOM' })
})
})