basic-auth.test.ts
This file
Path
src/middleware/basic-auth/basic-auth.test.ts
44 lines 路 1.1 KiB 路 TypeScript
Last changed by
feat: builtin middleware as sub module (#81)Yusuke Wadabefore adoption7dd486a
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445import { 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'
app.use( '*', basicAuth({ username, password, }) ) app.get('/', () => new Response('root'))
it('Unauthorized', async () => { const req = new Request('http://localhost/') 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/') 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('root') })})