From 7f80d152d3c298ca0c4d89a47efa985ce81d571c Mon Sep 17 00:00:00 2001 From: Asher Date: Tue, 16 Feb 2021 14:14:21 -0600 Subject: [PATCH] Add healthz tests --- test/health.test.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 test/health.test.ts diff --git a/test/health.test.ts b/test/health.test.ts new file mode 100644 index 000000000..4eae9c609 --- /dev/null +++ b/test/health.test.ts @@ -0,0 +1,40 @@ +import * as httpserver from "./httpserver" +import * as integration from "./integration" + +describe("health", () => { + let codeServer: httpserver.HttpServer | undefined + + afterEach(async () => { + if (codeServer) { + await codeServer.close() + codeServer = undefined + } + }) + + it("/healthz", async () => { + ;[, , codeServer] = await integration.setup(["--auth=none"], "") + const resp = await codeServer.fetch("/healthz") + expect(resp.status).toBe(200) + const json = await resp.json() + expect(json).toStrictEqual({ lastHeartbeat: 0, status: "expired" }) + }) + + it("/healthz (websocket)", async () => { + ;[, , codeServer] = await integration.setup(["--auth=none"], "") + const ws = codeServer.ws("/healthz") + const message = await new Promise((resolve, reject) => { + ws.on("error", console.error) + ws.on("message", (message) => { + try { + const j = JSON.parse(message.toString()) + resolve(j) + } catch (error) { + reject(error) + } + }) + ws.on("open", () => ws.send(JSON.stringify({ event: "health" }))) + }) + ws.terminate() + expect(message).toStrictEqual({ event: "health", status: "expired", lastHeartbeat: 0 }) + }) +})