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
|
|
|
})
|
|
|
|
}
|
|
|
|
it('GET /hello is ok', () => {
|
|
|
|
let req = new fetch.Request('https://example.com/hello')
|
|
|
|
let res = app.dispatch(req)
|
|
|
|
expect(res).not.toBeNull()
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
})
|
|
|
|
it('GET / is not found', () => {
|
|
|
|
let req = new fetch.Request('https://example.com/')
|
|
|
|
let res = app.dispatch(req)
|
|
|
|
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-20 01:18:33 +00:00
|
|
|
app.get('/entry/:id', (req) => {
|
|
|
|
const id = req.params('id')
|
|
|
|
return new fetch.Response(`id is ${id}`, {
|
|
|
|
status: 200,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
app.get('/search', (req) => {
|
|
|
|
const name = req.query('name')
|
|
|
|
return new fetch.Response(`name is ${name}`, {
|
|
|
|
status: 200,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
it('params of /entry/:id is found', async () => {
|
|
|
|
let req = new fetch.Request('https://example.com/entry/123')
|
|
|
|
let res = app.dispatch(req)
|
|
|
|
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')
|
|
|
|
let res = app.dispatch(req)
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
expect(await res.text()).toBe('name is sam')
|
|
|
|
})
|
|
|
|
})
|
2021-12-20 02:19:35 +00:00
|
|
|
|
|
|
|
describe('Middleware', () => {
|
|
|
|
const app = Hono()
|
|
|
|
const logger = (req) => {
|
|
|
|
console.log(`${req.method} : ${req.url}`)
|
|
|
|
}
|
|
|
|
app.get('/*', logger, () => {
|
|
|
|
return new fetch.Response('log!')
|
|
|
|
})
|
|
|
|
it('logger', async () => {
|
|
|
|
let req = new fetch.Request('https://example.com/log')
|
|
|
|
let res = app.dispatch(req)
|
|
|
|
expect(res.status).toBe(200)
|
|
|
|
})
|
|
|
|
})
|