0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-22 02:27:49 +01:00

fix(combine/every): make every middleware work with short-circuiting middlewares (#3441)

This commit is contained in:
Paweł Dąbrowski 2024-09-24 04:58:39 +02:00 committed by GitHub
parent ad0bba0964
commit fe0a82a615
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 0 deletions

View File

@ -150,6 +150,22 @@ describe('every', () => {
expect(await res.text()).toBe('oops')
expect(middleware2).not.toBeCalled()
})
it('Should return the same response a middleware returns if it short-circuits the chain', async () => {
const middleware1: MiddlewareHandler = async (c) => {
return c.text('Hello Middleware 1')
}
const middleware2 = vi.fn(nextMiddleware)
app.use('/', every(middleware1, middleware2))
app.get('/', (c) => {
return c.text('Hello World')
})
const res = await app.request('http://localhost/')
expect(await res.text()).toBe('Hello Middleware 1')
expect(middleware2).not.toBeCalled()
})
})
describe('except', () => {

View File

@ -94,6 +94,7 @@ export const every = (...middleware: (MiddlewareHandler | Condition)[]): Middlew
if (res === false) {
throw new Error('Unmet condition')
}
return res
})
const handler = async (c: Context, next: Next) =>