Skip to content

Commit

Permalink
fix route lookup if have query params
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasq committed May 29, 2019
1 parent 5f5595d commit c8e78c3
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ See buildParseQuery.
Change Log
==========

- 0.6.2 - fix router path lookups if have query params
- 0.6.1 - let createHandler also accept onRequest and onError handlers directly, not just via options
- 0.6.0 - Breaking: remove the broken `encoding` handling, make the app decode. Now returns Buffers.
Fix null response handling (`null` is an object not an empty string, JSON serialize it)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "microrest",
"version": "0.6.1",
"version": "0.6.2",
"description": "tiny embeddable REST web framework",
"keywords": ["micro", "nano", "framework", "middleware", "REST", "embeddable", "fast"],
"main": "rest.js",
Expand Down
2 changes: 2 additions & 0 deletions rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ Rest.NanoRouter.prototype.setRoute = function setRoute( path, method, mwStep ) {
this.routes[path] = mwStep;
}
Rest.NanoRouter.prototype.getRoute = function getRoute( path, method ) {
var mark = path.indexOf('?');
if (mark >= 0) path = path.slice(0, mark);
var mwSteps = this.routes[path];
while (!mwSteps && this.matchPrefix && path.length > 1) mwSteps = this.routes[path = path.slice(path, path.lastIndexOf('/')) || '/'];
return mwSteps;
Expand Down
3 changes: 2 additions & 1 deletion router.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ Router.prototype.deleteRoute = function deleteRoute( path, method ) {
}

Router.prototype.getRoute = function getRoute( path, method, route ) {
var mw;
var mw, mark;

if (!path) return null; // path is required
if ((mark = path.indexOf('?')) >= 0) path = path.slice(0, mark);
if (this.steps[path]) return this.steps[path]; // use-, pre-, err- and post- steps
mw = this.maproutes[path] && (this.maproutes[path][method] || this.maproutes[path]['_ANY_']);
if (mw) return mw; // direct-mapped routes
Expand Down
2 changes: 1 addition & 1 deletion test-rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ res.on('data', function(chunk) { console.log("AR: chunk", String(chunk)) });
var router = new Rest.NanoRouter();
router.setRoute('/path2', noop);
t.equal(router.getRoute('/path0'), null);
t.deepEqual(router.getRoute('/path2'), noop);
t.deepEqual(router.getRoute('/path2?a=1'), noop);
t.done();
},

Expand Down
6 changes: 6 additions & 0 deletions test-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ module.exports = {
t.done();
},

'should trim query params': function(t) {
this.router.setRoute('/test/path', this.fn1);
t.deepEqual(this.router.getRoute('/test/path?a=1&b=2'), [this.fn1]);
t.done();
},

'should exact match if tail is not gathered': function(t) {
this.router.setRoute('/:name1/:name2/other', this.fn2);
t.ok(this.router.getRoute('/test/path/other'));
Expand Down

0 comments on commit c8e78c3

Please sign in to comment.