middleware.test.tsx
This file
Path
runtime-tests/deno/middleware.test.tsx
92 lines 路 2.8 KiB 路 TypeScript
Last changed by
test: move adapter runtime tests into `adapters/*` (#5466)Yusuke Wadabefore adoption645ddd6
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293import { assertEquals } from '@std/assert'import { Hono } from '../../src/hono.ts'import { basicAuth } from '../../src/middleware/basic-auth/index.ts'import { jwt } from '../../src/middleware/jwt/index.ts'
// Test just only minimal patterns.// Because others are already tested well in Cloudflare Workers environment.
Deno.test('Basic Auth Middleware', async () => { const app = new Hono()
const username = 'hono' const password = 'ahotproject'
app.use( '/auth/*', basicAuth({ username, password, }) )
app.get('/auth/*', () => new Response('auth'))
const res = await app.request('http://localhost/auth/a') assertEquals(res.status, 401) assertEquals(await res.text(), 'Unauthorized')
const credential = 'aG9ubzphaG90cHJvamVjdA=='
const req = new Request('http://localhost/auth/a') req.headers.set('Authorization', `Basic ${credential}`) const resOK = await app.request(req) assertEquals(resOK.status, 200) assertEquals(await resOK.text(), 'auth')
const invalidCredential = 'G9ubzphY29vbHByb2plY3Q='
const req2 = new Request('http://localhost/auth/a') req2.headers.set('Authorization', `Basic ${invalidCredential}`) const resNG = await app.request(req2) assertEquals(resNG.status, 401) assertEquals(await resNG.text(), 'Unauthorized')})
Deno.test('JSX middleware', async () => { const app = new Hono() app.get('/', (c) => { return c.html(<h1>Hello</h1>) }) const res = await app.request('http://localhost/') assertEquals(res.status, 200) assertEquals(res.headers.get('Content-Type'), 'text/html; charset=UTF-8') assertEquals(await res.text(), '<h1>Hello</h1>')
// Fragment const template = ( <> <p>1</p> <p>2</p> </> ) assertEquals(template.toString(), '<p>1</p><p>2</p>')})
Deno.test('JWT Authentication middleware', async () => { const app = new Hono<{ Variables: { 'x-foo': string } }>() app.use('/*', async (c, next) => { await next() c.header('x-foo', c.get('x-foo') || '') }) app.use('/auth/*', jwt({ secret: 'a-secret', alg: 'HS256' })) app.get('/auth/*', (c) => { c.set('x-foo', 'bar') return new Response('auth') })
const req = new Request('http://localhost/auth/a') const res = await app.request(req) assertEquals(res.status, 401) assertEquals(await res.text(), 'Unauthorized') assertEquals(res.headers.get('x-foo'), '')
const credential = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE' const reqOK = new Request('http://localhost/auth/a') reqOK.headers.set('Authorization', `Bearer ${credential}`) const resOK = await app.request(reqOK) assertEquals(resOK.status, 200) assertEquals(await resOK.text(), 'auth') assertEquals(resOK.headers.get('x-foo'), 'bar')})