basic-auth.test.ts
This file
Path
src/middleware/basic-auth/basic-auth.test.ts
44 lines · 1.1 KiB · TypeScript
Last changed by
feat: builtin middleware as sub module (#81)Yusuke Wadabefore adoption7dd486a
History of this fileTake it
Support module workers syntax for Cloudflare Workers (#43)Yusuke Wadabefore adoptionfceed74
line 1why, one step back1import { Hono } from '../../hono'feat: builtin middleware as sub module (#81)Yusuke Wadabefore adoption7dd486a
line 2why, one step back2import { basicAuth } from './basic-auth'feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 3–18why, one step back34describe('Basic Auth by Middleware', () => {5 const crypto = global.crypto6 beforeAll(() => {7 global.crypto = require('crypto').webcrypto8 })9 afterAll(() => {10 global.crypto = crypto11 })1213 const app = new Hono()14 const username = 'hono-user-a'15 const password = 'hono-password-a'1617 app.use(18 '*',feat: builtin middleware as sub module (#81)Yusuke Wadabefore adoption7dd486a
line 19why, one step back19 basicAuth({feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 20–26why, one step back20 username,21 password,22 })23 )24 app.get('/', () => new Response('root'))2526 it('Unauthorized', async () => {feat(miniflare): repalce service-worker-mock with miniflare (#41)Minghebefore adoption52b56e6
line 27why, one step back27 const req = new Request('http://localhost/')feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 28–36why, one step back28 const res = await app.dispatch(req)29 expect(res).not.toBeNull()30 expect(res.status).toBe(401)31 expect(await res.text()).toBe('Unauthorized')32 })3334 it('Authorizated', async () => {35 const credential = Buffer.from(username + ':' + password).toString('base64')36feat(miniflare): repalce service-worker-mock with miniflare (#41)Minghebefore adoption52b56e6
line 37why, one step back37 const req = new Request('http://localhost/')feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 38–44why, one step back38 req.headers.set('Authorization', `Basic ${credential}`)39 const res = await app.dispatch(req)40 expect(res).not.toBeNull()41 expect(res.status).toBe(200)42 expect(await res.text()).toBe('root')43 })44})