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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768import { Hono } from '../../hono'import { basicAuth } from './basic-auth'
describe('Basic Auth by Middleware', () => { const crypto = global.crypto beforeAll(() => { global.crypto = require('crypto').webcrypto }) afterAll(() => { global.crypto = crypto })
const app = new Hono()
const username = 'hono-user-a' const password = 'hono-password-a' const unicodePassword = '炎'
app.use( '/auth/*', basicAuth({ username, password, }) )
app.use( '/auth-unicode/*', basicAuth({ username: username, password: unicodePassword, }) )
app.get('/auth/*', () => new Response('auth')) app.get('/auth-unicode/*', () => new Response('auth'))
it('Unauthorized', async () => { const req = new Request('http://localhost/auth/a') const res = await app.dispatch(req) expect(res).not.toBeNull() expect(res.status).toBe(401) expect(await res.text()).toBe('Unauthorized') })
it('Authorizated', async () => { const credential = Buffer.from(username + ':' + password).toString('base64')
const req = new Request('http://localhost/auth/a') req.headers.set('Authorization', `Basic ${credential}`) const res = await app.dispatch(req) expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe('auth') })
it('Authorizated Unicode', async () => { const credential = Buffer.from(username + ':' + unicodePassword).toString('base64')
const req = new Request('http://localhost/auth-unicode/a') req.headers.set('Authorization', `Basic ${credential}`) const res = await app.dispatch(req) expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe('auth') })})