I'm building a front-end application that makes a request to another web service and get's a response on a POST request:
http://greetings_api:3000/greeting
data {'language': 'es'}
Response: 'Hola'
I'm using nock to override the HTTP response:
module.exports = {
test: function(req, res) {
var http = require('http');
var nock = require('nock');
var greeting;
nock('http://greetings_api:3000').
post('/greeting').
socketDelay(2000).
reply(200, 'Hola');
var options = {
hostname: 'greetings_api',
port: 3000,
path: '/greeting',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'language': 'es'
}
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (body) {
greeting = body;
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
return res.send(greeting + ' tester')
}
};
My problem is that my tests are failing because greeting is being set properly:
1) Test Route
Test() function
Should welcome us:
AssertionError: expected 'undefined tester' to include 'Hola tester'
at Context.<anonymous> (/tests/routes/app.test.js:20:43)
Why is greeting not being properly defined here and/or why is the request not being intercepted properly?
Aucun commentaire:
Enregistrer un commentaire