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/middie/test/ drwxr-xr-x | |
| Viewing file: Select action/file-type: 'use strict'
// Original Fastify test/middlewares.test.js file
const t = require('tap')
const test = t.test
const sget = require('simple-get').concat
const fastify = require('fastify')
const fp = require('fastify-plugin')
const cors = require('cors')
const helmet = require('helmet')
const fs = require('fs')
const middiePlugin = require('../index')
test('use a middleware', t => {
t.plan(7)
const instance = fastify()
instance.register(middiePlugin)
.after(() => {
const useRes = instance.use(function (req, res, next) {
t.pass('middleware called')
next()
})
t.equal(useRes, instance)
})
instance.get('/', function (request, reply) {
reply.send({ hello: 'world' })
})
instance.listen(0, err => {
t.error(err)
t.teardown(instance.server.close.bind(instance.server))
sget({
method: 'GET',
url: 'http://localhost:' + instance.server.address().port
}, (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('use cors', t => {
t.plan(3)
const instance = fastify()
instance.register(middiePlugin)
.after(() => {
instance.use(cors())
})
instance.get('/', function (request, reply) {
reply.send({ hello: 'world' })
})
instance.listen(0, err => {
t.error(err)
t.teardown(instance.server.close.bind(instance.server))
sget({
method: 'GET',
url: 'http://localhost:' + instance.server.address().port
}, (err, response, body) => {
t.error(err)
t.equal(response.headers['access-control-allow-origin'], '*')
})
})
})
test('use helmet', t => {
t.plan(3)
const instance = fastify()
instance.register(middiePlugin)
.after(() => {
instance.use(helmet())
})
instance.get('/', function (request, reply) {
reply.send({ hello: 'world' })
})
instance.listen(0, err => {
t.error(err)
t.teardown(instance.server.close.bind(instance.server))
sget({
method: 'GET',
url: 'http://localhost:' + instance.server.address().port
}, (err, response, body) => {
t.error(err)
t.ok(response.headers['x-xss-protection'])
})
})
})
test('use helmet and cors', t => {
t.plan(4)
const instance = fastify()
instance.register(middiePlugin)
.after(() => {
instance.use(cors())
instance.use(helmet())
})
instance.get('/', function (request, reply) {
reply.send({ hello: 'world' })
})
instance.listen(0, err => {
t.error(err)
t.teardown(instance.server.close.bind(instance.server))
sget({
method: 'GET',
url: 'http://localhost:' + instance.server.address().port
}, (err, response, body) => {
t.error(err)
t.ok(response.headers['x-xss-protection'])
t.equal(response.headers['access-control-allow-origin'], '*')
})
})
})
test('middlewares with prefix', t => {
t.plan(5)
const instance = fastify()
instance.register(middiePlugin)
.after(() => {
instance.use(function (req, res, next) {
req.global = true
next()
})
instance.use('', function (req, res, next) {
req.global2 = true
next()
})
instance.use('/', function (req, res, next) {
req.root = true
next()
})
instance.use('/prefix', function (req, res, next) {
req.prefixed = true
next()
})
instance.use('/prefix/', function (req, res, next) {
req.slashed = true
next()
})
})
function handler (request, reply) {
reply.send({
prefixed: request.raw.prefixed,
slashed: request.raw.slashed,
global: request.raw.global,
global2: request.raw.global2,
root: request.raw.root
})
}
instance.get('/', handler)
instance.get('/prefix', handler)
instance.get('/prefix/', handler)
instance.get('/prefix/inner', handler)
instance.listen(0, err => {
t.error(err)
t.teardown(instance.server.close.bind(instance.server))
t.test('/', t => {
t.plan(2)
sget({
method: 'GET',
url: 'http://localhost:' + instance.server.address().port + '/',
json: true
}, (err, response, body) => {
t.error(err)
t.same(body, {
global: true,
global2: true,
root: true
})
})
})
t.test('/prefix', t => {
t.plan(2)
sget({
method: 'GET',
url: 'http://localhost:' + instance.server.address().port + '/prefix',
json: true
}, (err, response, body) => {
t.error(err)
t.same(body, {
prefixed: true,
global: true,
global2: true,
root: true,
slashed: true
})
})
})
t.test('/prefix/', t => {
t.plan(2)
sget({
method: 'GET',
url: 'http://localhost:' + instance.server.address().port + '/prefix/',
json: true
}, (err, response, body) => {
t.error(err)
t.same(body, {
prefixed: true,
slashed: true,
global: true,
global2: true,
root: true
})
})
})
t.test('/prefix/inner', t => {
t.plan(2)
sget({
method: 'GET',
url: 'http://localhost:' + instance.server.address().port + '/prefix/inner',
json: true
}, (err, response, body) => {
t.error(err)
t.same(body, {
prefixed: true,
slashed: true,
global: true,
global2: true,
root: true
})
})
})
})
})
test('res.end should block middleware execution', t => {
t.plan(4)
const instance = fastify()
instance.register(middiePlugin)
.after(() => {
instance.use(function (req, res, next) {
res.end('hello')
})
instance.use(function (req, res, next) {
t.fail('we should not be here')
})
})
instance.addHook('onRequest', (req, res, next) => {
t.ok('called')
next()
})
instance.addHook('preHandler', (req, reply, next) => {
t.fail('this should not be called')
})
instance.addHook('onSend', (req, reply, payload, next) => {
t.fail('this should not be called')
})
instance.addHook('onResponse', (request, reply, next) => {
t.ok('called')
next()
})
instance.get('/', function (request, reply) {
t.fail('we should no be here')
})
instance.inject({
url: '/',
method: 'GET'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.equal(res.payload, 'hello')
})
})
test('middlewares should be able to respond with a stream', t => {
t.plan(4)
const instance = fastify()
instance.addHook('onRequest', (req, res, next) => {
t.ok('called')
next()
})
instance.register(middiePlugin)
.after(() => {
instance.use(function (req, res, next) {
const stream = fs.createReadStream(process.cwd() + '/test/middleware.test.js', 'utf8')
stream.pipe(res)
res.once('finish', next)
})
instance.use(function (req, res, next) {
t.fail('we should not be here')
})
})
instance.addHook('preHandler', (req, reply, next) => {
t.fail('this should not be called')
})
instance.addHook('onSend', (req, reply, payload, next) => {
t.fail('this should not be called')
})
instance.addHook('onResponse', (request, reply, next) => {
t.ok('called')
next()
})
instance.get('/', function (request, reply) {
t.fail('we should no be here')
})
instance.inject({
url: '/',
method: 'GET'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
})
})
test('Use a middleware inside a plugin after an encapsulated plugin', t => {
t.plan(4)
const f = fastify()
f.register(middiePlugin)
f.register(function (instance, opts, next) {
instance.use(function (req, res, next) {
t.ok('first middleware called')
next()
})
instance.get('/', function (request, reply) {
reply.send({ hello: 'world' })
})
next()
})
f.register(fp(function (instance, opts, next) {
instance.use(function (req, res, next) {
t.ok('second middleware called')
next()
})
next()
}))
f.inject('/', (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { hello: 'world' })
})
})
test('middlewares should run in the order in which they are defined', t => {
t.plan(9)
const f = fastify()
f.register(middiePlugin)
f.register(fp(function (instance, opts, next) {
instance.use(function (req, res, next) {
t.equal(req.previous, undefined)
req.previous = 1
next()
})
instance.register(fp(function (i, opts, next) {
i.use(function (req, res, next) {
t.equal(req.previous, 2)
req.previous = 3
next()
})
next()
}))
instance.use(function (req, res, next) {
t.equal(req.previous, 1)
req.previous = 2
next()
})
next()
}))
f.register(function (instance, opts, next) {
instance.use(function (req, res, next) {
t.equal(req.previous, 3)
req.previous = 4
next()
})
instance.get('/', function (request, reply) {
t.equal(request.raw.previous, 5)
reply.send({ hello: 'world' })
})
instance.register(fp(function (i, opts, next) {
i.use(function (req, res, next) {
t.equal(req.previous, 4)
req.previous = 5
next()
})
next()
}))
next()
})
f.inject('/', (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.same(JSON.parse(res.payload), { hello: 'world' })
})
})
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0048 ]-- |