From 0f763e7913778e3ca6b9e57b81b44a7dce4b4639 Mon Sep 17 00:00:00 2001 From: om-3004 Date: Thu, 17 Oct 2024 11:06:46 +0530 Subject: [PATCH] promisify the resolve method to pass the error to the middleware and updat test cases accordingly --- Server/controllers/monitorController.js | 20 +++++++++---------- .../controllers/monitorController.test.js | 10 ++++------ 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/Server/controllers/monitorController.js b/Server/controllers/monitorController.js index d72a8254b..5ee6d2c5d 100644 --- a/Server/controllers/monitorController.js +++ b/Server/controllers/monitorController.js @@ -276,18 +276,18 @@ const checkEndpointResolution = async (req, res, next) => { try { let { monitorURL } = req.query; monitorURL = new URL(monitorURL); - dns.resolve(monitorURL.hostname, (error) => { - if (error) { - return res.status(400).json({ - success: false, - msg: `DNS resolution failed`, - }); - } - return res.status(200).json({ - success: true, - msg: `URL resolved successfully`, + await new Promise((resolve, reject) => { + dns.resolve(monitorURL.hostname, (error) => { + if (error) { + reject(error); + } + resolve(); }); }); + return res.status(200).json({ + success: true, + msg: `URL resolved successfully`, + }); } catch (error) { next(handleError(error, SERVICE_NAME, "checkEndpointResolution")); } diff --git a/Server/tests/controllers/monitorController.test.js b/Server/tests/controllers/monitorController.test.js index 503a2c5cb..f83d74396 100644 --- a/Server/tests/controllers/monitorController.test.js +++ b/Server/tests/controllers/monitorController.test.js @@ -484,12 +484,10 @@ describe('checkEndpointResolution', () => { const dnsError = new Error("DNS resolution failed"); dnsResolveStub.callsFake((hostname, callback) => callback(dnsError)); await checkEndpointResolution(req, res, next); - expect(res.status.calledOnceWith(400)).to.be.true; - expect(res.json.calledOnceWith({ - success: false, - msg: "DNS resolution failed", - })).to.be.true; - expect(next.notCalled).to.be.true; + expect(next.calledOnce).to.be.true; + const errorPassedToNext = next.getCall(0).args[0]; + expect(errorPassedToNext).to.be.an.instanceOf(Error); + expect(errorPassedToNext.message).to.include('DNS resolution failed'); }); it('should call next with an error for invalid monitorURL', async () => { req.query.monitorURL = 'invalid-url';