From 7448ec04fc173a23df720957096146b084977d7a Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Mon, 20 Dec 2021 09:32:53 +0900 Subject: [PATCH 1/2] Fixed tests (#4) --- src/node.root.test.js | 17 ----------------- src/node.test.js | 23 ++++++++++++++++++++++- 2 files changed, 22 insertions(+), 18 deletions(-) delete mode 100644 src/node.root.test.js diff --git a/src/node.root.test.js b/src/node.root.test.js deleted file mode 100644 index 9b577b59..00000000 --- a/src/node.root.test.js +++ /dev/null @@ -1,17 +0,0 @@ -const Node = require('./node') - -describe('Basic Usage', () => { - const node = new Node() - node.insert('get', '/', 'get root') - it('get /', () => { - expect(node.search('get', '/')).not.toBeNull() - }) -}) - -describe('Basic Usage', () => { - const node = new Node() - node.insert('get', '/hello', 'get hello') - it('get /', () => { - expect(node.search('get', '/')).toBeNull() - }) -}) diff --git a/src/node.test.js b/src/node.test.js index b1256992..e3f2183e 100644 --- a/src/node.test.js +++ b/src/node.test.js @@ -1,7 +1,7 @@ const Node = require('./node') -const node = new Node() describe('Util Methods', () => { + const node = new Node() it('node.splitPath', () => { let ps = node.splitPath('/') expect(ps[0]).toBe('/') @@ -11,7 +11,24 @@ describe('Util Methods', () => { }) }) +describe('Root Node', () => { + const node = new Node() + node.insert('get', '/', 'get root') + it('get /', () => { + expect(node.search('get', '/')).not.toBeNull() + }) +}) + +describe('Root Node', () => { + const node = new Node() + node.insert('get', '/hello', 'get hello') + it('get /', () => { + expect(node.search('get', '/')).toBeNull() + }) +}) + describe('Basic Usage', () => { + const node = new Node() node.insert('get', '/hello', 'get hello') node.insert('post', '/hello', 'post hello') node.insert('get', '/hello/foo', 'get hello foo') @@ -38,6 +55,7 @@ describe('Basic Usage', () => { }) describe('Name path', () => { + const node = new Node() it('get /entry/123', () => { node.insert('get', '/entry/:id', 'get entry') let res = node.search('get', '/entry/123') @@ -65,6 +83,7 @@ describe('Name path', () => { }) describe('Wildcard', () => { + const node = new Node() it('/wildcard-abc/xxxxxx/wildcard-efg', () => { node.insert('get', '/wildcard-abc/*/wildcard-efg', 'wildcard') res = node.search('get', '/wildcard-abc/xxxxxx/wildcard-efg') @@ -74,6 +93,7 @@ describe('Wildcard', () => { }) describe('Regexp', () => { + const node = new Node() node.insert( 'get', '/regex-abc/:id{[0-9]+}/comment/:comment_id{[a-z]+}', @@ -97,6 +117,7 @@ describe('Regexp', () => { }) describe('All', () => { + const node = new Node() node.insert('all', '/all-methods', 'all methods') it('/all-methods', () => { res = node.search('get', '/all-methods') From 7cc5295ca0afdbd0beaf58dec7856a980732019c Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Mon, 20 Dec 2021 10:19:42 +0900 Subject: [PATCH 2/2] Support query and param method on Request (#5) * Fixed tests * Support params and query --- src/hono.js | 21 +++++++++++++-------- src/hono.test.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/hono.js b/src/hono.js index 382dee21..9ca0b561 100644 --- a/src/hono.js +++ b/src/hono.js @@ -16,13 +16,13 @@ class Router { return WrappedRouter(this) } - matchRoute(method, path) { + match(method, path) { method = method.toLowerCase() const res = this.node.search(method, path) return res } - handle(event) { + handleEvent(event) { const result = this.dispatch(event.request) const response = this.filter(result) return event.respondWith(response) @@ -53,15 +53,20 @@ class Router { dispatch(request) { const url = new URL(request.url) - const path = url.pathname - const method = request.method - const res = this.matchRoute(method, path) + const result = this.match(request.method, url.pathname) - if (!res) { + if (!result) { return this.notFound() } - const handler = res.handler + request.query = (key) => { + return url.searchParams.get(key) + } + request.params = (key) => { + return result.params[key] + } + + const handler = result.handler return handler(request) } @@ -76,7 +81,7 @@ class Router { fire() { addEventListener('fetch', (event) => { - this.handle(event) + this.handleEvent(event) }) } } diff --git a/src/hono.test.js b/src/hono.test.js index 75e23d1f..7d2ab3b0 100644 --- a/src/hono.test.js +++ b/src/hono.test.js @@ -3,7 +3,7 @@ const fetch = require('node-fetch') const app = Hono() -describe('GET match', () => { +describe('GET Request', () => { app.get('/hello', () => { return new fetch.Response('hello', { status: 200, @@ -27,3 +27,30 @@ describe('GET match', () => { expect(res.status).toBe(404) }) }) + +describe('params and query', () => { + 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') + }) +})