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

85 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-12-14 19:58:45 +00:00
const Hono = require('./hono')
const fetch = require('node-fetch')
2021-12-20 01:18:33 +00:00
describe('GET Request', () => {
2021-12-20 02:19:35 +00:00
const app = Hono()
2021-12-14 19:58:45 +00:00
app.get('/hello', () => {
return new fetch.Response('hello', {
2021-12-17 07:43:40 +00:00
status: 200,
2021-12-14 19:58:45 +00:00
})
})
app.notFound = () => {
return new fetch.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 () => {
2021-12-14 19:58:45 +00:00
let req = new fetch.Request('https://example.com/hello')
2021-12-20 17:35:03 +00:00
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-20 17:35:03 +00:00
it('GET / is not found', async () => {
2021-12-14 19:58:45 +00:00
let req = new fetch.Request('https://example.com/')
2021-12-20 17:35:03 +00:00
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', () => {
2021-12-20 02:19:35 +00:00
const app = Hono()
2021-12-21 00:52:56 +00:00
app.get('/entry/:id', async (req) => {
const id = await req.params('id')
2021-12-20 01:18:33 +00:00
return new fetch.Response(`id is ${id}`, {
status: 200,
})
})
2021-12-21 00:52:56 +00:00
app.get('/search', async (req) => {
const name = await req.query('name')
let res = new fetch.Response(`name is ${name}`, {
2021-12-20 01:18:33 +00:00
status: 200,
})
2021-12-21 00:52:56 +00:00
return res
2021-12-20 01:18:33 +00:00
})
it('params of /entry/:id is found', async () => {
let req = new fetch.Request('https://example.com/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 () => {
let req = new fetch.Request('https://example.com/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 00:52:56 +00:00
describe.skip('Middleware', () => {
const app = Hono()
2021-12-20 17:35:03 +00:00
const logger = (req, _, next) => {
2021-12-20 02:19:35 +00:00
console.log(`${req.method} : ${req.url}`)
2021-12-20 17:35:03 +00:00
next()
2021-12-20 02:19:35 +00:00
}
2021-12-20 17:35:03 +00:00
const customHeader = (_, res, next) => {
next()
res.headers.append('x-message', 'custom-header')
}
app.use('*', logger)
app.use('/hello', customHeader)
app.get('/hello', () => {
return new fetch.Response('hello')
2021-12-20 02:19:35 +00:00
})
2021-12-20 17:35:03 +00:00
it('logging and custom header', async () => {
let req = new fetch.Request('https://example.com/hello')
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')
expect(res.headers.get('x-message')).toBe('custom-header')
2021-12-20 02:19:35 +00:00
})
})