basic-auth.test.ts
This file
Path
src/middleware/basic-auth/basic-auth.test.ts
67 lines · 1.7 KiB · TypeScript
Last changed by
fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
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–13why, 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()fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
line 14why, one step back14feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 15–16why, one step back15 const username = 'hono-user-a'16 const password = 'hono-password-a'fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
line 17why, one step back17 const unicodePassword = '炎'feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 18–19why, one step back1819 app.use(fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
line 20why, one step back20 '/auth/*',feat: builtin middleware as sub module (#81)Yusuke Wadabefore adoption7dd486a
line 21why, one step back21 basicAuth({feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 22–26why, one step back22 username,23 password,24 })25 )26fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
lines 27–37why, one step back27 app.use(28 '/auth-unicode/*',29 basicAuth({30 username: username,31 password: unicodePassword,32 })33 )3435 app.get('/auth/*', () => new Response('auth'))36 app.get('/auth-unicode/*', () => new Response('auth'))37feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
line 38why, one step back38 it('Unauthorized', async () => {fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
line 39why, one step back39 const req = new Request('http://localhost/auth/a')feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 40–48why, one step back40 const res = await app.dispatch(req)41 expect(res).not.toBeNull()42 expect(res.status).toBe(401)43 expect(await res.text()).toBe('Unauthorized')44 })4546 it('Authorizated', async () => {47 const credential = Buffer.from(username + ':' + password).toString('base64')48fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
line 49why, one step back49 const req = new Request('http://localhost/auth/a')feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 50–53why, one step back50 req.headers.set('Authorization', `Basic ${credential}`)51 const res = await app.dispatch(req)52 expect(res).not.toBeNull()53 expect(res.status).toBe(200)fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
lines 54–65why, one step back54 expect(await res.text()).toBe('auth')55 })5657 it('Authorizated Unicode', async () => {58 const credential = Buffer.from(username + ':' + unicodePassword).toString('base64')5960 const req = new Request('http://localhost/auth-unicode/a')61 req.headers.set('Authorization', `Basic ${credential}`)62 const res = await app.dispatch(req)63 expect(res).not.toBeNull()64 expect(res.status).toBe(200)65 expect(await res.text()).toBe('auth')feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 66–67why, one step back66 })67})