This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release 4.2.0. 🌿 Added HealthCheckRoute.
- Loading branch information
jeff
committed
Sep 15, 2017
1 parent
d35001e
commit 8304418
Showing
3 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
} |