rewrite monitor resolution check, update tests
This commit is contained in:
1 parent
cb0271613e
commit
58be0c018b
2 files changed
+21
-21
No files matched your search
@@ -19,7 +19,7 @@ import jwt from "jsonwebtoken";
|
|||||||
import { getTokenFromHeaders } from "../utils/utils.js";
|
import { getTokenFromHeaders } from "../utils/utils.js";
|
||||||
import logger from "../utils/logger.js";
|
import logger from "../utils/logger.js";
|
||||||
import { handleError, handleValidationError } from "./controllerUtils.js";
|
import { handleError, handleValidationError } from "./controllerUtils.js";
|
||||||
import dns from "dns";
|
import axios from "axios";
|
||||||
|
|
||||||
const SERVICE_NAME = "monitorController";
|
const SERVICE_NAME = "monitorController";
|
||||||
|
|
||||||
@@ -288,18 +288,16 @@ const checkEndpointResolution = async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let { monitorURL } = req.query;
|
const { monitorURL } = req.query;
|
||||||
monitorURL = new URL(monitorURL);
|
const parsedUrl = new URL(monitorURL);
|
||||||
await new Promise((resolve, reject) => {
|
const response = await axios.get(parsedUrl, {
|
||||||
dns.resolve(monitorURL.hostname, (error) => {
|
timeout: 5000,
|
||||||
if (error) {
|
validateStatus: () => true,
|
||||||
reject(error);
|
|
||||||
}
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
|
code: response.status,
|
||||||
|
statusText: response.statusText,
|
||||||
msg: `URL resolved successfully`,
|
msg: `URL resolved successfully`,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import jwt from "jsonwebtoken";
|
|||||||
import sinon from "sinon";
|
import sinon from "sinon";
|
||||||
import { successMessages } from "../../utils/messages.js";
|
import { successMessages } from "../../utils/messages.js";
|
||||||
import logger from "../../utils/logger.js";
|
import logger from "../../utils/logger.js";
|
||||||
import dns from "dns";
|
import axios from "axios";
|
||||||
const SERVICE_NAME = "monitorController";
|
const SERVICE_NAME = "monitorController";
|
||||||
|
|
||||||
describe("Monitor Controller - getAllMonitors", () => {
|
describe("Monitor Controller - getAllMonitors", () => {
|
||||||
@@ -476,38 +476,40 @@ describe("Monitor Controller - createMonitor", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Monitor Controllor - checkEndpointResolution", () => {
|
describe("Monitor Controller - checkEndpointResolution", () => {
|
||||||
let req, res, next, dnsResolveStub;
|
let req, res, next, axiosGetStub;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
req = { query: { monitorURL: "https://example.com" } };
|
req = { query: { monitorURL: "https://example.com" } };
|
||||||
res = { status: sinon.stub().returnsThis(), json: sinon.stub() };
|
res = { status: sinon.stub().returnsThis(), json: sinon.stub() };
|
||||||
next = sinon.stub();
|
next = sinon.stub();
|
||||||
dnsResolveStub = sinon.stub(dns, "resolve");
|
axiosGetStub = sinon.stub(axios, "get");
|
||||||
});
|
});
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
dnsResolveStub.restore();
|
sinon.restore();
|
||||||
});
|
});
|
||||||
it("should resolve the URL successfully", async () => {
|
it("should resolve the URL successfully", async () => {
|
||||||
dnsResolveStub.callsFake((hostname, callback) => callback(null));
|
axiosGetStub.resolves({ status: 200, statusText: "OK" });
|
||||||
await checkEndpointResolution(req, res, next);
|
await checkEndpointResolution(req, res, next);
|
||||||
expect(res.status.calledWith(200)).to.be.true;
|
expect(res.status.calledWith(200)).to.be.true;
|
||||||
expect(
|
expect(
|
||||||
res.json.calledWith({
|
res.json.calledWith({
|
||||||
success: true,
|
success: true,
|
||||||
|
code: 200,
|
||||||
|
statusText: "OK",
|
||||||
msg: "URL resolved successfully",
|
msg: "URL resolved successfully",
|
||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
expect(next.called).to.be.false;
|
expect(next.called).to.be.false;
|
||||||
});
|
});
|
||||||
it("should return an error if DNS resolution fails", async () => {
|
it("should return an error if endpoint resolution fails", async () => {
|
||||||
const dnsError = new Error("DNS resolution failed");
|
const axiosError = new Error("resolution failed");
|
||||||
dnsError.code = "ENOTFOUND";
|
axiosError.code = "ENOTFOUND";
|
||||||
dnsResolveStub.callsFake((hostname, callback) => callback(dnsError));
|
axiosGetStub.rejects(axiosError);
|
||||||
await checkEndpointResolution(req, res, next);
|
await checkEndpointResolution(req, res, next);
|
||||||
expect(next.calledOnce).to.be.true;
|
expect(next.calledOnce).to.be.true;
|
||||||
const errorPassedToNext = next.getCall(0).args[0];
|
const errorPassedToNext = next.getCall(0).args[0];
|
||||||
expect(errorPassedToNext).to.be.an.instanceOf(Error);
|
expect(errorPassedToNext).to.be.an.instanceOf(Error);
|
||||||
expect(errorPassedToNext.message).to.include("DNS resolution failed");
|
expect(errorPassedToNext.message).to.include("resolution failed");
|
||||||
expect(errorPassedToNext.code).to.equal("ENOTFOUND");
|
expect(errorPassedToNext.code).to.equal("ENOTFOUND");
|
||||||
expect(errorPassedToNext.status).to.equal(500);
|
expect(errorPassedToNext.status).to.equal(500);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user