Skip to content
Myr

basic-auth.test.ts

This file

Path

src/middleware/basic-auth/basic-auth.test.ts

107 lines · 2.8 KiB · TypeScript

Last changed by

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

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): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6

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

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

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

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

line 18why, one step back
18

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

lines 19–24why, one step back
19 const usernameB = 'hono-user-b'
20 const passwordB = 'hono-password-b'2122 const usernameC = 'hono-user-c'23 const passwordC = 'hono-password-c'24

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

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

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

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

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

line 27why, one step back
27 basicAuth({

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

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

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

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

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

lines 41–54why, one step back
41 app.use(
42 '/auth-multi/*',43 basicAuth(44 {45 username: usernameB,46 password: passwordB,47 },48 {49 username: usernameC,50 password: passwordC,51 }52 )53 )54

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

lines 55–56why, one step back
55 app.get('/auth/*', () => new Response('auth'))
56 app.get('/auth-unicode/*', () => new Response('auth'))

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

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

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

line 58why, one step back
58

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

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

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

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

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

lines 61–69why, one step back
61 const res = await app.dispatch(req)
62 expect(res).not.toBeNull()63 expect(res.status).toBe(401)64 expect(await res.text()).toBe('Unauthorized')65 })6667 it('Authorizated', async () => {68 const credential = Buffer.from(username + ':' + password).toString('base64')69

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

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

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

lines 71–74why, one step back
71 req.headers.set('Authorization', `Basic ${credential}`)
72 const res = await app.dispatch(req)73 expect(res).not.toBeNull()74 expect(res.status).toBe(200)

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

lines 75–83why, one step back
75 expect(await res.text()).toBe('auth')
76 })7778 it('Authorizated Unicode', async () => {79 const credential = Buffer.from(username + ':' + unicodePassword).toString('base64')8081 const req = new Request('http://localhost/auth-unicode/a')82 req.headers.set('Authorization', `Basic ${credential}`)83 const res = await app.dispatch(req)

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

lines 84–102why, one step back
84 expect(res).not.toBeNull()
85 expect(res.status).toBe(200)86 expect(await res.text()).toBe('auth')87 })8889 it('Authorizated multiple users', async () => {90 let credential = Buffer.from(usernameB + ':' + passwordB).toString('base64')9192 let req = new Request('http://localhost/auth-multi/b')93 req.headers.set('Authorization', `Basic ${credential}`)94 let res = await app.dispatch(req)95 expect(res).not.toBeNull()96 expect(res.status).toBe(200)97 expect(await res.text()).toBe('auth')9899 credential = Buffer.from(usernameC + ':' + passwordC).toString('base64')100 req = new Request('http://localhost/auth-multi/c')101 req.headers.set('Authorization', `Basic ${credential}`)102 res = await app.dispatch(req)

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

lines 103–105why, one step back
103 expect(res).not.toBeNull()
104 expect(res.status).toBe(200)105 expect(await res.text()).toBe('auth')

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

lines 106–107why, one step back
106 })
107})