2022-01-03 22:02:31 +00:00
|
|
|
const { makeEdgeEnv } = require('edge-mock')
|
2022-01-01 06:29:41 +00:00
|
|
|
const { Hono } = require('./hono')
|
2021-12-14 19:58:45 +00:00
|
|
|
|
2022-01-03 22:02:31 +00:00
|
|
|
makeEdgeEnv()
|
|
|
|
|
2021-12-20 01:18:33 +00:00
|
|
|
describe('GET Request', () => {
|
2022-01-01 06:29:41 +00:00
|
|
|
const app = new Hono()
|
|
|
|
|
2021-12-14 19:58:45 +00:00
|
|
|
app.get('/hello', () => {
|
2022-01-03 22:02:31 +00:00
|
|
|
return new Response('hello', {
|
2021-12-17 07:43:40 +00:00
|
|
|
status: 200,
|
2021-12-14 19:58:45 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
app.notFound = () => {
|
2022-01-03 22:02:31 +00:00
|
|
|
return new Response('not found', {
|
2021-12-17 07:43:40 +00:00
|
|
|
status: 404,
|
2021-12-14 19:58:45 +00:00
|
|
|
})
|
|
|
|
}
|
2021-12-20 17:35:03 +00:00
|
|
|
it('GET /hello is ok', async () => {
|
2022-01-03 22:02:31 +00:00
|
|
|
let req = new Request('/hello')
|
|
|
|
let res = await app.dispatch(req)
|
2021-12-14 19:58:45 +00:00
|
|
|
expect(res).not.toBeNull()
|
|
|
|
expect(res.status).toBe(200)
|
2021-12-21 08:37:02 +00:00
|
|
|
expect(await res.text()).toBe('hello')
|
2021-12-14 19:58:45 +00:00
|
|
|
})
|
2021-12-20 17:35:03 +00:00
|
|
|
it('GET / is not found', async () => {
|
2022-01-03 22:02:31 +00:00
|
|
|
let req = new Request('/')
|
|
|
|
let res = await app.dispatch(req)
|
2021-12-14 19:58:45 +00:00
|
|
|
expect(res).not.toBeNull()
|
|
|
|
expect(res.status).toBe(404)
|
|
|
|
})
|
|
|
|
})
|
2021-12-20 01:18:33 +00:00
|
|
|
|
|
|
|
describe('params and query', () => {
|
2022-01-01 06:29:41 +00:00
|
|
|
const app = new Hono()
|
2021-12-21 08:37:02 +00:00
|
|
|
|
|
|
|
app.get('/entry/:id', async (c) => {
|
|
|
|
const id = await c.req.params('id')
|
2022-01-03 22:02:31 +00:00
|
|
|
return new Response(`id is ${id}`, {
|
2021-12-20 01:18:33 +00:00
|
|
|
status: 200,
|
|
|
|
})
|
|
|
|
})
|
2021-12-21 08:37:02 +00:00
|
|
|
|
|
|
|
app.get('/search', async (c) => {
|
|
|
|
const name = await c.req.query('name')
|
2022-01-03 22:02:31 +00:00
|
|
|
return new Response(`name is ${name}`, {
|
2021-12-20 01:18:33 +00:00
|
|
|
status: 200,
|
|
|
|
})
|
|
|
|
})
|
2021-12-21 08:37:02 +00:00
|
|
|
|
2021-12-20 01:18:33 +00:00
|
|
|
it('params of /entry/:id is found', async () => {
|
2022-01-03 22:02:31 +00:00
|
|
|
let req = new Request('/entry/123')
|
2021-12-20 17:35:03 +00:00
|
|
|
let res = await app.dispatch(req)
|
2021-12-20 01:18:33 +00:00
|
|
|
expect(res.status).toBe(200)
|
|
|
|
expect(await res.text()).toBe('id is 123')
|
|
|
|
})
|
|
|
|
it('query of /search?name=sam is found', async () => {
|
2022-01-03 22:02:31 +00:00
|
|
|
let req = new Request('/search?name=sam')
|
2021-12-20 17:35:03 +00:00
|
|
|
let res = await app.dispatch(req)
|
2021-12-20 01:18:33 +00:00
|
|
|
expect(res.status).toBe(200)
|
|
|
|
expect(await res.text()).toBe('name is sam')
|
|
|
|
})
|
|
|
|
})
|
2021-12-20 02:19:35 +00:00
|
|
|
|
2021-12-21 08:37:02 +00:00
|
|
|
describe('Middleware', () => {
|
2022-01-01 06:29:41 +00:00
|
|
|
const app = new Hono()
|
2021-12-20 17:35:03 +00:00
|
|
|
|
2022-01-03 09:11:46 +00:00
|
|
|
const logger = async (c, next) => {
|
2021-12-21 08:37:02 +00:00
|
|
|
console.log(`${c.req.method} : ${c.req.url}`)
|
2022-01-03 09:11:46 +00:00
|
|
|
await next()
|
2021-12-20 02:19:35 +00:00
|
|
|
}
|
2021-12-21 08:37:02 +00:00
|
|
|
|
2022-01-03 09:11:46 +00:00
|
|
|
const rootHeader = async (c, next) => {
|
|
|
|
await next()
|
|
|
|
await c.res.headers.append('x-custom', 'root')
|
2021-12-28 05:24:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 09:11:46 +00:00
|
|
|
const customHeader = async (c, next) => {
|
|
|
|
await next()
|
|
|
|
await c.res.headers.append('x-message', 'custom-header')
|
2021-12-28 05:24:31 +00:00
|
|
|
}
|
2022-01-03 09:11:46 +00:00
|
|
|
const customHeader2 = async (c, next) => {
|
|
|
|
await next()
|
2022-01-01 15:29:03 +00:00
|
|
|
c.res.headers.append('x-message-2', 'custom-header-2')
|
2021-12-20 17:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.use('*', logger)
|
2021-12-28 05:24:31 +00:00
|
|
|
app.use('*', rootHeader)
|
2021-12-20 17:35:03 +00:00
|
|
|
app.use('/hello', customHeader)
|
2021-12-28 05:24:31 +00:00
|
|
|
app.use('/hello/*', customHeader2)
|
2022-01-03 09:11:46 +00:00
|
|
|
|
2021-12-20 17:35:03 +00:00
|
|
|
app.get('/hello', () => {
|
2022-01-03 22:02:31 +00:00
|
|
|
return new Response('hello')
|
2021-12-20 02:19:35 +00:00
|
|
|
})
|
2021-12-28 05:24:31 +00:00
|
|
|
app.get('/hello/:message', (c) => {
|
|
|
|
const message = c.req.params('message')
|
2022-01-03 22:02:31 +00:00
|
|
|
return new Response(`${message}`)
|
2021-12-28 05:24:31 +00:00
|
|
|
})
|
2021-12-20 17:35:03 +00:00
|
|
|
|
|
|
|
it('logging and custom header', async () => {
|
2022-01-03 22:02:31 +00:00
|
|
|
let req = new Request('/hello')
|
2021-12-20 17:35:03 +00:00
|
|
|
let res = await app.dispatch(req)
|
2021-12-20 02:19:35 +00:00
|
|
|
expect(res.status).toBe(200)
|
2021-12-20 17:35:03 +00:00
|
|
|
expect(await res.text()).toBe('hello')
|
2021-12-28 05:24:31 +00:00
|
|
|
expect(await res.headers.get('x-custom')).toBe('root')
|
2021-12-21 08:37:02 +00:00
|
|
|
expect(await res.headers.get('x-message')).toBe('custom-header')
|
2021-12-28 05:24:31 +00:00
|
|
|
expect(await res.headers.get('x-message-2')).toBe('custom-header-2')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('logging and custom header with named params', async () => {
|
2022-01-03 22:02:31 +00:00
|
|
|
let req = new Request('/hello/message')
|
2021-12-28 05:24:31 +00:00
|
|
|
let res = await app.dispatch(req)
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
expect(await res.text()).toBe('message')
|
|
|
|
expect(await res.headers.get('x-custom')).toBe('root')
|
|
|
|
expect(await res.headers.get('x-message-2')).toBe('custom-header-2')
|
2021-12-20 02:19:35 +00:00
|
|
|
})
|
|
|
|
})
|
2022-01-01 13:54:29 +00:00
|
|
|
|
|
|
|
describe('Custom 404', () => {
|
|
|
|
const app = new Hono()
|
|
|
|
|
2022-01-03 09:11:46 +00:00
|
|
|
const customNotFound = async (c, next) => {
|
|
|
|
await next()
|
2022-01-01 13:54:29 +00:00
|
|
|
if (c.res.status === 404) {
|
2022-01-03 22:02:31 +00:00
|
|
|
c.res = new Response('Custom 404 Not Found', { status: 404 })
|
2022-01-01 13:54:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
app.notFound = () => {
|
2022-01-03 22:02:31 +00:00
|
|
|
return new Response('Default 404 Nout Found', { status: 404 })
|
2022-01-01 13:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app.use('*', customNotFound)
|
|
|
|
app.get('/hello', () => {
|
2022-01-03 22:02:31 +00:00
|
|
|
return new Response('hello')
|
2022-01-01 13:54:29 +00:00
|
|
|
})
|
|
|
|
it('Custom 404 Not Found', async () => {
|
2022-01-03 22:02:31 +00:00
|
|
|
let req = new Request('/hello')
|
2022-01-01 13:54:29 +00:00
|
|
|
let res = await app.dispatch(req)
|
|
|
|
expect(res.status).toBe(200)
|
2022-01-03 22:02:31 +00:00
|
|
|
req = new Request('/foo')
|
2022-01-01 13:54:29 +00:00
|
|
|
res = await app.dispatch(req)
|
|
|
|
expect(res.status).toBe(404)
|
|
|
|
expect(await res.text()).toBe('Custom 404 Not Found')
|
|
|
|
})
|
|
|
|
})
|
2022-01-01 14:18:48 +00:00
|
|
|
|
|
|
|
describe('Error Handling', () => {
|
|
|
|
const app = new Hono()
|
|
|
|
|
2022-01-03 09:11:46 +00:00
|
|
|
it('Middleware must be async function', () => {
|
2022-01-01 14:18:48 +00:00
|
|
|
expect(() => {
|
2022-01-01 15:29:03 +00:00
|
|
|
app.use('*', {})
|
2022-01-01 14:18:48 +00:00
|
|
|
}).toThrow(TypeError)
|
2022-01-03 09:11:46 +00:00
|
|
|
expect(() => {
|
|
|
|
app.use('*', () => '')
|
|
|
|
}).toThrow(TypeError)
|
2022-01-01 14:18:48 +00:00
|
|
|
})
|
|
|
|
})
|