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
The walk stopped after 200 commits: lines older than f0b094d point at it.
test: move adapter runtime tests into `adapters/*` (#5466)Yusuke Wadabefore adoption645ddd6
line 1why, one step back1import { assertEquals } from '@std/assert'Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 2–92why, one step back2import { Hono } from '../../src/hono.ts'3import { basicAuth } from '../../src/middleware/basic-auth/index.ts'4import { jwt } from '../../src/middleware/jwt/index.ts'56// Test just only minimal patterns.7// Because others are already tested well in Cloudflare Workers environment.89Deno.test('Basic Auth Middleware', async () => {10 const app = new Hono()1112 const username = 'hono'13 const password = 'ahotproject'1415 app.use(16 '/auth/*',17 basicAuth({18 username,19 password,20 })21 )2223 app.get('/auth/*', () => new Response('auth'))2425 const res = await app.request('http://localhost/auth/a')26 assertEquals(res.status, 401)27 assertEquals(await res.text(), 'Unauthorized')2829 const credential = 'aG9ubzphaG90cHJvamVjdA=='3031 const req = new Request('http://localhost/auth/a')32 req.headers.set('Authorization', `Basic ${credential}`)33 const resOK = await app.request(req)34 assertEquals(resOK.status, 200)35 assertEquals(await resOK.text(), 'auth')3637 const invalidCredential = 'G9ubzphY29vbHByb2plY3Q='3839 const req2 = new Request('http://localhost/auth/a')40 req2.headers.set('Authorization', `Basic ${invalidCredential}`)41 const resNG = await app.request(req2)42 assertEquals(resNG.status, 401)43 assertEquals(await resNG.text(), 'Unauthorized')44})4546Deno.test('JSX middleware', async () => {47 const app = new Hono()48 app.get('/', (c) => {49 return c.html(<h1>Hello</h1>)50 })51 const res = await app.request('http://localhost/')52 assertEquals(res.status, 200)53 assertEquals(res.headers.get('Content-Type'), 'text/html; charset=UTF-8')54 assertEquals(await res.text(), '<h1>Hello</h1>')5556 // Fragment57 const template = (58 <>59 <p>1</p>60 <p>2</p>61 </>62 )63 assertEquals(template.toString(), '<p>1</p><p>2</p>')64})6566Deno.test('JWT Authentication middleware', async () => {67 const app = new Hono<{ Variables: { 'x-foo': string } }>()68 app.use('/*', async (c, next) => {69 await next()70 c.header('x-foo', c.get('x-foo') || '')71 })72 app.use('/auth/*', jwt({ secret: 'a-secret', alg: 'HS256' }))73 app.get('/auth/*', (c) => {74 c.set('x-foo', 'bar')75 return new Response('auth')76 })7778 const req = new Request('http://localhost/auth/a')79 const res = await app.request(req)80 assertEquals(res.status, 401)81 assertEquals(await res.text(), 'Unauthorized')82 assertEquals(res.headers.get('x-foo'), '')8384 const credential =85 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE'86 const reqOK = new Request('http://localhost/auth/a')87 reqOK.headers.set('Authorization', `Bearer ${credential}`)88 const resOK = await app.request(reqOK)89 assertEquals(resOK.status, 200)90 assertEquals(await resOK.text(), 'auth')91 assertEquals(resOK.headers.get('x-foo'), 'bar')92})