Skip to content

Commit

Permalink
fixed issue with req.param not returning when falsy
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettheaver committed Jan 4, 2016
1 parent 06b6d1a commit da922fd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/mockRequest.js
Expand Up @@ -163,11 +163,11 @@ function createRequest(options) {
* - req.query
*/
mockRequest.param = function(parameterName, defaultValue) {
if (mockRequest.params[parameterName]) {
if (null != mockRequest.params[parameterName]) {
return mockRequest.params[parameterName];
} else if (mockRequest.body[parameterName]) {
} else if (null != mockRequest.body[parameterName]) {
return mockRequest.body[parameterName];
} else if (mockRequest.query[parameterName]) {
} else if (null != mockRequest.query[parameterName]) {
return mockRequest.query[parameterName];
}
return defaultValue;
Expand Down
33 changes: 33 additions & 0 deletions test/lib/mockRequest.spec.js
Expand Up @@ -370,6 +370,17 @@ describe('mockRequest', function() {
expect(request.param('key')).to.equal('value');
});

it('should return falsy param, when found in params', function() {
var options = {
params: {
key: 0
}
};

request = mockRequest.createRequest(options);
expect(request.param('key')).to.equal(0);
});

it('should return param, when found in body', function() {
var options = {
body: {
Expand All @@ -381,6 +392,17 @@ describe('mockRequest', function() {
expect(request.param('key')).to.equal('value');
});

it('should return falsy param, when found in body', function() {
var options = {
body: {
key: 0
}
};

request = mockRequest.createRequest(options);
expect(request.param('key')).to.equal(0);
});

it('should return param, when found in query', function() {
var options = {
query: {
Expand All @@ -392,6 +414,17 @@ describe('mockRequest', function() {
expect(request.param('key')).to.equal('value');
});

it('should return falsy param, when found in query', function() {
var options = {
query: {
key: 0
}
};

request = mockRequest.createRequest(options);
expect(request.param('key')).to.equal(0);
});

it('should not return param, when not found in params/body/query', function() {
request = mockRequest.createRequest();
expect(request.get('key')).to.not.exist;
Expand Down

0 comments on commit da922fd

Please sign in to comment.