mirror of
https://github.com/honojs/hono.git
synced 2024-11-25 13:19:30 +01:00
d77d890a73
* feat: check response type * refactor * refactor: add types
48 lines
1.1 KiB
TypeScript
48 lines
1.1 KiB
TypeScript
import { compose } from '../src/compose'
|
|
|
|
type C = {
|
|
req: { [key: string]: string }
|
|
res: { [key: string]: string }
|
|
}
|
|
|
|
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<C>(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')
|
|
})
|
|
})
|