diff --git a/server/src/controllers/controllerUtils.ts b/server/src/controllers/controllerUtils.ts index 67dc71a70..0e765bae8 100755 --- a/server/src/controllers/controllerUtils.ts +++ b/server/src/controllers/controllerUtils.ts @@ -6,7 +6,8 @@ type SSLCheckerType = typeof sslChecker; export const fetchMonitorCertificate = async (checker: SSLCheckerType, monitor: Monitor): Promise => { const monitorUrl = new URL(monitor.url); const hostname = monitorUrl.hostname; - const cert = await checker(hostname); + const port = monitorUrl.port ? Number(monitorUrl.port) : undefined; + const cert = await checker(hostname, port ? { port } : undefined); if (cert?.validTo === null || cert?.validTo === undefined) { throw new Error("Certificate not found"); } diff --git a/server/test/unit/controllers/controllerUtils.test.ts b/server/test/unit/controllers/controllerUtils.test.ts new file mode 100644 index 000000000..c3ac92246 --- /dev/null +++ b/server/test/unit/controllers/controllerUtils.test.ts @@ -0,0 +1,34 @@ +import { describe, expect, it, jest } from "@jest/globals"; +import { fetchMonitorCertificate } from "../../../src/controllers/controllerUtils.ts"; +import type { Monitor } from "../../../src/types/monitor.ts"; + +const certificate = { + daysRemaining: 30, + valid: true, + validFrom: "2026-01-01T00:00:00.000Z", + validTo: "2026-02-01T00:00:00.000Z", + validFor: ["checkmate.example.org"], +}; + +const makeMonitor = (url: string) => + ({ + url, + }) as Monitor; + +describe("fetchMonitorCertificate", () => { + it("passes a custom HTTPS port to the SSL checker", async () => { + const checker = jest.fn().mockResolvedValue(certificate); + + await fetchMonitorCertificate(checker, makeMonitor("https://checkmate.example.org:54321/status")); + + expect(checker).toHaveBeenCalledWith("checkmate.example.org", { port: 54321 }); + }); + + it("uses the SSL checker's default port when the URL does not include one", async () => { + const checker = jest.fn().mockResolvedValue(certificate); + + await fetchMonitorCertificate(checker, makeMonitor("https://checkmate.example.org/status")); + + expect(checker).toHaveBeenCalledWith("checkmate.example.org", undefined); + }); +});