basic-auth.test.ts
This file
Path
src/middleware/basic-auth/basic-auth.test.ts
43 lines 路 1.1 KiB 路 TypeScript
Last changed by
feat(miniflare): repalce service-worker-mock with miniflare (#41)Minghebefore adoption52b56e6
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344import { Hono, Middleware } from '../../hono'
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( '*', Middleware.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') })})