NodeJS / Express
I have a method inside a controller that call a external API through a POST service. This method uses HystrixJS between then to make the call.
request.js
...
const post = (url, req, res, xsd, body = {}) => {
// ... simple execute the post method and return a promisse
return client.postPromise(url, options).then((response) => (response));
};
...
controller.js
const { post } = require('./services/request');
function Xpto(req, res, xsd) {
let url = `http://host.io/post-method`;
const request = req.app.get('hystrix').hystrixRequestHandler(post, 'blablabla');
const inRequest = req.body;
const inResponse = request.execute(
url,
req,
res,
xsd,
inRequest,
);
return inResponse;
}
// -----------------------------------
function handler(req, res, next) {
const response = await Xpto(
req,
res,
xsd
);
console.log('================>', response);
}
Testing the controller.js mocking services/request to avoid a live API call.
controller.test.js
...
it('should return success', async () => {
mock01 = proxyquire("./controller", {
"./services/request": {
post: () =>
Promise.resolve({
response: { statusCode: 200 },
data: {
xxx: "00000",
},
}),
},
});
const response = await mock01.handler(request, res, next);
const data = response.body[0].data;
expect(response.statusCode).to.equal(200);
});
...
it('should return error', async () => {
mock02 = proxyquire("./controller", {
"./services/request": {
post: () =>
Promise.resolve({
response: { statusCode: 500 },
data: {
zzzz: "99999",
},
}),
},
});
const response = await mock02.handler(request, res, next);
const data = response.body[0].data;
expect(response.statusCode).to.equal(500);
});
Everything works fine except due the Hystrix cache it always return statusCode = 200 even in the mock2 where I explicit define it to return statusCode = 500.
AssertionError: expected 200 to equal 500
+ expected - actual
-200
+500
I looking to find a way to mock the Hystrix call const request = req.app.get('hystrix').hystrixRequestHandler(post, 'blablabla'); or another way that avoid this behavior.