Skip to content
Myr

fix(basic/bearer): support "nested" middleware (#610)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by GitHub · parent dc82384 · Browse files · 3 files+50 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+50 added−22 removed

  • tests:+49 added−21 removed
  • other:+1 added−1 removed

3 files+50 added−22 removed

modified:src/middleware/basic-auth/index.test.ts+19 added-21 removed
modified:src/middleware/bearer-auth/index.test.ts+30 added
8 unmodified lines
9 beforeEach(async () => {10 app = new Hono()11 handlerExecuted = false12 token = 'abcdefg12345-._~+/='1314 app.use('/auth/*', bearerAuth({ token }))15 app.use('/auth/*', async (c, next) => {16 c.header('x-custom', 'foo')17 await next()
7 unmodified lines
25 app.get('/authBot/*', (c) => {26 handlerExecuted = true27 return c.text('auth bot')28 })2930 app.use('/nested/*', async (c, next) => {31 const auth = bearerAuth({ token })32 return auth(c, next)33 })34 app.get('/nested/*', (c) => {35 handlerExecuted = true36 return c.text('auth nested')37 })38 })3940 it('Should authorize', async () => {41 const req = new Request('http://localhost/auth/a')
55 unmodified lines
97 expect(handlerExecuted).toBeFalsy()98 expect(res.status).toBe(400)99 expect(await res.text()).toBe('Bad Request')100 })101102 it('Should authorize - nested', async () => {103 const req = new Request('http://localhost/nested/a')104 req.headers.set('Authorization', 'Bearer abcdefg12345-._~+/=')105 const res = await app.request(req)106 expect(res).not.toBeNull()107 expect(res.status).toBe(200)108 expect(handlerExecuted).toBeTruthy()109 expect(await res.text()).toBe('auth nested')110 })111112 it('Should not authorize - nested', async () => {113 const req = new Request('http://localhost/nested/a')114 req.headers.set('Authorization', 'Bearer invalid-token')115 const res = await app.request(req)116 expect(res).not.toBeNull()117 expect(handlerExecuted).toBeFalsy()118 expect(res.status).toBe(401)119 expect(await res.text()).toBe('Unauthorized')120 })121})
modified:src/hono.ts+1 added-1 removed
27 unmodified lines
28 D extends ValidatedData = ValidatedData29> = (30 c: Context<RequestParamKeyType, E, D>,31 next: Next32) => Response | Promise<Response> | Promise<void> | Promise<Response | undefined>32) => Response | Promise<Response> | Promise<void> | Promise<Response | undefined | void>3334export type MiddlewareHandler = <E extends Partial<Environment> = Environment>(35 c: Context<string, E>,36 next: Next
250 unmodified lines