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
1017 B
JavaScript
Raw Normal View History

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