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
feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 1–25why, one step back1import { Hono, Middleware } from '../../hono'23describe('Basic Auth by Middleware', () => {4 const crypto = global.crypto5 beforeAll(() => {6 global.crypto = require('crypto').webcrypto7 })8 afterAll(() => {9 global.crypto = crypto10 })1112 const app = new Hono()13 const username = 'hono-user-a'14 const password = 'hono-password-a'1516 app.use(17 '*',18 Middleware.basicAuth({19 username,20 password,21 })22 )23 app.get('/', () => new Response('root'))2425 it('Unauthorized', async () => {feat(miniflare): repalce service-worker-mock with miniflare (#41)Minghebefore adoption52b56e6
line 26why, one step back26 const req = new Request('http://localhost/')feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 27–35why, one step back27 const res = await app.dispatch(req)28 expect(res).not.toBeNull()29 expect(res.status).toBe(401)30 expect(await res.text()).toBe('Unauthorized')31 })3233 it('Authorizated', async () => {34 const credential = Buffer.from(username + ':' + password).toString('base64')35feat(miniflare): repalce service-worker-mock with miniflare (#41)Minghebefore adoption52b56e6
line 36why, one step back36 const req = new Request('http://localhost/')feat(basic-auth): basic-auth middleware (#28)Minghebefore adoptioncb4f3d6
lines 37–43why, one step back37 req.headers.set('Authorization', `Basic ${credential}`)38 const res = await app.dispatch(req)39 expect(res).not.toBeNull()40 expect(res.status).toBe(200)41 expect(await res.text()).toBe('root')42 })43})