Skip to content

Commit

Permalink
Fix server.param with false values (#1572)
Browse files Browse the repository at this point in the history
* fix(server): check for property instead of value in server.param

* test(server): cover server.param with tests
  • Loading branch information
hekike committed Nov 27, 2017
1 parent d5cd7b9 commit ea563b7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/server.js
Expand Up @@ -527,7 +527,7 @@ Server.prototype.use = function use() {
*/
Server.prototype.param = function param(name, fn) {
this.use(function _param(req, res, next) {
if (req.params && req.params[name]) {
if (req.params && req.params.hasOwnProperty(name)) {
fn.call(this, req, res, next, req.params[name], name);
} else {
next();
Expand Down
54 changes: 54 additions & 0 deletions test/server.test.js
Expand Up @@ -1611,6 +1611,60 @@ test('gh-193 route ENOEXIST', function(t) {
});
});

test('run param only with existing req.params', function(t) {
var count = 0;

SERVER.param('name', function(req, res, next) {
count++;
next();
});

SERVER.param('userId', function(req, res, next, param, name) {
t.equal(param, '1');
t.equal(name, 'userId');
count++;
next();
});

SERVER.get('/users/:userId', function(req, res, next) {
res.send(200);
});

CLIENT.get('/users/1', function(err, _, res) {
t.ifError(err);
t.equal(res.statusCode, 200);
t.equal(count, 1);
t.end();
});
});

test('run param with false value', function(t) {
var count = 0;

SERVER.param('name', function(req, res, next) {
count++;
next();
});

SERVER.param('userId', function(req, res, next, param, name) {
t.equal(param, '');
t.equal(name, 'userId');
count++;
next();
});

SERVER.get('/users/:userId', function(req, res, next) {
res.send(200);
});

CLIENT.get('/users//', function(err, _, res) {
t.ifError(err);
t.equal(res.statusCode, 200);
t.equal(count, 1);
t.end();
});
});

test('gh-193 route only run use once', function(t) {
var count = 0;

Expand Down

0 comments on commit ea563b7

Please sign in to comment.