0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 10:51:01 +00:00
hono/src/compose.test.js

43 lines
957 B
JavaScript
Raw Normal View History

2021-12-21 08:37:02 +00:00
const compose = require('./compose')
describe('compose middleware', () => {
const middleware = []
const a = (c, next) => {
c.req['log'] = 'log'
next()
}
middleware.push(a)
const b = (c, next) => {
next()
c.res['header'] = `${c.res.header}-custom-header`
}
middleware.push(b)
const handler = (c, next) => {
c.req['log'] = `${c.req.log} message`
next()
c.res = { message: 'new response' }
}
middleware.push(handler)
const request = {}
const response = {}
it('Request', () => {
const c = { req: request, res: response }
const composed = compose(middleware)
composed(c)
expect(c.req['log']).not.toBeNull()
expect(c.req['log']).toBe('log message')
})
it('Response', () => {
const c = { req: request, res: response }
const composed = compose(middleware)
composed(c)
expect(c.res['header']).not.toBeNull()
expect(c.res['message']).toBe('new response')
})
})