/** @jsx jsx */ /** @jsxFrag Fragment */ import { basicAuth, jsx, Fragment, serveStatic, jwt } from '../../deno_dist/middleware.ts' import { Hono } from '../../deno_dist/mod.ts' import { assertEquals, assertMatch } from './deps.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-user-a' const password = 'hono-password-a' 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 = 'aG9uby11c2VyLWE6aG9uby1wYXNzd29yZC1h' 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') }) Deno.test('JSX middleware', async () => { const app = new Hono() app.get('/', (c) => { return c.html(
1
2
> ) assertEquals(template.toString(), '1
2
') }) Deno.test('Serve Static middleware', async () => { const app = new Hono() app.all('/favicon.ico', serveStatic({ path: './runtime_tests/deno/favicon.ico' })) app.all( '/favicon-notfound.ico', serveStatic({ path: './runtime_tests/deno/favicon-notfound.ico' }) ) app.use('/favicon-notfound.ico', async (c, next) => { await next() c.header('X-Custom', 'Deno') }) app.get( '/static/*', serveStatic({ root: './runtime_tests/deno', }) ) app.get( '/dot-static/*', serveStatic({ root: './runtime_tests/deno', rewriteRequestPath: (path) => path.replace(/^\/dot-static/, './.static'), }) ) let res = await app.request('http://localhost/favicon.ico') assertEquals(res.status, 200) assertEquals(res.headers.get('Content-Type'), 'image/x-icon') res = await app.request('http://localhost/favicon-notfound.ico') assertEquals(res.status, 404) assertMatch(res.headers.get('Content-Type') || '', /^text\/plain/) assertEquals(res.headers.get('X-Custom'), 'Deno') res = await app.request('http://localhost/static/plain.txt') assertEquals(res.status, 200) assertEquals(await res.text(), 'Deno!') res = await app.request('http://localhost/dot-static/plain.txt') assertEquals(res.status, 200) assertEquals(await res.text(), 'Deno!!') }) 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' })) 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') })