Skip to content
Myr

Use test directory (#19)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by GitHub · parent dbb5016 · Browse files · 21 files+614 added−614 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

21 files+614 added−614 removed

  • tests:+543 added−543 removed
  • other:+71 added−71 removed

21 files+614 added−614 removed

added:src/middleware/logger/logger.js+63 added
added:src/middleware/logger/logger.test.js+36 added
added:src/middleware/poweredBy/poweredBy.js+6 added
added:src/middleware/poweredBy/poweredBy.test.js+19 added
deleted:src/middleware/logger.js-63 removed
deleted:src/middleware/logger.test.js-36 removed
deleted:src/middleware/poweredBy.js-6 removed
deleted:src/middleware/poweredBy.test.js-19 removed
deleted:src/compose.test.js-42 removed
1const compose = require('./compose')23describe('compose middleware', () => {4 const middleware = []56 const a = async (c, next) => {7 c.req['log'] = 'log'8 await next()9 }10 middleware.push(a)1112 const b = async (c, next) => {13 await next()14 c.res['header'] = `${c.res.header}-custom-header`15 }16 middleware.push(b)1718 const handler = async (c, next) => {19 c.req['log'] = `${c.req.log} message`20 await next()21 c.res = { message: 'new response' }22 }23 middleware.push(handler)2425 const request = {}26 const response = {}2728 it('Request', async () => {29 const c = { req: request, res: response }30 const composed = compose(middleware)31 await composed(c)32 expect(c.req['log']).not.toBeNull()33 expect(c.req['log']).toBe('log message')34 })35 it('Response', async () => {36 const c = { req: request, res: response }37 const composed = compose(middleware)38 await composed(c)39 expect(c.res['header']).not.toBeNull()40 expect(c.res['message']).toBe('new response')41 })42})
deleted:src/hono.test.js-160 removed
1const { makeEdgeEnv } = require('edge-mock')2const { Hono } = require('./hono')34makeEdgeEnv()56describe('GET Request', () => {7 const app = new Hono()89 app.get('/hello', () => {10 return new Response('hello', {11 status: 200,12 })13 })14 app.notFound = () => {15 return new Response('not found', {16 status: 404,17 })18 }19 it('GET /hello is ok', async () => {20 let req = new Request('/hello')21 let res = await app.dispatch(req)22 expect(res).not.toBeNull()23 expect(res.status).toBe(200)24 expect(await res.text()).toBe('hello')25 })26 it('GET / is not found', async () => {27 let req = new Request('/')28 let res = await app.dispatch(req)29 expect(res).not.toBeNull()30 expect(res.status).toBe(404)31 })32})3334describe('params and query', () => {35 const app = new Hono()3637 app.get('/entry/:id', async (c) => {38 const id = await c.req.params('id')39 return new Response(`id is ${id}`, {40 status: 200,41 })42 })4344 app.get('/search', async (c) => {45 const name = await c.req.query('name')46 return new Response(`name is ${name}`, {47 status: 200,48 })49 })5051 it('params of /entry/:id is found', async () => {52 let req = new Request('/entry/123')53 let res = await app.dispatch(req)54 expect(res.status).toBe(200)55 expect(await res.text()).toBe('id is 123')56 })57 it('query of /search?name=sam is found', async () => {58 let req = new Request('/search?name=sam')59 let res = await app.dispatch(req)60 expect(res.status).toBe(200)61 expect(await res.text()).toBe('name is sam')62 })63})6465describe('Middleware', () => {66 const app = new Hono()6768 const logger = async (c, next) => {69 console.log(`${c.req.method} : ${c.req.url}`)70 await next()71 }7273 const rootHeader = async (c, next) => {74 await next()75 await c.res.headers.append('x-custom', 'root')76 }7778 const customHeader = async (c, next) => {79 await next()80 await c.res.headers.append('x-message', 'custom-header')81 }82 const customHeader2 = async (c, next) => {83 await next()84 c.res.headers.append('x-message-2', 'custom-header-2')85 }8687 app.use('*', logger)88 app.use('*', rootHeader)89 app.use('/hello', customHeader)90 app.use('/hello/*', customHeader2)9192 app.get('/hello', () => {93 return new Response('hello')94 })95 app.get('/hello/:message', (c) => {96 const message = c.req.params('message')97 return new Response(`${message}`)98 })99100 it('logging and custom header', async () => {101 let req = new Request('/hello')102 let res = await app.dispatch(req)103 expect(res.status).toBe(200)104 expect(await res.text()).toBe('hello')105 expect(await res.headers.get('x-custom')).toBe('root')106 expect(await res.headers.get('x-message')).toBe('custom-header')107 expect(await res.headers.get('x-message-2')).toBe('custom-header-2')108 })109110 it('logging and custom header with named params', async () => {111 let req = new Request('/hello/message')112 let res = await app.dispatch(req)113 expect(res.status).toBe(200)114 expect(await res.text()).toBe('message')115 expect(await res.headers.get('x-custom')).toBe('root')116 expect(await res.headers.get('x-message-2')).toBe('custom-header-2')117 })118})119120describe('Custom 404', () => {121 const app = new Hono()122123 const customNotFound = async (c, next) => {124 await next()125 if (c.res.status === 404) {126 c.res = new Response('Custom 404 Not Found', { status: 404 })127 }128 }129130 app.notFound = () => {131 return new Response('Default 404 Nout Found', { status: 404 })132 }133134 app.use('*', customNotFound)135 app.get('/hello', () => {136 return new Response('hello')137 })138 it('Custom 404 Not Found', async () => {139 let req = new Request('/hello')140 let res = await app.dispatch(req)141 expect(res.status).toBe(200)142 req = new Request('/foo')143 res = await app.dispatch(req)144 expect(res.status).toBe(404)145 expect(await res.text()).toBe('Custom 404 Not Found')146 })147})148149describe('Error Handling', () => {150 const app = new Hono()151152 it('Middleware must be async function', () => {153 expect(() => {154 app.use('*', {})155 }).toThrow(TypeError)156 expect(() => {157 app.use('*', () => '')158 }).toThrow(TypeError)159 })160})
modified:src/middleware.js+2 added-2 removed
1const defaultFilter = require('./middleware/defaultFilter')2const poweredBy = require('./middleware/poweredBy')3const logger = require('./middleware/logger')2const poweredBy = require('./middleware/poweredBy/poweredBy')3const logger = require('./middleware/logger/logger')45function Middleware() {}67Middleware.defaultFilter = defaultFilter
4 unmodified lines
deleted:src/middleware.test.js-19 removed
1const { makeEdgeEnv } = require('edge-mock')2const { Hono, Middleware } = require('./hono')34makeEdgeEnv()56describe('Builtin Middleware', () => {7 const app = new Hono()89 app.use('*', Middleware.poweredBy)10 app.get('/', () => new Response('root'))1112 it('Builtin Powered By Middleware', async () => {13 let req = new Request('/')14 let res = await app.dispatch(req)15 expect(res).not.toBeNull()16 expect(res.status).toBe(200)17 expect(res.headers.get('X-Powered-By')).toBe('Hono')18 })19})
deleted:src/node.test.js-135 removed
1const Node = require('./node')23describe('Root Node', () => {4 const node = new Node()5 node.insert('get', '/', 'get root')6 it('get /', () => {7 let res = node.search('get', '/')8 expect(res).not.toBeNull()9 expect(res.handler).toBe('get root')10 expect(node.search('get', '/hello')).toBeNull()11 })12})1314describe('Root Node id not defined', () => {15 const node = new Node()16 node.insert('get', '/hello', 'get hello')17 it('get /', () => {18 expect(node.search('get', '/')).toBeNull()19 })20})2122describe('All with *', () => {23 const node = new Node()24 node.insert('get', '*', 'get all')25 it('get /', () => {26 expect(node.search('get', '/')).not.toBeNull()27 expect(node.search('get', '/hello')).not.toBeNull()28 })29})3031describe('Basic Usage', () => {32 const node = new Node()33 node.insert('get', '/hello', 'get hello')34 node.insert('post', '/hello', 'post hello')35 node.insert('get', '/hello/foo', 'get hello foo')3637 it('get, post /hello', () => {38 expect(node.search('get', '/')).toBeNull()39 expect(node.search('post', '/')).toBeNull()4041 expect(node.search('get', '/hello').handler).toBe('get hello')42 expect(node.search('post', '/hello').handler).toBe('post hello')43 expect(node.search('put', '/hello')).toBeNull()44 })45 it('get /nothing', () => {46 expect(node.search('get', '/nothing')).toBeNull()47 })48 it('/hello/foo, /hello/bar', () => {49 expect(node.search('get', '/hello/foo').handler).toBe('get hello foo')50 expect(node.search('post', '/hello/foo')).toBeNull()51 expect(node.search('get', '/hello/bar')).toBeNull()52 })53 it('/hello/foo/bar', () => {54 expect(node.search('get', '/hello/foo/bar')).toBeNull()55 })56})5758describe('Name path', () => {59 const node = new Node()60 it('get /entry/123', () => {61 node.insert('get', '/entry/:id', 'get entry')62 let res = node.search('get', '/entry/123')63 expect(res).not.toBeNull()64 expect(res.handler).toBe('get entry')65 expect(res.params).not.toBeNull()66 expect(res.params['id']).toBe('123')67 expect(res.params['id']).not.toBe('1234')68 })6970 it('get /entry/456/comment', () => {71 node.insert('get', '/entry/:id', 'get entry')72 let res = node.search('get', '/entry/456/comment')73 expect(res).toBeNull()74 })7576 it('get /entry/789/comment/123', () => {77 node.insert('get', '/entry/:id/comment/:comment_id', 'get comment')78 let res = node.search('get', '/entry/789/comment/123')79 expect(res).not.toBeNull()80 expect(res.handler).toBe('get comment')81 expect(res.params['id']).toBe('789')82 expect(res.params['comment_id']).toBe('123')83 })8485 it('get /map/:location/events', () => {86 node.insert('get', '/map/:location/events', 'get events')87 let res = node.search('get', '/map/yokohama/events')88 expect(res).not.toBeNull()89 expect(res.handler).toBe('get events')90 expect(res.params['location']).toBe('yokohama')91 })92})9394describe('Wildcard', () => {95 const node = new Node()96 it('/wildcard-abc/xxxxxx/wildcard-efg', () => {97 node.insert('get', '/wildcard-abc/*/wildcard-efg', 'wildcard')98 let res = node.search('get', '/wildcard-abc/xxxxxx/wildcard-efg')99 expect(res).not.toBeNull()100 expect(res.handler).toBe('wildcard')101 })102})103104describe('Regexp', () => {105 const node = new Node()106 node.insert('get', '/regex-abc/:id{[0-9]+}/comment/:comment_id{[a-z]+}', 'regexp')107 it('/regexp-abc/123/comment/abc', () => {108 res = node.search('get', '/regex-abc/123/comment/abc')109 expect(res).not.toBeNull()110 expect(res.handler).toBe('regexp')111 expect(res.params['id']).toBe('123')112 expect(res.params['comment_id']).toBe('abc')113 })114 it('/regexp-abc/abc', () => {115 res = node.search('get', '/regex-abc/abc')116 expect(res).toBeNull()117 })118 it('/regexp-abc/123/comment/123', () => {119 res = node.search('get', '/regex-abc/123/comment/123')120 expect(res).toBeNull()121 })122})123124describe('All', () => {125 const node = new Node()126 node.insert('ALL', '/all-methods', 'all methods') // ALL127 it('/all-methods', () => {128 res = node.search('get', '/all-methods')129 expect(res).not.toBeNull()130 expect(res.handler).toBe('all methods')131 res = node.search('put', '/all-methods')132 expect(res).not.toBeNull()133 expect(res.handler).toBe('all methods')134 })135})
deleted:src/router.test.js-88 removed
1const { Hono } = require('./hono')23describe('Basic Usage', () => {4 const router = new Hono()56 it('get, post hello', async () => {7 router.get('/hello', 'get hello')8 router.post('/hello', 'post hello')910 let res = await router.matchRoute('GET', '/hello')11 expect(res).not.toBeNull()12 expect(res.handler[0]).toBe('get hello')1314 res = await router.matchRoute('POST', '/hello')15 expect(res).not.toBeNull()16 expect(res.handler[0]).toBe('post hello')1718 res = await router.matchRoute('PUT', '/hello')19 expect(res).toBeNull()2021 res = await router.matchRoute('GET', '/')22 expect(res).toBeNull()23 })24})2526describe('Complex', () => {27 let router = new Hono()2829 it('Named Param', async () => {30 router.get('/entry/:id', 'get entry')31 res = await router.matchRoute('GET', '/entry/123')32 expect(res).not.toBeNull()33 expect(res.handler[0]).toBe('get entry')34 expect(res.params['id']).toBe('123')35 })3637 it('Wildcard', async () => {38 router.get('/wild/*/card', 'get wildcard')39 res = await router.matchRoute('GET', '/wild/xxx/card')40 expect(res).not.toBeNull()41 expect(res.handler[0]).toBe('get wildcard')42 })4344 it('Regexp', async () => {45 router.get('/post/:date{[0-9]+}/:title{[a-z]+}', 'get post')46 res = await router.matchRoute('GET', '/post/20210101/hello')47 expect(res).not.toBeNull()48 expect(res.handler[0]).toBe('get post')49 expect(res.params['date']).toBe('20210101')50 expect(res.params['title']).toBe('hello')51 res = await router.matchRoute('GET', '/post/onetwothree')52 expect(res).toBeNull()53 res = await router.matchRoute('GET', '/post/123/123')54 expect(res).toBeNull()55 })56})5758describe('Chained Route', () => {59 let router = new Hono()6061 it('Return rooter object', async () => {62 router = router.patch('/hello', 'patch hello')63 expect(router).not.toBeNull()64 router = router.delete('/hello', 'delete hello')65 res = await router.matchRoute('DELETE', '/hello')66 expect(res).not.toBeNull()67 expect(res.handler[0]).toBe('delete hello')68 })6970 it('Chain with route method', async () => {71 router.route('/api/book').get('get book').post('post book').put('put book')7273 res = await router.matchRoute('GET', '/api/book')74 expect(res).not.toBeNull()75 expect(res.handler[0]).toBe('get book')7677 res = await router.matchRoute('POST', '/api/book')78 expect(res).not.toBeNull()79 expect(res.handler[0]).toBe('post book')8081 res = await router.matchRoute('PUT', '/api/book')82 expect(res).not.toBeNull()83 expect(res.handler[0]).toBe('put book')8485 res = await router.matchRoute('DELETE', '/api/book')86 expect(res).toBeNull()87 })88})
deleted:src/util.test.js-44 removed
1const { splitPath, getPattern, getPathFromURL } = require('./util')23describe('Utility methods', () => {4 it('splitPath', () => {5 let ps = splitPath('/')6 expect(ps[0]).toBe('')7 ps = splitPath('/hello')8 expect(ps[0]).toBe('hello')9 ps = splitPath('*')10 expect(ps[0]).toBe('*')11 ps = splitPath('/wildcard-abc/*/wildcard-efg')12 expect(ps[0]).toBe('wildcard-abc')13 expect(ps[1]).toBe('*')14 expect(ps[2]).toBe('wildcard-efg')15 ps = splitPath('/map/:location/events')16 expect(ps[0]).toBe('map')17 expect(ps[1]).toBe(':location')18 expect(ps[2]).toBe('events')19 })2021 it('getPattern', () => {22 let res = getPattern(':id')23 expect(res[0]).toBe('id')24 expect(res[1]).toBe('(.+)')25 res = getPattern(':id{[0-9]+}')26 expect(res[0]).toBe('id')27 expect(res[1]).toBe('([0-9]+)')28 })2930 it('getPathFromURL', () => {31 let path = getPathFromURL('https://example.com/')32 expect(path).toBe('/')33 path = getPathFromURL('https://example.com/hello')34 expect(path).toBe('/hello')35 path = getPathFromURL('https://example.com/hello/hey')36 expect(path).toBe('/hello/hey')37 path = getPathFromURL('https://example.com/hello?name=foo')38 expect(path).toBe('/hello')39 path = getPathFromURL('https://example.com/hello/hey?name=foo&name=bar')40 expect(path).toBe('/hello/hey')41 path = getPathFromURL('https://example.com/hello/hey#fragment')42 expect(path).toBe('/hello/hey')43 })44})
added:test/compose.test.js+42 added
1const compose = require('../src/compose')23describe('compose middleware', () => {4 const middleware = []56 const a = async (c, next) => {7 c.req['log'] = 'log'8 await next()9 }10 middleware.push(a)1112 const b = async (c, next) => {13 await next()14 c.res['header'] = `${c.res.header}-custom-header`15 }16 middleware.push(b)1718 const handler = async (c, next) => {19 c.req['log'] = `${c.req.log} message`20 await next()21 c.res = { message: 'new response' }22 }23 middleware.push(handler)2425 const request = {}26 const response = {}2728 it('Request', async () => {29 const c = { req: request, res: response }30 const composed = compose(middleware)31 await composed(c)32 expect(c.req['log']).not.toBeNull()33 expect(c.req['log']).toBe('log message')34 })35 it('Response', async () => {36 const c = { req: request, res: response }37 const composed = compose(middleware)38 await composed(c)39 expect(c.res['header']).not.toBeNull()40 expect(c.res['message']).toBe('new response')41 })42})
added:test/hono.test.js+160 added
1const { makeEdgeEnv } = require('edge-mock')2const { Hono } = require('../src/hono')34makeEdgeEnv()56describe('GET Request', () => {7 const app = new Hono()89 app.get('/hello', () => {10 return new Response('hello', {11 status: 200,12 })13 })14 app.notFound = () => {15 return new Response('not found', {16 status: 404,17 })18 }19 it('GET /hello is ok', async () => {20 let req = new Request('/hello')21 let res = await app.dispatch(req)22 expect(res).not.toBeNull()23 expect(res.status).toBe(200)24 expect(await res.text()).toBe('hello')25 })26 it('GET / is not found', async () => {27 let req = new Request('/')28 let res = await app.dispatch(req)29 expect(res).not.toBeNull()30 expect(res.status).toBe(404)31 })32})3334describe('params and query', () => {35 const app = new Hono()3637 app.get('/entry/:id', async (c) => {38 const id = await c.req.params('id')39 return new Response(`id is ${id}`, {40 status: 200,41 })42 })4344 app.get('/search', async (c) => {45 const name = await c.req.query('name')46 return new Response(`name is ${name}`, {47 status: 200,48 })49 })5051 it('params of /entry/:id is found', async () => {52 let req = new Request('/entry/123')53 let res = await app.dispatch(req)54 expect(res.status).toBe(200)55 expect(await res.text()).toBe('id is 123')56 })57 it('query of /search?name=sam is found', async () => {58 let req = new Request('/search?name=sam')59 let res = await app.dispatch(req)60 expect(res.status).toBe(200)61 expect(await res.text()).toBe('name is sam')62 })63})6465describe('Middleware', () => {66 const app = new Hono()6768 const logger = async (c, next) => {69 console.log(`${c.req.method} : ${c.req.url}`)70 await next()71 }7273 const rootHeader = async (c, next) => {74 await next()75 await c.res.headers.append('x-custom', 'root')76 }7778 const customHeader = async (c, next) => {79 await next()80 await c.res.headers.append('x-message', 'custom-header')81 }82 const customHeader2 = async (c, next) => {83 await next()84 c.res.headers.append('x-message-2', 'custom-header-2')85 }8687 app.use('*', logger)88 app.use('*', rootHeader)89 app.use('/hello', customHeader)90 app.use('/hello/*', customHeader2)9192 app.get('/hello', () => {93 return new Response('hello')94 })95 app.get('/hello/:message', (c) => {96 const message = c.req.params('message')97 return new Response(`${message}`)98 })99100 it('logging and custom header', async () => {101 let req = new Request('/hello')102 let res = await app.dispatch(req)103 expect(res.status).toBe(200)104 expect(await res.text()).toBe('hello')105 expect(await res.headers.get('x-custom')).toBe('root')106 expect(await res.headers.get('x-message')).toBe('custom-header')107 expect(await res.headers.get('x-message-2')).toBe('custom-header-2')108 })109110 it('logging and custom header with named params', async () => {111 let req = new Request('/hello/message')112 let res = await app.dispatch(req)113 expect(res.status).toBe(200)114 expect(await res.text()).toBe('message')115 expect(await res.headers.get('x-custom')).toBe('root')116 expect(await res.headers.get('x-message-2')).toBe('custom-header-2')117 })118})119120describe('Custom 404', () => {121 const app = new Hono()122123 const customNotFound = async (c, next) => {124 await next()125 if (c.res.status === 404) {126 c.res = new Response('Custom 404 Not Found', { status: 404 })127 }128 }129130 app.notFound = () => {131 return new Response('Default 404 Nout Found', { status: 404 })132 }133134 app.use('*', customNotFound)135 app.get('/hello', () => {136 return new Response('hello')137 })138 it('Custom 404 Not Found', async () => {139 let req = new Request('/hello')140 let res = await app.dispatch(req)141 expect(res.status).toBe(200)142 req = new Request('/foo')143 res = await app.dispatch(req)144 expect(res.status).toBe(404)145 expect(await res.text()).toBe('Custom 404 Not Found')146 })147})148149describe('Error Handling', () => {150 const app = new Hono()151152 it('Middleware must be async function', () => {153 expect(() => {154 app.use('*', {})155 }).toThrow(TypeError)156 expect(() => {157 app.use('*', () => '')158 }).toThrow(TypeError)159 })160})
added:test/middleware.test.js+19 added
1const { makeEdgeEnv } = require('edge-mock')2const { Hono, Middleware } = require('../src/hono')34makeEdgeEnv()56describe('Builtin Middleware', () => {7 const app = new Hono()89 app.use('*', Middleware.poweredBy)10 app.get('/', () => new Response('root'))1112 it('Builtin Powered By Middleware', async () => {13 let req = new Request('/')14 let res = await app.dispatch(req)15 expect(res).not.toBeNull()16 expect(res.status).toBe(200)17 expect(res.headers.get('X-Powered-By')).toBe('Hono')18 })19})
added:test/node.test.js+135 added
1const Node = require('../src/node')23describe('Root Node', () => {4 const node = new Node()5 node.insert('get', '/', 'get root')6 it('get /', () => {7 let res = node.search('get', '/')8 expect(res).not.toBeNull()9 expect(res.handler).toBe('get root')10 expect(node.search('get', '/hello')).toBeNull()11 })12})1314describe('Root Node id not defined', () => {15 const node = new Node()16 node.insert('get', '/hello', 'get hello')17 it('get /', () => {18 expect(node.search('get', '/')).toBeNull()19 })20})2122describe('All with *', () => {23 const node = new Node()24 node.insert('get', '*', 'get all')25 it('get /', () => {26 expect(node.search('get', '/')).not.toBeNull()27 expect(node.search('get', '/hello')).not.toBeNull()28 })29})3031describe('Basic Usage', () => {32 const node = new Node()33 node.insert('get', '/hello', 'get hello')34 node.insert('post', '/hello', 'post hello')35 node.insert('get', '/hello/foo', 'get hello foo')3637 it('get, post /hello', () => {38 expect(node.search('get', '/')).toBeNull()39 expect(node.search('post', '/')).toBeNull()4041 expect(node.search('get', '/hello').handler).toBe('get hello')42 expect(node.search('post', '/hello').handler).toBe('post hello')43 expect(node.search('put', '/hello')).toBeNull()44 })45 it('get /nothing', () => {46 expect(node.search('get', '/nothing')).toBeNull()47 })48 it('/hello/foo, /hello/bar', () => {49 expect(node.search('get', '/hello/foo').handler).toBe('get hello foo')50 expect(node.search('post', '/hello/foo')).toBeNull()51 expect(node.search('get', '/hello/bar')).toBeNull()52 })53 it('/hello/foo/bar', () => {54 expect(node.search('get', '/hello/foo/bar')).toBeNull()55 })56})5758describe('Name path', () => {59 const node = new Node()60 it('get /entry/123', () => {61 node.insert('get', '/entry/:id', 'get entry')62 let res = node.search('get', '/entry/123')63 expect(res).not.toBeNull()64 expect(res.handler).toBe('get entry')65 expect(res.params).not.toBeNull()66 expect(res.params['id']).toBe('123')67 expect(res.params['id']).not.toBe('1234')68 })6970 it('get /entry/456/comment', () => {71 node.insert('get', '/entry/:id', 'get entry')72 let res = node.search('get', '/entry/456/comment')73 expect(res).toBeNull()74 })7576 it('get /entry/789/comment/123', () => {77 node.insert('get', '/entry/:id/comment/:comment_id', 'get comment')78 let res = node.search('get', '/entry/789/comment/123')79 expect(res).not.toBeNull()80 expect(res.handler).toBe('get comment')81 expect(res.params['id']).toBe('789')82 expect(res.params['comment_id']).toBe('123')83 })8485 it('get /map/:location/events', () => {86 node.insert('get', '/map/:location/events', 'get events')87 let res = node.search('get', '/map/yokohama/events')88 expect(res).not.toBeNull()89 expect(res.handler).toBe('get events')90 expect(res.params['location']).toBe('yokohama')91 })92})9394describe('Wildcard', () => {95 const node = new Node()96 it('/wildcard-abc/xxxxxx/wildcard-efg', () => {97 node.insert('get', '/wildcard-abc/*/wildcard-efg', 'wildcard')98 let res = node.search('get', '/wildcard-abc/xxxxxx/wildcard-efg')99 expect(res).not.toBeNull()100 expect(res.handler).toBe('wildcard')101 })102})103104describe('Regexp', () => {105 const node = new Node()106 node.insert('get', '/regex-abc/:id{[0-9]+}/comment/:comment_id{[a-z]+}', 'regexp')107 it('/regexp-abc/123/comment/abc', () => {108 res = node.search('get', '/regex-abc/123/comment/abc')109 expect(res).not.toBeNull()110 expect(res.handler).toBe('regexp')111 expect(res.params['id']).toBe('123')112 expect(res.params['comment_id']).toBe('abc')113 })114 it('/regexp-abc/abc', () => {115 res = node.search('get', '/regex-abc/abc')116 expect(res).toBeNull()117 })118 it('/regexp-abc/123/comment/123', () => {119 res = node.search('get', '/regex-abc/123/comment/123')120 expect(res).toBeNull()121 })122})123124describe('All', () => {125 const node = new Node()126 node.insert('ALL', '/all-methods', 'all methods') // ALL127 it('/all-methods', () => {128 res = node.search('get', '/all-methods')129 expect(res).not.toBeNull()130 expect(res.handler).toBe('all methods')131 res = node.search('put', '/all-methods')132 expect(res).not.toBeNull()133 expect(res.handler).toBe('all methods')134 })135})
added:test/router.test.js+88 added
1const { Hono } = require('../src/hono')23describe('Basic Usage', () => {4 const router = new Hono()56 it('get, post hello', async () => {7 router.get('/hello', 'get hello')8 router.post('/hello', 'post hello')910 let res = await router.matchRoute('GET', '/hello')11 expect(res).not.toBeNull()12 expect(res.handler[0]).toBe('get hello')1314 res = await router.matchRoute('POST', '/hello')15 expect(res).not.toBeNull()16 expect(res.handler[0]).toBe('post hello')1718 res = await router.matchRoute('PUT', '/hello')19 expect(res).toBeNull()2021 res = await router.matchRoute('GET', '/')22 expect(res).toBeNull()23 })24})2526describe('Complex', () => {27 let router = new Hono()2829 it('Named Param', async () => {30 router.get('/entry/:id', 'get entry')31 res = await router.matchRoute('GET', '/entry/123')32 expect(res).not.toBeNull()33 expect(res.handler[0]).toBe('get entry')34 expect(res.params['id']).toBe('123')35 })3637 it('Wildcard', async () => {38 router.get('/wild/*/card', 'get wildcard')39 res = await router.matchRoute('GET', '/wild/xxx/card')40 expect(res).not.toBeNull()41 expect(res.handler[0]).toBe('get wildcard')42 })4344 it('Regexp', async () => {45 router.get('/post/:date{[0-9]+}/:title{[a-z]+}', 'get post')46 res = await router.matchRoute('GET', '/post/20210101/hello')47 expect(res).not.toBeNull()48 expect(res.handler[0]).toBe('get post')49 expect(res.params['date']).toBe('20210101')50 expect(res.params['title']).toBe('hello')51 res = await router.matchRoute('GET', '/post/onetwothree')52 expect(res).toBeNull()53 res = await router.matchRoute('GET', '/post/123/123')54 expect(res).toBeNull()55 })56})5758describe('Chained Route', () => {59 let router = new Hono()6061 it('Return rooter object', async () => {62 router = router.patch('/hello', 'patch hello')63 expect(router).not.toBeNull()64 router = router.delete('/hello', 'delete hello')65 res = await router.matchRoute('DELETE', '/hello')66 expect(res).not.toBeNull()67 expect(res.handler[0]).toBe('delete hello')68 })6970 it('Chain with route method', async () => {71 router.route('/api/book').get('get book').post('post book').put('put book')7273 res = await router.matchRoute('GET', '/api/book')74 expect(res).not.toBeNull()75 expect(res.handler[0]).toBe('get book')7677 res = await router.matchRoute('POST', '/api/book')78 expect(res).not.toBeNull()79 expect(res.handler[0]).toBe('post book')8081 res = await router.matchRoute('PUT', '/api/book')82 expect(res).not.toBeNull()83 expect(res.handler[0]).toBe('put book')8485 res = await router.matchRoute('DELETE', '/api/book')86 expect(res).toBeNull()87 })88})
added:test/util.test.js+44 added
1const { splitPath, getPattern, getPathFromURL } = require('../src/util')23describe('Utility methods', () => {4 it('splitPath', () => {5 let ps = splitPath('/')6 expect(ps[0]).toBe('')7 ps = splitPath('/hello')8 expect(ps[0]).toBe('hello')9 ps = splitPath('*')10 expect(ps[0]).toBe('*')11 ps = splitPath('/wildcard-abc/*/wildcard-efg')12 expect(ps[0]).toBe('wildcard-abc')13 expect(ps[1]).toBe('*')14 expect(ps[2]).toBe('wildcard-efg')15 ps = splitPath('/map/:location/events')16 expect(ps[0]).toBe('map')17 expect(ps[1]).toBe(':location')18 expect(ps[2]).toBe('events')19 })2021 it('getPattern', () => {22 let res = getPattern(':id')23 expect(res[0]).toBe('id')24 expect(res[1]).toBe('(.+)')25 res = getPattern(':id{[0-9]+}')26 expect(res[0]).toBe('id')27 expect(res[1]).toBe('([0-9]+)')28 })2930 it('getPathFromURL', () => {31 let path = getPathFromURL('https://example.com/')32 expect(path).toBe('/')33 path = getPathFromURL('https://example.com/hello')34 expect(path).toBe('/hello')35 path = getPathFromURL('https://example.com/hello/hey')36 expect(path).toBe('/hello/hey')37 path = getPathFromURL('https://example.com/hello?name=foo')38 expect(path).toBe('/hello')39 path = getPathFromURL('https://example.com/hello/hey?name=foo&name=bar')40 expect(path).toBe('/hello/hey')41 path = getPathFromURL('https://example.com/hello/hey#fragment')42 expect(path).toBe('/hello/hey')43 })44})