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

48 lines
1.1 KiB
TypeScript
Raw Normal View History

import { compose } from '../src/compose'
type C = {
req: { [key: string]: string }
res: { [key: string]: string }
}
2021-12-21 09:37:02 +01:00
describe('compose middleware', () => {
const middleware: Function[] = []
2021-12-21 09:37:02 +01:00
const a = async (c: C, next: Function) => {
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)
const b = async (c: C, next: Function) => {
2022-01-03 10:11:46 +01:00
await next()
c.res['headers'] = `${c.res.headers}-custom-header`
2021-12-21 09:37:02 +01:00
}
middleware.push(b)
const handler = async (c: C, next: Function) => {
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 () => {
const c: C = { req: request, res: response }
2021-12-21 09:37:02 +01:00
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 () => {
const c: C = { req: request, res: response }
2021-12-21 09:37:02 +01:00
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')
})
})