Skip to content

Commit

Permalink
Routing: fix nested params routing
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmrcaga committed Mar 10, 2022
1 parent 76a4109 commit 7f50a15
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
33 changes: 32 additions & 1 deletion test/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
Expand All @@ -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();

Expand Down
35 changes: 35 additions & 0 deletions test/routing/lib/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {};
Expand Down

0 comments on commit 7f50a15

Please sign in to comment.