0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 10:51:01 +00:00
hono/test/compose.test.ts
Yusuke Wada e6039f69f9
Migrate to TypeScript (#21)
* Migrate to TypeScript

* ready for publish
2022-01-05 18:41:29 +09:00

48 lines
1.1 KiB
TypeScript

import { compose } from '../src/compose'
class C {
req: { [key: string]: any }
res: { [key: string]: any }
}
describe('compose middleware', () => {
const middleware: Function[] = []
const a = async (c: C, next: Function) => {
c.req['log'] = 'log'
await next()
}
middleware.push(a)
const b = async (c: C, next: Function) => {
await next()
c.res['headers'] = `${c.res.headers}-custom-header`
}
middleware.push(b)
const handler = async (c: C, next: Function) => {
c.req['log'] = `${c.req.log} message`
await next()
c.res = { message: 'new response' }
}
middleware.push(handler)
const request = {}
const response = {}
it('Request', async () => {
const c: C = { req: request, res: response }
const composed = compose(middleware)
await composed(c)
expect(c.req['log']).not.toBeNull()
expect(c.req['log']).toBe('log message')
})
it('Response', async () => {
const c: C = { req: request, res: response }
const composed = compose(middleware)
await composed(c)
expect(c.res['header']).not.toBeNull()
expect(c.res['message']).toBe('new response')
})
})