Skip to content
Myr

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 file

Take it

Support module workers syntax for Cloudflare Workers (#43)Yusuke Wadabefore adoptionfceed74

line 1why, one step back
1import { Hono } from '../../hono'

feat: builtin middleware as sub module (#81)Yusuke Wadabefore adoption7dd486a

line 2why, one step back
2import { basicAuth } from './basic-auth'

feat: basic auth middleware supports overriding `hashFunction` (#128)Yusuke Wadabefore adoption7a31de5

line 3why, one step back
3import { SHA256 } from 'crypto-js'

feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6

lines 4–14why, one step back
4
5describe('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 back
15

feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6

lines 16–17why, one step back
16 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 back
18 const unicodePassword = '炎'

feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6

line 19why, one step back
19

feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987

lines 20–25why, one step back
20 const usernameB = 'hono-user-b'
21 const passwordB = 'hono-password-b'2223 const usernameC = 'hono-user-c'24 const passwordC = 'hono-password-c'25

feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6

line 26why, one step back
26 app.use(

fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb

line 27why, one step back
27 '/auth/*',

feat: builtin middleware as sub module (#81)Yusuke Wadabefore adoption7dd486a

line 28why, one step back
28 basicAuth({

feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6

lines 29–33why, one step back
29 username,
30 password,31 })32 )33

fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb

lines 34–41why, one step back
34 app.use(
35 '/auth-unicode/*',36 basicAuth({37 username: username,38 password: unicodePassword,39 })40 )41

feat: basic auth middleware supports multiple users (#112)Yusuke Wadabefore adoption2b76987

lines 42–55why, one step back
42 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 )55

feat: basic auth middleware supports overriding `hashFunction` (#128)Yusuke Wadabefore adoption7a31de5

lines 56–64why, one step back
56 app.use(
57 '/auth-override-func/*',58 basicAuth({59 username: username,60 password: password,61 hashFunction: (data: string) => SHA256(data).toString(),62 })63 )64

fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb

lines 65–66why, one step back
65 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 back
67 app.get('/auth-multi/*', () => new Response('auth'))

feat: basic auth middleware supports overriding `hashFunction` (#128)Yusuke Wadabefore adoption7a31de5

line 68why, one step back
68 app.get('/auth-override-func/*', () => new Response('auth'))

fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb

line 69why, one step back
69

feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6

line 70why, one step back
70 it('Unauthorized', async () => {

fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb

line 71why, one step back
71 const req = new Request('http://localhost/auth/a')

feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6

lines 72–80why, one step back
72 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')80

fix: Unicode problem about atob/btoa (#85)Yusuke Wadabefore adoption04bb4bb

line 81why, one step back
81 const req = new Request('http://localhost/auth/a')

feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6

lines 82–85why, one step back
82 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 back
86 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 back
95 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 back
114 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 back
125 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 back
128 })
129})