Refactor code for check endpoint and update test cases accordingly
This commit is contained in:
1 parent
d1e65f53ba
commit
6715a8b500
2 files changed
+16
-23
No files matched your search
@@ -277,13 +277,8 @@ const createMonitor = async (req, res, next) => {
|
|||||||
const checkEndpointResolution = async (req, res, next) => {
|
const checkEndpointResolution = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
let { monitorURL } = req.query;
|
let { monitorURL } = req.query;
|
||||||
|
monitorURL = new URL(monitorURL);
|
||||||
// Remove the protocol (http:// or https://) if present
|
dns.resolve(monitorURL.hostname, (error) => {
|
||||||
monitorURL = monitorURL.replace(/^https?:\/\//, '');
|
|
||||||
// Remove the trailing slash if it exists
|
|
||||||
monitorURL = monitorURL.endsWith('/') ? monitorURL.slice(0, -1) : monitorURL;
|
|
||||||
|
|
||||||
dns.resolve(monitorURL, (error) => {
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
success: false,
|
success: false,
|
||||||
|
|||||||
@@ -478,7 +478,7 @@ describe("Monitor Controller - createMonitor", () => {
|
|||||||
describe('checkEndpointResolution', () => {
|
describe('checkEndpointResolution', () => {
|
||||||
let dnsResolveStub;
|
let dnsResolveStub;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
req = { query: { monitorURL: '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');
|
dnsResolveStub = sinon.stub(dns, 'resolve');
|
||||||
@@ -487,7 +487,7 @@ describe('checkEndpointResolution', () => {
|
|||||||
dnsResolveStub.restore();
|
dnsResolveStub.restore();
|
||||||
});
|
});
|
||||||
it('should resolve the URL successfully', async () => {
|
it('should resolve the URL successfully', async () => {
|
||||||
dnsResolveStub.callsFake((monitorURL, callback) => callback(null));
|
dnsResolveStub.callsFake((hostname, callback) => callback(null));
|
||||||
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(res.json.calledWith({
|
expect(res.json.calledWith({
|
||||||
@@ -498,7 +498,7 @@ describe('checkEndpointResolution', () => {
|
|||||||
});
|
});
|
||||||
it("should return a 400 error message if DNS resolution fails", async () => {
|
it("should return a 400 error message if DNS resolution fails", async () => {
|
||||||
const dnsError = new Error("DNS resolution failed");
|
const dnsError = new Error("DNS resolution failed");
|
||||||
dnsResolveStub.callsFake((monitorURL, 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(res.status.calledOnceWith(400)).to.be.true;
|
||||||
expect(res.json.calledOnceWith({
|
expect(res.json.calledOnceWith({
|
||||||
@@ -507,23 +507,21 @@ describe('checkEndpointResolution', () => {
|
|||||||
})).to.be.true;
|
})).to.be.true;
|
||||||
expect(next.notCalled).to.be.true;
|
expect(next.notCalled).to.be.true;
|
||||||
});
|
});
|
||||||
it("should remove the trailing slash and resolve the URL successfully", async () => {
|
it('should call next with an error for invalid monitorURL', async () => {
|
||||||
req.query.monitorURL = 'http://example.com/'; // URL with a trailing slash
|
req.query.monitorURL = 'invalid-url';
|
||||||
dnsResolveStub.callsFake((monitorURL, callback) => callback(null));
|
|
||||||
await checkEndpointResolution(req, res, next);
|
|
||||||
expect(res.status.calledWith(200)).to.be.true;
|
|
||||||
expect(res.json.calledWith({
|
|
||||||
success: true,
|
|
||||||
msg: 'URL resolved successfully',
|
|
||||||
})).to.be.true;
|
|
||||||
expect(next.called).to.be.false;
|
|
||||||
});
|
|
||||||
it("should call next with an error if an exception is thrown", async () => {
|
|
||||||
req.query = {}; // Missing monitorURL will cause an error
|
|
||||||
await checkEndpointResolution(req, res, next);
|
await checkEndpointResolution(req, res, next);
|
||||||
expect(next.calledOnce).to.be.true;
|
expect(next.calledOnce).to.be.true;
|
||||||
const error = next.getCall(0).args[0];
|
const error = next.getCall(0).args[0];
|
||||||
expect(error).to.be.an.instanceOf(Error);
|
expect(error).to.be.an.instanceOf(Error);
|
||||||
|
expect(error.message).to.include('Invalid URL');
|
||||||
|
});
|
||||||
|
it('should handle an edge case where monitorURL is not provided', async () => {
|
||||||
|
req.query = {};
|
||||||
|
await checkEndpointResolution(req, res, next);
|
||||||
|
expect(next.calledOnce).to.be.true;
|
||||||
|
const error = next.getCall(0).args[0];
|
||||||
|
expect(error).to.be.an.instanceOf(Error);
|
||||||
|
expect(error.message).to.include('Invalid URL');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user