2022-07-12 22:22:28 +00:00
|
|
|
/** @jsx jsx */
|
|
|
|
/** @jsxFrag Fragment */
|
2023-05-02 12:16:17 +00:00
|
|
|
import { basicAuth, jsx, Fragment, serveStatic, jwt } from '../../deno_dist/middleware.ts'
|
|
|
|
import { Hono } from '../../deno_dist/mod.ts'
|
2022-09-19 03:22:55 +00:00
|
|
|
import { assertEquals, assertMatch } from './deps.ts'
|
2022-07-12 22:22:28 +00:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
2023-05-21 11:48:35 +00:00
|
|
|
const username = 'hono'
|
|
|
|
const password = 'acoolproject'
|
2022-07-12 22:22:28 +00:00
|
|
|
|
|
|
|
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')
|
|
|
|
|
2023-05-21 11:48:35 +00:00
|
|
|
const credential = 'aG9ubzphY29vbHByb2plY3Q='
|
2022-07-12 22:22:28 +00:00
|
|
|
|
|
|
|
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')
|
2023-05-21 11:48:35 +00:00
|
|
|
|
|
|
|
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')
|
2022-07-12 22:22:28 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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('Serve Static middleware', async () => {
|
|
|
|
const app = new Hono()
|
2023-05-02 12:16:17 +00:00
|
|
|
app.all('/favicon.ico', serveStatic({ path: './runtime_tests/deno/favicon.ico' }))
|
|
|
|
app.all(
|
|
|
|
'/favicon-notfound.ico',
|
|
|
|
serveStatic({ path: './runtime_tests/deno/favicon-notfound.ico' })
|
|
|
|
)
|
2022-10-09 15:11:36 +00:00
|
|
|
app.use('/favicon-notfound.ico', async (c, next) => {
|
|
|
|
await next()
|
|
|
|
c.header('X-Custom', 'Deno')
|
|
|
|
})
|
2023-05-03 14:05:21 +00:00
|
|
|
|
|
|
|
app.get(
|
|
|
|
'/static/*',
|
|
|
|
serveStatic({
|
|
|
|
root: './runtime_tests/deno',
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
'/dot-static/*',
|
|
|
|
serveStatic({
|
|
|
|
root: './runtime_tests/deno',
|
|
|
|
rewriteRequestPath: (path) => path.replace(/^\/dot-static/, './.static'),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2022-09-19 03:22:55 +00:00
|
|
|
let res = await app.request('http://localhost/favicon.ico')
|
2022-07-12 22:22:28 +00:00
|
|
|
assertEquals(res.status, 200)
|
2022-08-09 01:51:08 +00:00
|
|
|
assertEquals(res.headers.get('Content-Type'), 'image/x-icon')
|
2023-05-03 14:05:21 +00:00
|
|
|
|
2022-09-19 03:22:55 +00:00
|
|
|
res = await app.request('http://localhost/favicon-notfound.ico')
|
|
|
|
assertEquals(res.status, 404)
|
|
|
|
assertMatch(res.headers.get('Content-Type') || '', /^text\/plain/)
|
2022-10-09 15:11:36 +00:00
|
|
|
assertEquals(res.headers.get('X-Custom'), 'Deno')
|
2023-05-03 14:05:21 +00:00
|
|
|
|
|
|
|
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!!')
|
2022-07-12 22:22:28 +00:00
|
|
|
})
|
2022-07-13 12:06:02 +00:00
|
|
|
|
|
|
|
Deno.test('JWT Authentication middleware', async () => {
|
2023-01-03 13:00:29 +00:00
|
|
|
const app = new Hono<{ Variables: { 'x-foo': string } }>()
|
2022-07-13 12:06:02 +00:00
|
|
|
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')
|
|
|
|
})
|