diff --git a/lib/tree.js b/lib/tree.js index 97988e9..80890aa 100644 --- a/lib/tree.js +++ b/lib/tree.js @@ -136,7 +136,7 @@ class RouteNode { const first = path.shift(); if(this.children.get(first)) { // advance in tree - return this.children.get(first).find(path); + return this.children.get(first).find(path, params); } for(const [regex, route] of this.regex_routes.entries()) { diff --git a/test/app/app.js b/test/app/app.js index 864c497..646e4fc 100644 --- a/test/app/app.js +++ b/test/app/app.js @@ -113,7 +113,7 @@ describe('App', () => { expect(response).to.be.instanceof(Response); expect(response.status).to.be.eql(status); expect(response.body).to.be.eql(body); - expect(response.headers['Content-Type']).to.be.eql(content_type); + expect(response.headers.get('Content-Type')).to.be.eql(content_type); done(); }).catch(e => done(e)); }); @@ -132,6 +132,37 @@ describe('App', () => { }).catch(e => done(e)); }); + for(const route of [ + '/something/:name/something_else', + '/:name', + '/something/:name', + ]){ + it('Passes params correctly', done => { + const router = new Router(); + router.get(route, (request, { name }) => { + return { name }; + }); + + const app = new App(router); + + app.run({ + request: { + url: `https://google.com${route.replace(/:name/, 'test_string')}`, + method: 'GET' + } + }).then(result => { + expect(result).to.be.instanceof(Response); + expect(result.status).to.be.eql(200); + expect(result.body).to.be.a('string'); + const data = JSON.parse(result.body); + + expect(data).to.have.property('name'); + expect(data.name).to.be.eql('test_string'); + done(); + }).catch(e => done(e)); + }); + } + it('Runs with pre processing', done => { const router = new Router(); diff --git a/test/routing/lib/tree.js b/test/routing/lib/tree.js index 1d329a3..d43f3f5 100644 --- a/test/routing/lib/tree.js +++ b/test/routing/lib/tree.js @@ -165,6 +165,41 @@ describe('RouterTree', () => { expect(params.plep).to.be.eql('plip'); }); + it('Should find a nested lookup route', () => { + const tree = new RouterTree(); + const cb = () => {}; + tree.register('any', '/one/:plep/two', cb); + + const callbacks_and_params = [ + '/one/123/two', + '/one/plep/two', + '/one/another-name/two' + ].map(path => tree.find('get', path)); + + const callbacks = callbacks_and_params.map(({ callback }) => callback); + const callbacks_set = new Set(callbacks); + expect(callbacks_set.size).to.be.eql(1); + expect(Array.from(callbacks_set)[0]).to.be.eql(cb); + + for(const { params } of callbacks_and_params) { + expect(params).to.have.property('plep'); + expect(['123', 'plep', 'another-name'].includes(params.plep)).to.be.true; + } + }); + + it('Should find multiple lookup route', () => { + const tree = new RouterTree(); + const cb = () => {}; + tree.register('any', '/one/:name/two/:id', cb); + + const { callback, params } = tree.find('get', '/one/test_name/two/test_id'); + expect(callback).to.be.eql(cb); + expect(params).to.have.property('name'); + expect(params).to.have.property('id'); + expect(params.name).to.be.eql('test_name'); + expect(params.id).to.be.eql('test_id'); + }); + it('Should find a regex route', () => { const tree = new RouterTree(); const cb = () => {};