basic-auth.test.ts
This file
Path
src/middleware/basic-auth/basic-auth.test.ts
129 lines · 3.5 KiB · TypeScript
Last changed by
feat: basic auth middleware supports overriding `hashFunction` (#128)Yusuke Wadabefore adoption7a31de5
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 middleware supports overriding `hashFunction` (#128)Yusuke Wadabefore adoption7a31de5
line 3why, one step back3import { SHA256 } from 'crypto-js'feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 4–14why, one step back45describe('Basic Auth by Middleware', () => {6 const crypto = global.crypto7 beforeAll(() => {8 global.crypto = require('crypto').webcrypto9 })10 afterAll(() => {11 global.crypto = crypto12 })1314 const app = new Hono()fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
line 15why, one step back15feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 16–17why, one step back16 const username = 'hono-user-a'17 const password = 'hono-password-a'fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
line 18why, one step back18 const unicodePassword = '炎'feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
line 19why, one step back19feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987
lines 20–25why, one step back20 const usernameB = 'hono-user-b'21 const passwordB = 'hono-password-b'2223 const usernameC = 'hono-user-c'24 const passwordC = 'hono-password-c'25feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
line 26why, one step back26 app.use(fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
line 27why, one step back27 '/auth/*',feat: builtin middleware as sub module (#81)Yusuke Wadabefore adoption7dd486a
line 28why, one step back28 basicAuth({feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 29–33why, one step back29 username,30 password,31 })32 )33fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
lines 34–41why, one step back34 app.use(35 '/auth-unicode/*',36 basicAuth({37 username: username,38 password: unicodePassword,39 })40 )41feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987
lines 42–55why, one step back42 app.use(43 '/auth-multi/*',44 basicAuth(45 {46 username: usernameB,47 password: passwordB,48 },49 {50 username: usernameC,51 password: passwordC,52 }53 )54 )55feat: basic auth middleware supports overriding `hashFunction` (#128)Yusuke Wadabefore adoption7a31de5
lines 56–64why, one step back56 app.use(57 '/auth-override-func/*',58 basicAuth({59 username: username,60 password: password,61 hashFunction: (data: string) => SHA256(data).toString(),62 })63 )64fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
lines 65–66why, one step back65 app.get('/auth/*', () => new Response('auth'))66 app.get('/auth-unicode/*', () => new Response('auth'))feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987
line 67why, one step back67 app.get('/auth-multi/*', () => new Response('auth'))feat: basic auth middleware supports overriding `hashFunction` (#128)Yusuke Wadabefore adoption7a31de5
line 68why, one step back68 app.get('/auth-override-func/*', () => new Response('auth'))fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
line 69why, one step back69feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
line 70why, one step back70 it('Unauthorized', async () => {fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
line 71why, one step back71 const req = new Request('http://localhost/auth/a')feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 72–80why, one step back72 const res = await app.dispatch(req)73 expect(res).not.toBeNull()74 expect(res.status).toBe(401)75 expect(await res.text()).toBe('Unauthorized')76 })7778 it('Authorizated', async () => {79 const credential = Buffer.from(username + ':' + password).toString('base64')80fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
line 81why, one step back81 const req = new Request('http://localhost/auth/a')feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 82–85why, one step back82 req.headers.set('Authorization', `Basic ${credential}`)83 const res = await app.dispatch(req)84 expect(res).not.toBeNull()85 expect(res.status).toBe(200)fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
lines 86–94why, one step back86 expect(await res.text()).toBe('auth')87 })8889 it('Authorizated Unicode', async () => {90 const credential = Buffer.from(username + ':' + unicodePassword).toString('base64')9192 const req = new Request('http://localhost/auth-unicode/a')93 req.headers.set('Authorization', `Basic ${credential}`)94 const res = await app.dispatch(req)feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987
lines 95–113why, one step back95 expect(res).not.toBeNull()96 expect(res.status).toBe(200)97 expect(await res.text()).toBe('auth')98 })99100 it('Authorizated multiple users', async () => {101 let credential = Buffer.from(usernameB + ':' + passwordB).toString('base64')102103 let req = new Request('http://localhost/auth-multi/b')104 req.headers.set('Authorization', `Basic ${credential}`)105 let res = await app.dispatch(req)106 expect(res).not.toBeNull()107 expect(res.status).toBe(200)108 expect(await res.text()).toBe('auth')109110 credential = Buffer.from(usernameC + ':' + passwordC).toString('base64')111 req = new Request('http://localhost/auth-multi/c')112 req.headers.set('Authorization', `Basic ${credential}`)113 res = await app.dispatch(req)feat: basic auth middleware supports overriding `hashFunction` (#128)Yusuke Wadabefore adoption7a31de5
lines 114–124why, one step back114 expect(res).not.toBeNull()115 expect(res.status).toBe(200)116 expect(await res.text()).toBe('auth')117 })118119 it('should authorize with sha256 function override', async () => {120 const credential = Buffer.from(username + ':' + password).toString('base64')121122 const req = new Request('http://localhost/auth-override-func/a')123 req.headers.set('Authorization', `Basic ${credential}`)124 const res = await app.dispatch(req)fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb
lines 125–127why, one step back125 expect(res).not.toBeNull()126 expect(res.status).toBe(200)127 expect(await res.text()).toBe('auth')feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 128–129why, one step back128 })129})