Software: Apache/2.4.41 (Ubuntu). PHP/8.0.30 uname -a: Linux apirnd 5.4.0-204-generic #224-Ubuntu SMP Thu Dec 5 13:38:28 UTC 2024 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) Safe-mode: OFF (not secure) /usr/local/lib/node_modules/homebridge-config-ui-x/node_modules/fastify/test/ drwxr-xr-x | |
| Viewing file: Select action/file-type: 'use strict'
const t = require('tap')
const test = t.test
const sget = require('simple-get').concat
const fastify = require('..')()
const opts = {
schema: {
response: {
200: {
type: 'object',
properties: {
hello: {
type: 'string'
}
}
},
'2xx': {
type: 'object',
properties: {
hello: {
type: 'number'
}
}
}
}
}
}
test('shorthand - output string', t => {
t.plan(1)
try {
fastify.get('/string', opts, function (req, reply) {
reply.code(200).send({ hello: 'world' })
})
t.pass()
} catch (e) {
t.fail()
}
})
test('shorthand - output number', t => {
t.plan(1)
try {
fastify.get('/number', opts, function (req, reply) {
reply.code(201).send({ hello: 55 })
})
t.pass()
} catch (e) {
t.fail()
}
})
test('wrong object for schema - output', t => {
t.plan(1)
try {
fastify.get('/wrong-object-for-schema', opts, function (req, reply) {
// will send { }
reply.code(201).send({ hello: 'world' })
})
t.pass()
} catch (e) {
t.fail()
}
})
test('empty response', t => {
t.plan(1)
try {
// no checks
fastify.get('/empty', opts, function (req, reply) {
reply.code(204).send()
})
t.pass()
} catch (e) {
t.fail()
}
})
test('unlisted response code', t => {
t.plan(1)
try {
fastify.get('/400', opts, function (req, reply) {
reply.code(400).send({ hello: 'DOOM' })
})
t.pass()
} catch (e) {
t.fail()
}
})
fastify.listen(0, err => {
t.error(err)
fastify.server.unref()
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' })
})
})
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 })
})
})
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, 201)
t.equal(response.headers['content-length'], '' + body.length)
t.same(JSON.parse(body), {})
})
})
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' })
})
})
})
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0058 ]-- |