From 1749f6a962141656f13ca1371414a9972e2eee7d Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Sat, 29 Oct 2022 00:09:09 +0900 Subject: [PATCH] test(bun): JWT middleware works on Bun v0.2.2 (#633) --- bun_test/index.test.tsx | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/bun_test/index.test.tsx b/bun_test/index.test.tsx index 2d00d323..469cd673 100644 --- a/bun_test/index.test.tsx +++ b/bun_test/index.test.tsx @@ -80,18 +80,28 @@ describe('Serve Static Middleware', () => { }) }) -// JWT is not available for Bun -// It throw the Error -describe('JWT Middleware (Not supported yet)', () => { +// Bun support WebCrypto since v0.2.2 +// So, JWT middleware works well. +describe('JWT Auth Middleware', () => { const app = new Hono() - let t = false - try { - app.use('/jwt/*', jwt({ secret: 'a-secret' })) - } catch { - t = true - } - it('Throw the error', () => { - expect(t).toBe(true) + app.use('/jwt/*', jwt({ secret: 'a-secret' })) + app.get('/jwt/a', (c) => c.text('auth')) + + it('Should not authorize, return 401 Response', async () => { + const req = new Request('http://localhost/jwt/a') + const res = await app.request(req) + expect(res.status).toBe(401) + expect(await res.text()).toBe('Unauthorized') + }) + + it('Should authorize, return 200 Response', async () => { + const credential = + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE' + const req = new Request('http://localhost/jwt/a') + req.headers.set('Authorization', `Bearer ${credential}`) + const res = await app.request(req) + expect(res.status).toBe(200) + expect(await res.text()).toBe('auth') }) })