Skip to content

Commit

Permalink
feat(index): support returning a pingdom status report on GET requests
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff committed Jun 16, 2019
1 parent a8f99c8 commit 559d373
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
45 changes: 38 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,46 @@
* governing permissions and limitations under the License.
*/

async function report() {
const start = Date.now();

return {
statusCode: 200,
headers: {
'Content-Type': 'application/xml',
},
body: `<pingdom_http_custom_check>
<status>OK</status>
<response_time>${Math.abs(Date.now() - start)}</response_time>
</pingdom_http_custom_check>`,
};
}

function wrap(func, checks) {
return (params) => {
// eslint-disable-next-line no-underscore-dangle
if (params.__ow_methd === 'get') {
return report(checks);
}
return func(params);
};
}

/**
* This is the main function
* @param {string} name name of the person to greet
* @returns {object} a greeting
* @param {object|function} paramsorfunction a params object (if called as an OpenWhisk action)
* or a function to wrap.
* @param {object} checks a map of checks to perfom. Each key is a name of the check,
* each value a URL to ping
* @returns {object|function} a status report for Pingdom or a wrapped function
*/
function main({ name = 'world' } = {}) {
return {
body: `Hello, ${name}.`,
};
function main(paramsorfunction, checks = {}) {
if (typeof paramsorfunction === 'function') {
return wrap(paramsorfunction, checks);
} else if (typeof paramsorfunction === 'object') {
return report(paramsorfunction);
}
throw new Error('Invalid Arguments: expected function or object');
}

module.exports = { main };
module.exports = { main, wrap };
14 changes: 11 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@
'use strict';

const assert = require('assert');
const { AssertionError } = require('assert');
const index = require('../src/index.js').main;

describe('Index Tests', () => {
it('index function is present', async () => {
const result = await index({});
assert.deepEqual(result, { body: 'Hello, world.' });
assert.deepEqual(result.statusCode, 200);
});

it('index function returns an object', async () => {
const result = await index();
assert.equal(typeof result, 'object');
try {
await index();
assert.fail('this should never happen');
} catch (e) {
if (e instanceof AssertionError) {
throw e;
}
assert.equal(e.message, 'Invalid Arguments: expected function or object');
}
});
});

0 comments on commit 559d373

Please sign in to comment.