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