0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 11:51:01 +01:00
hono/test/router.test.ts
Minghe 7c87ac68cd
Setup lint to enable code styles check (#27)
* feat(ci): setup github action to enable ci

* feat(ci): enable lint

* fix(lint): fix critical code style issues
2022-01-07 07:03:54 +09:00

57 lines
1.6 KiB
TypeScript

import { Router } from '../src/hono'
describe('Basic Usage', () => {
const router = new Router<string>()
router.add('GET', '/hello', 'get hello')
router.add('POST', '/hello', 'post hello')
it('get, post hello', async () => {
let res = router.match('GET', '/hello')
expect(res).not.toBeNull()
expect(res.handler).toBe('get hello')
res = router.match('POST', '/hello')
expect(res).not.toBeNull()
expect(res.handler).toBe('post hello')
res = router.match('PUT', '/hello')
expect(res).toBeNull()
res = router.match('GET', '/')
expect(res).toBeNull()
})
})
describe('Complex', () => {
const router = new Router<string>()
it('Named Param', async () => {
router.add('GET', '/entry/:id', 'get entry')
const res = router.match('GET', '/entry/123')
expect(res).not.toBeNull()
expect(res.handler).toBe('get entry')
expect(res.params['id']).toBe('123')
})
it('Wildcard', async () => {
router.add('GET', '/wild/*/card', 'get wildcard')
const res = router.match('GET', '/wild/xxx/card')
expect(res).not.toBeNull()
expect(res.handler).toBe('get wildcard')
})
it('Regexp', async () => {
router.add('GET', '/post/:date{[0-9]+}/:title{[a-z]+}', 'get post')
let res = router.match('GET', '/post/20210101/hello')
expect(res).not.toBeNull()
expect(res.handler).toBe('get post')
expect(res.params['date']).toBe('20210101')
expect(res.params['title']).toBe('hello')
res = router.match('GET', '/post/onetwothree')
expect(res).toBeNull()
res = router.match('GET', '/post/123/123')
expect(res).toBeNull()
})
})