I am busy building functional tests for an existing nodejs api I wrote. I am using mocha and expect.js.
One test that is failing is when I want to do a negative test for the existence of a url parameter. What I want is to send a message back to the the consumer of the service if the param is missing.
Currently the positive test I have works:
var request = require('request');
it('get a specific user from the api', function (done) {
request.get('http://localhost:8080/api/v1/user/123', function (error, response, body) { ... });
});
However, the following does not work and all I get is a timeout:
it('should return a 400 message if UserId is not supplied stating that "UserId expected"', function(done) {
request.get('http://localhost:8080/api/v1/user/', function (error, response, body) {
if (!error && response.statusCode == 400) {
expect(body).to.be.equal('UserId expected');
done();
}
});
});
Both the above tests are testing this endpoint:
app.get('/api/v1/user/:userid', function (req, res) {
if(!req.params.userid) {
return res.status(400).json('UserId expected');
}
... get the user and return it with status 200 ...
});
However, as stated before, the first test is successful while the second test times out.
Update 1: Additionally, I am getting the following output:
GET /api/v1/user/123 200 2.905 ms - 274
GET /api/v1/user/ - - ms - -
Update 2: So adding the following code solves the problem, but doesn't really solve the problem:
app.get('/api/v1/user', function (req, res) {
return res.status(400).json('UserId expected');
});
Obviously I don't want a rather redundant piece of code sitting around. I am baffled as to why I cannot enter the 'api/v1/user/:userid' endpoint and just test for the existence of :userid
in there? It's as if nodejs is requiring the existence of :userid
, otherwise the endpoint does not exist. Is this correct?
What am I missing?