Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
Release 4.2.0. 🌿 Added HealthCheckRoute.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff committed Sep 15, 2017
1 parent d35001e commit 8304418
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "giftbit-cassava-routes",
"version": "4.1.1",
"version": "4.2.0",
"description": "Private Giftbit routes for use with Cassava.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
60 changes: 60 additions & 0 deletions src/HealthCheckRoute.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as cassava from "cassava";
import * as chai from "chai";
import {HealthCheckRoute} from "./HealthCheckRoute";

describe("HealthCheckRoute", () => {
it("responds with 200 with no checks", async () => {
const router = new cassava.Router();
router.route(new HealthCheckRoute("/healthCheck"));

const resp = await cassava.testing.testRouter(router, cassava.testing.createTestProxyEvent("/healthCheck", "GET"));

chai.assert.isObject(resp);
chai.assert.equal(resp.statusCode, 200, JSON.stringify(resp));
chai.assert.deepEqual(resp.body, JSON.stringify({}), JSON.stringify(resp));
});

it("responds with 200 for passing checks", async () => {
const router = new cassava.Router();
router.route(new HealthCheckRoute("/healthCheck", {
hold: () => Promise.resolve("up"),
wait: () => Promise.resolve("a minute")
}));

const resp = await cassava.testing.testRouter(router, cassava.testing.createTestProxyEvent("/healthCheck", "GET"));

chai.assert.isObject(resp);
chai.assert.equal(resp.statusCode, 200, JSON.stringify(resp));
chai.assert.deepEqual<any>(resp.body, JSON.stringify({hold: "up", wait: "a minute"}), JSON.stringify(resp));
});

it("responds with 500 for a failing check", async () => {
const router = new cassava.Router();
router.route(new HealthCheckRoute("/healthCheck", {
"let": () => Promise.resolve("me"),
clear: () => Promise.reject(new Error("cannot clear throat"))
}));

const resp = await cassava.testing.testRouter(router, cassava.testing.createTestProxyEvent("/healthCheck", "GET"));

chai.assert.isObject(resp);
chai.assert.equal(resp.statusCode, 500, JSON.stringify(resp));
chai.assert.deepEqual<any>(resp.body, JSON.stringify({"let": "me", clear: "Error: cannot clear throat"}), JSON.stringify(resp));
});

it("does not match other paths", async () => {
const router = new cassava.Router();
router.route(new HealthCheckRoute("/healthCheck"));
router.route(/.*/).handler(async res => {
return {
statusCode: 404,
body: {}
}
});

const resp = await cassava.testing.testRouter(router, cassava.testing.createTestProxyEvent("/butts", "GET"));

chai.assert.isObject(resp);
chai.assert.notEqual(resp.statusCode, 200, JSON.stringify(resp));
});
});
37 changes: 37 additions & 0 deletions src/HealthCheckRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as cassava from "cassava";

export class HealthCheckRoute implements cassava.routes.Route {

/**
* @param path Exact path to respond on.
* @param checks map of check name to function that returns a Promise of check result
*/
constructor(private readonly path: string = "/", private readonly checks: {[key: string]: () => Promise<string>} = {}) {}

matches(evt: cassava.RouterEvent): boolean {
return evt.path === this.path;
}

async handle(evt: cassava.RouterEvent): Promise<cassava.RouterResponse> {
const checkPromises: {[key: string]: Promise<string>} = {};
for (const key in this.checks) {
checkPromises[key] = this.checks[key]();
}

const checkResults: {[key: string]: string} = {};
let failure = false;
for (const key in checkPromises) {
try {
checkResults[key] = await checkPromises[key];
} catch (err) {
failure = true;
checkResults[key] = err + "";
}
}

return {
statusCode: failure ? 500 : 200,
body: checkResults
};
}
}

0 comments on commit 8304418

Please sign in to comment.