0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 11:51:01 +01:00
hono/test/hono.test.ts

203 lines
5.7 KiB
TypeScript
Raw Normal View History

import makeServiceWorkerEnv from 'service-worker-mock'
import { Hono } from '../src/hono'
2021-12-14 20:58:45 +01:00
declare let global: any
Object.assign(global, makeServiceWorkerEnv())
2021-12-20 02:18:33 +01:00
describe('GET Request', () => {
const app = new Hono()
2021-12-14 20:58:45 +01:00
app.get('/hello', () => {
return new Response('hello', {
2021-12-17 08:43:40 +01:00
status: 200,
2021-12-14 20:58:45 +01:00
})
})
app.notFound = () => {
return new Response('not found', {
2021-12-17 08:43:40 +01:00
status: 404,
2021-12-14 20:58:45 +01:00
})
}
2021-12-20 18:35:03 +01:00
it('GET /hello is ok', async () => {
const req = new Request('/hello')
const res = await app.dispatch(req)
2021-12-14 20:58:45 +01:00
expect(res).not.toBeNull()
expect(res.status).toBe(200)
2021-12-21 09:37:02 +01:00
expect(await res.text()).toBe('hello')
2021-12-14 20:58:45 +01:00
})
2021-12-20 18:35:03 +01:00
it('GET / is not found', async () => {
const req = new Request('/')
const res = await app.dispatch(req)
2021-12-14 20:58:45 +01:00
expect(res).not.toBeNull()
expect(res.status).toBe(404)
})
})
2021-12-20 02:18:33 +01:00
describe('Routing', () => {
const app = new Hono()
it('Return it self', async () => {
const appRes = app.get('/', () => new Response('get /'))
expect(appRes).not.toBeUndefined()
appRes.delete('/', () => new Response('delete /'))
const req = new Request('/', { method: 'DELETE' })
const res = await appRes.dispatch(req)
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(await res.text()).toBe('delete /')
})
it('Chained route', async () => {
app
.route('/route')
.get(() => new Response('get /route'))
.post(() => new Response('post /route'))
.put(() => new Response('put /route'))
let req = new Request('/route', { method: 'GET' })
let res = await app.dispatch(req)
expect(res.status).toBe(200)
expect(await res.text()).toBe('get /route')
req = new Request('/route', { method: 'POST' })
res = await app.dispatch(req)
expect(res.status).toBe(200)
expect(await res.text()).toBe('post /route')
req = new Request('/route', { method: 'DELETE' })
res = await app.dispatch(req)
expect(res.status).toBe(404)
})
it('Chained route without route method', async () => {
app
.get('/without-route', () => new Response('get /without-route'))
.post(() => new Response('post /without-route'))
.put(() => new Response('put /without-route'))
let req = new Request('/without-route', { method: 'GET' })
let res = await app.dispatch(req)
expect(res.status).toBe(200)
expect(await res.text()).toBe('get /without-route')
req = new Request('/without-route', { method: 'POST' })
res = await app.dispatch(req)
expect(res.status).toBe(200)
expect(await res.text()).toBe('post /without-route')
req = new Request('/without-route', { method: 'DELETE' })
res = await app.dispatch(req)
expect(res.status).toBe(404)
})
})
2021-12-20 02:18:33 +01:00
describe('params and query', () => {
const app = new Hono()
2021-12-21 09:37:02 +01:00
app.get('/entry/:id', async (c) => {
const id = await c.req.params('id')
return new Response(`id is ${id}`, {
2021-12-20 02:18:33 +01:00
status: 200,
})
})
2021-12-21 09:37:02 +01:00
app.get('/search', async (c) => {
const name = await c.req.query('name')
return new Response(`name is ${name}`, {
2021-12-20 02:18:33 +01:00
status: 200,
})
})
2021-12-21 09:37:02 +01:00
2021-12-20 02:18:33 +01:00
it('params of /entry/:id is found', async () => {
const req = new Request('/entry/123')
const res = await app.dispatch(req)
2021-12-20 02:18:33 +01:00
expect(res.status).toBe(200)
expect(await res.text()).toBe('id is 123')
})
it('query of /search?name=sam is found', async () => {
const req = new Request('/search?name=sam')
const res = await app.dispatch(req)
2021-12-20 02:18:33 +01:00
expect(res.status).toBe(200)
expect(await res.text()).toBe('name is sam')
})
})
2021-12-20 03:19:35 +01:00
2021-12-21 09:37:02 +01:00
describe('Middleware', () => {
const app = new Hono()
2021-12-20 18:35:03 +01:00
// Custom Logger
app.use('*', async (c, next) => {
2021-12-21 09:37:02 +01:00
console.log(`${c.req.method} : ${c.req.url}`)
2022-01-03 10:11:46 +01:00
await next()
})
2021-12-21 09:37:02 +01:00
// Apeend Custom Header
app.use('*', async (c, next) => {
2022-01-03 10:11:46 +01:00
await next()
await c.res.headers.append('x-custom', 'root')
})
2021-12-28 06:24:31 +01:00
app.use('/hello', async (c, next) => {
2022-01-03 10:11:46 +01:00
await next()
await c.res.headers.append('x-message', 'custom-header')
})
2021-12-20 18:35:03 +01:00
app.use('/hello/*', async (c, next) => {
await next()
await c.res.headers.append('x-message-2', 'custom-header-2')
})
2022-01-03 10:11:46 +01:00
2021-12-20 18:35:03 +01:00
app.get('/hello', () => {
return new Response('hello')
2021-12-20 03:19:35 +01:00
})
2021-12-28 06:24:31 +01:00
app.get('/hello/:message', (c) => {
const message = c.req.params('message')
return new Response(`${message}`)
2021-12-28 06:24:31 +01:00
})
2021-12-20 18:35:03 +01:00
it('logging and custom header', async () => {
const req = new Request('/hello')
const res = await app.dispatch(req)
2021-12-20 03:19:35 +01:00
expect(res.status).toBe(200)
2021-12-20 18:35:03 +01:00
expect(await res.text()).toBe('hello')
2021-12-28 06:24:31 +01:00
expect(await res.headers.get('x-custom')).toBe('root')
2021-12-21 09:37:02 +01:00
expect(await res.headers.get('x-message')).toBe('custom-header')
2021-12-28 06:24:31 +01:00
expect(await res.headers.get('x-message-2')).toBe('custom-header-2')
})
it('logging and custom header with named params', async () => {
const req = new Request('/hello/message')
const res = await app.dispatch(req)
2021-12-28 06:24:31 +01:00
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 03:19:35 +01:00
})
})
2022-01-01 14:54:29 +01:00
describe('Custom 404', () => {
const app = new Hono()
app.notFound = () => {
return new Response('Default 404 Nout Found', { status: 404 })
}
app.use('*', async (c, next) => {
2022-01-03 10:11:46 +01:00
await next()
2022-01-01 14:54:29 +01:00
if (c.res.status === 404) {
c.res = new Response('Custom 404 Not Found', { status: 404 })
2022-01-01 14:54:29 +01:00
}
})
2022-01-01 14:54:29 +01:00
app.get('/hello', () => {
return new Response('hello')
2022-01-01 14:54:29 +01:00
})
2022-01-01 14:54:29 +01:00
it('Custom 404 Not Found', async () => {
let req = new Request('/hello')
2022-01-01 14:54:29 +01:00
let res = await app.dispatch(req)
expect(res.status).toBe(200)
req = new Request('/foo')
2022-01-01 14:54:29 +01:00
res = await app.dispatch(req)
expect(res.status).toBe(404)
expect(await res.text()).toBe('Custom 404 Not Found')
})
})