index.test.ts
This file
Path
src/middleware/combine/index.test.ts
355 lines 路 10.2 KiB 路 TypeScript
Last changed by
fix(combine): return a Response from a short-circuiting middleware in some() (#5391)Harshit Prakashbefore adoptionafb2068
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356import { every, except, some } from '.'import { Hono } from '../../hono'import type { MiddlewareHandler } from '../../types'
const nextMiddleware: MiddlewareHandler = async (_, next) => await next()
describe('some', () => { let app: Hono
beforeEach(() => { app = new Hono() })
it('Should call only the first middleware', async () => { const middleware1 = vi.fn(nextMiddleware) const middleware2 = vi.fn(nextMiddleware)
app.use('/', some(middleware1, middleware2)) app.get('/', (c) => { return c.text('Hello World') }) const res = await app.request('http://localhost/')
expect(middleware1).toBeCalled() expect(middleware2).not.toBeCalled() expect(await res.text()).toBe('Hello World') })
it('Should try to call the second middleware if the first one throws an error', async () => { const middleware1 = () => { throw new Error('Error') } const middleware2 = vi.fn(nextMiddleware)
app.use('/', some(middleware1, middleware2)) app.get('/', (c) => { return c.text('Hello World') }) const res = await app.request('http://localhost/')
expect(middleware2).toBeCalled() expect(await res.text()).toBe('Hello World') })
it('Should try to call the second middleware if the first one returns false', async () => { const middleware1 = () => false const middleware2 = vi.fn(nextMiddleware)
app.use('/', some(middleware1, middleware2)) app.get('/', (c) => { return c.text('Hello World') }) const res = await app.request('http://localhost/')
expect(middleware2).toBeCalled() expect(await res.text()).toBe('Hello World') })
it('Should throw last error if all middleware throw an error', async () => { const middleware1 = () => { throw new Error('Error1') } const middleware2 = () => { throw new Error('Error2') }
app.use('/', some(middleware1, middleware2)) app.get('/', (c) => { return c.text('Hello World') }) app.onError((error, c) => { return c.text(error.message) }) const res = await app.request('http://localhost/')
expect(await res.text()).toBe('Error2') })
it('Should throw error if all middleware return false', async () => { const middleware1 = () => false const middleware2 = () => false
app.use('/', some(middleware1, middleware2)) app.get('/', (c) => { return c.text('Hello World') }) app.onError((_, c) => { return c.text('oops') }) const res = await app.request('http://localhost/')
expect(await res.text()).toBe('oops') })
it('Should not call skipped middleware even if an error is thrown', async () => { const middleware1: MiddlewareHandler = async (_c, next) => { await next() } const middleware2 = vi.fn(() => true)
app.use( '/', every(some(middleware1, middleware2), () => { throw new Error('Error') }) ) app.get('/', (c) => c.text('OK')) app.onError((_, c) => { return c.text('oops') }) const res = await app.request('http://localhost/')
expect(middleware2).not.toBeCalled() expect(await res.text()).toBe('oops') })
it('Should not call skipped middleware even if an error is thrown with returning truthy value middleware', async () => { const middleware1 = () => true const middleware2 = vi.fn(() => true)
app.use( '/', every(some(middleware1, middleware2), () => { throw new Error('Error') }) ) app.get('/', (c) => c.text('OK')) app.onError((_, c) => { return c.text('oops') }) const res = await app.request('http://localhost/')
expect(middleware2).not.toBeCalled() expect(await res.text()).toBe('oops') })
it('Should return a Response from a middleware that short-circuits', async () => { const shortCircuit: MiddlewareHandler = async (c) => c.json({ error: 'bad request' }, 400) const next = vi.fn(async (_c, n) => { await n() })
const app = new Hono() app.use('/', some(shortCircuit, next)) app.get('/', (c) => c.text('handler'))
const res = await app.request('http://localhost/')
expect(res.status).toBe(400) expect(await res.json()).toEqual({ error: 'bad request' }) expect(next).not.toBeCalled() })})
describe('every', () => { let app: Hono
beforeEach(() => { app = new Hono() })
it('Should call all middleware', async () => { const middleware1 = vi.fn(nextMiddleware) 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(middleware1).toBeCalled() expect(middleware2).toBeCalled() expect(await res.text()).toBe('Hello World') })
it('Should throw error if any middleware throws an error', async () => { const middleware1 = () => { throw new Error('Error1') } const middleware2 = vi.fn(nextMiddleware)
app.use('/', every(middleware1, middleware2)) app.get('/', (c) => { return c.text('Hello World') }) app.onError((error, c) => { return c.text(error.message) }) const res = await app.request('http://localhost/')
expect(await res.text()).toBe('Error1') expect(middleware2).not.toBeCalled() })
it('Should throw error if any middleware returns false', async () => { const middleware1 = () => false const middleware2 = vi.fn(nextMiddleware)
app.use('/', every(middleware1, middleware2)) app.get('/', (c) => { return c.text('Hello World') }) app.onError((_, c) => { return c.text('oops') }) const res = await app.request('http://localhost/')
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() })
it('Should pass the path params to middlewares', async () => { const app = new Hono() app.use('*', nextMiddleware) const paramMiddleware: MiddlewareHandler = async (c) => { return c.json(c.req.param(), 200) }
app.use('/:id', every(paramMiddleware)) app.get('/:id', (c) => { return c.text('Hello World') })
const res = await app.request('http://localhost/123') expect(await res.json()).toEqual({ id: '123' }) })})
describe('except', () => { let app: Hono
beforeEach(() => { app = new Hono() })
it('Should call all middleware, except the one that matches the condition', async () => { const middleware1 = vi.fn(nextMiddleware) const middleware2 = vi.fn(nextMiddleware)
app.use('*', except('/maintenance', middleware1, middleware2)) app.get('/maintenance', (c) => { return c.text('Hello Maintenance') }) app.get('*', (c) => { return c.redirect('/maintenance') }) let res = await app.request('http://localhost/')
expect(middleware1).toBeCalled() expect(middleware2).toBeCalled() expect(res.headers.get('location')).toBe('/maintenance')
middleware1.mockClear() middleware2.mockClear() res = await app.request('http://localhost/maintenance')
expect(middleware1).not.toBeCalled() expect(middleware2).not.toBeCalled() expect(await res.text()).toBe('Hello Maintenance') })
it('Should call all middleware, except the one that matches some of the conditions', async () => { const middleware1 = vi.fn(nextMiddleware) const middleware2 = vi.fn(nextMiddleware)
app.use('*', except(['/maintenance', '/public/users/:id'], middleware1, middleware2)) app.get('/maintenance', (c) => { return c.text('Hello Maintenance') }) app.get('/public/users/:id', (c) => { return c.text(`Hello Public User ${c.req.param('id')}`) }) app.get('/secret', (c) => { return c.text('Hello Secret') }) let res = await app.request('http://localhost/secret')
expect(middleware1).toBeCalled() expect(middleware2).toBeCalled() expect(await res.text()).toBe('Hello Secret')
middleware1.mockClear() middleware2.mockClear() res = await app.request('http://localhost/maintenance')
expect(middleware1).not.toBeCalled() expect(middleware2).not.toBeCalled() expect(await res.text()).toBe('Hello Maintenance')
middleware1.mockClear() middleware2.mockClear() res = await app.request('http://localhost/public/users/123')
expect(middleware1).not.toBeCalled() expect(middleware2).not.toBeCalled() expect(await res.text()).toBe('Hello Public User 123') })
it('Should call all middleware, except the one that matches some of the condition function', async () => { const middleware1 = vi.fn(nextMiddleware) const middleware2 = vi.fn(nextMiddleware)
app.use( '*', except(['/maintenance', (c) => !!c.req.path.match(/public/)], middleware1, middleware2) ) app.get('/maintenance', (c) => { return c.text('Hello Maintenance') }) app.get('/public/users/:id', (c) => { return c.text(`Hello Public User ${c.req.param('id')}`) }) app.get('/secret', (c) => { return c.text('Hello Secret') }) let res = await app.request('http://localhost/secret')
expect(middleware1).toBeCalled() expect(middleware2).toBeCalled() expect(await res.text()).toBe('Hello Secret')
middleware1.mockClear() middleware2.mockClear() res = await app.request('http://localhost/maintenance')
expect(middleware1).not.toBeCalled() expect(middleware2).not.toBeCalled() expect(await res.text()).toBe('Hello Maintenance')
middleware1.mockClear() middleware2.mockClear() res = await app.request('http://localhost/public/users/123')
expect(middleware1).not.toBeCalled() expect(middleware2).not.toBeCalled() expect(await res.text()).toBe('Hello Public User 123') })})