-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
271 additions
and
36 deletions.
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
File renamed without changes.
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,11 @@ | ||
const http = require('http'); | ||
const healthcheck = require('../'); | ||
|
||
http.createServer( (request, response) => { | ||
const check = healthcheck(); | ||
const isHealthCheckCall = check.http(request, response); | ||
if (isHealthCheckCall) return; | ||
|
||
response.end("HELLO! This is pretty amaziiiiing"); | ||
}).listen(3535); | ||
console.log('Server running on port 3535'); |
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
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
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,78 @@ | ||
const test = require('blue-tape'); | ||
const nf = require('node-fetch'); | ||
const log = require('metalogger')(); | ||
const fakepromise = require('fakepromise'); | ||
|
||
test('Failing Express Health Check', async t => { | ||
const brokenserver = getServer(); | ||
try { | ||
const util = require('../support/util'); | ||
const baseuri = util.serverUri(brokenserver); | ||
const uri = `${baseuri}/health`; | ||
|
||
const res = await nf(`${baseuri}/health`); | ||
t.equal(res.status, 200, 'correct http status code'); | ||
const response = await res.json(); | ||
|
||
t.same(response.status, 'warn', | ||
'correct status'); | ||
t.same(response.details["backend:something"].metricUnit, 'warnps', | ||
'correct payload'); | ||
|
||
} catch (err) { | ||
t.fail(err); | ||
} | ||
brokenserver.close(); | ||
|
||
//t.end(); // async tests dont need t.end() because blue-tape. | ||
}); | ||
|
||
function getServer() { | ||
const express = require('express'); | ||
const app = express(); | ||
const healthcheck = require('../../lib/health')(); | ||
|
||
healthcheck.addCheck('backend', 'healthy', async() => { | ||
|
||
const status = { | ||
status : 'pass', | ||
metricValue: 'A++', | ||
metricUnit: "grade" | ||
}; | ||
|
||
const delayedResponse = await fakepromise.promise(50, status); | ||
return delayedResponse; | ||
}); | ||
|
||
healthcheck.addCheck('backend', 'something', async() => { | ||
const status = { | ||
status : 'warn', | ||
unusualProp : false, | ||
metricValue: 17, | ||
metricUnit: "warnps" | ||
}; | ||
|
||
const delayedResponse = await fakepromise.promise(50, status); | ||
return delayedResponse; | ||
}); | ||
app.use(healthcheck.express()); | ||
|
||
const server = app.listen(0, function () { | ||
const port = server.address().port; | ||
//log.info(`Test server listening at port ${port} \n`); | ||
}); | ||
|
||
return server; | ||
} | ||
|
||
/* | ||
let res = await request('http://api.froyo.io/names?gender=f', {headers}); | ||
//console.log(res); | ||
console.log(res.ok); | ||
console.log(res.status); | ||
console.log(res.statusText); | ||
console.log(res.headers.raw()); | ||
console.log(res.headers.get('content-type')); | ||
console.log(await res.json()); | ||
*/ |
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,70 @@ | ||
const test = require('blue-tape'); | ||
const nf = require('node-fetch'); | ||
|
||
test('Basic Healthy No-Frameworks Health Check', async t => { | ||
|
||
// avoid problems if this env var is already set from wherever test was run | ||
process.env.NODE_HEALTH_ENDPOINT_PATH = ""; | ||
|
||
const server = getServer(); | ||
const util = require('../support/util'); | ||
const baseuri = util.serverUri(server); | ||
|
||
try { | ||
const res = await nf(`${baseuri}/`); | ||
t.equal(res.status, 200, 'proper http status code for /'); | ||
t.same(await res.text(), 'Hello, World!', | ||
'proper content for /'); | ||
|
||
const res2 = await nf(`${baseuri}/health`); | ||
t.equal(res2.status, 200, 'proper http status code for /health'); | ||
t.equal(res2.headers.get('content-type'), | ||
'application/health+json; charset=utf-8', | ||
'proper content type for health endpoint'); | ||
const response = await res2.json(); | ||
t.same(response.status, 'pass', | ||
'healthcheck endpoint status works'); | ||
t.same(response.details["backend:http-downstream"].metricUnit, 'pureseconds', | ||
'healthcheck endpoint details work'); | ||
|
||
} catch (err) { | ||
t.fail(err); | ||
} | ||
|
||
server.close(); | ||
|
||
}); | ||
|
||
function getServer() { | ||
const http = require('http'); | ||
const healthcheck = require('../../'); | ||
|
||
const server = http.createServer( (request, response) => { | ||
const check = healthcheck(); | ||
|
||
check.addCheck('backend', 'http-downstream', async() => { | ||
const status = { | ||
status : 'pass', | ||
metricValue: 17, | ||
metricUnit: "pureseconds" | ||
}; | ||
|
||
const fakepromise = require('fakepromise'); | ||
const delayedResponse = await fakepromise.promise(50, status); | ||
return delayedResponse; | ||
}); | ||
|
||
const isHealthCheckCall = check.http(request, response); | ||
if (isHealthCheckCall) return; | ||
|
||
response.end("Hello, World!"); | ||
}); | ||
|
||
server.listen(0, function(err) { | ||
if (err) console.error(err) && process.exit(1); | ||
const port = server.address().port; | ||
//console.log(`Test server listening at port ${port} \n`); | ||
}); | ||
|
||
return server; | ||
} |
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
Oops, something went wrong.