promisify the resolve method to pass the error to the middleware and updat test cases accordingly

This commit is contained in:
om-3004 committed 2024-10-17 11:06:46 +05:30
1 parent 7ec5e64f6f
commit 0f763e7913
2 files changed
+14 -16

No files matched your search

+10 -10
View File
@@ -276,18 +276,18 @@ const checkEndpointResolution = async (req, res, next) => {
try { try {
let { monitorURL } = req.query; let { monitorURL } = req.query;
monitorURL = new URL(monitorURL); monitorURL = new URL(monitorURL);
dns.resolve(monitorURL.hostname, (error) => { await new Promise((resolve, reject) => {
if (error) { dns.resolve(monitorURL.hostname, (error) => {
return res.status(400).json({ if (error) {
success: false, reject(error);
msg: `DNS resolution failed`, }
}); resolve();
}
return res.status(200).json({
success: true,
msg: `URL resolved successfully`,
}); });
}); });
return res.status(200).json({
success: true,
msg: `URL resolved successfully`,
});
} catch (error) { } catch (error) {
next(handleError(error, SERVICE_NAME, "checkEndpointResolution")); next(handleError(error, SERVICE_NAME, "checkEndpointResolution"));
} }
@@ -484,12 +484,10 @@ describe('checkEndpointResolution', () => {
const dnsError = new Error("DNS resolution failed"); const dnsError = new Error("DNS resolution failed");
dnsResolveStub.callsFake((hostname, callback) => callback(dnsError)); dnsResolveStub.callsFake((hostname, callback) => callback(dnsError));
await checkEndpointResolution(req, res, next); await checkEndpointResolution(req, res, next);
expect(res.status.calledOnceWith(400)).to.be.true; expect(next.calledOnce).to.be.true;
expect(res.json.calledOnceWith({ const errorPassedToNext = next.getCall(0).args[0];
success: false, expect(errorPassedToNext).to.be.an.instanceOf(Error);
msg: "DNS resolution failed", expect(errorPassedToNext.message).to.include('DNS resolution failed');
})).to.be.true;
expect(next.notCalled).to.be.true;
}); });
it('should call next with an error for invalid monitorURL', async () => { it('should call next with an error for invalid monitorURL', async () => {
req.query.monitorURL = 'invalid-url'; req.query.monitorURL = 'invalid-url';