For example suppose I have the following
app.get('/', function(req, res) {
var ip;
if(req.headers['x-forwarded-for']){
ip = req.headers['x-forwarded-for'];
}
else {
ip = req.connection.remoteAddress;
}
});
I would like to unit test to see whether ip is being properly retrieved. One way is as follows
function getIp(req) {
var ip;
if(req.headers['x-forwarded-for']){
ip = req.headers['x-forwarded-for'];
}
else {
ip = req.connection.remoteAddress;
}
return ip;
}
app.get('/', function(req, res) {
var ip = getIp(req);
});
Now I have a function getIp that I can unit test. However I’m still stuck. How can I feed a simulated req object into getIp?
I would just write integration-tests for that. Node.js is fast enough for that. Especially when you use something like Mocha’s watch-mode. You could use something like superagent or request to perform http requests.
There is also something like for example nock to mock out your http requests. Although I have never used it because integration-tests test the real thing and are fast enough for my tast.