describe('WebHook', function webHookSuite() { it('returns 200 OK for health endpoint', function test(done) { utils.sendWebHookRequest(webHookPort2, '/healthz').then(resp => { assert.equal(resp, 'OK'); return done(); }); }); it('returns 401 error if token is wrong', function test(done) { utils.sendWebHookMessage(webHookPort2, 'wrong-token').catch(resp => { assert.equal(resp.statusCode, 401); return done(); }); }); it('only accepts POST method', function test() { const methods = ['GET', 'PUT', 'DELETE', 'OPTIONS']; return Promise.each(methods, (method) => { return utils.sendWebHookMessage(webHookPort2, TOKEN, { method, }).then(() => { throw new Error(`expected error with webhook ${method} request`); }).catch(resp => { if (!resp.statusCode) throw resp; if (resp.statusCode !== 418) throw new Error(`unexpected error: ${resp.body}`); }); }); // Promise.each }); });
/** * retrieve destination configuration * * @param {string} destinationName * @param {string} destinationApiUrl * @param {string} accessToken - OAuth2.0 Bearer token ("client_credentials" grant type) * @returns {Promise<T | never>} */ async function getDestination(destinationName, destinationApiUrl, accessToken) { if (cfenv.getAppEnv().isLocal) { let object = { destinationConfiguration: { URL: destinationName } } return Promise.resolve(JSON.stringify(object)) } const options = { url: `${destinationApiUrl}/${destinationName}`, headers: { Authorization: `Bearer ${accessToken}` } } return rp(options).catch(err => { throw err // bubble-up }) }
/** * dispenserd.schedule(dispenserd.job_test, { * key: 'value' * }); */ schedule(job, options) { options.job = job; request .post({ url: 'http://' + sails.config.dispenserd.host + ':8282/schedule', body: JSON.stringify({ lane: options.lane || dispenserd.lane_main, priority: options.priority || 10000, message: JSON.stringify(options) }), resolveWithFullResponse: true, simple: false }) .catch(err => { console.log('job failed to schedule'); }); }
promiseRetry( {retries: 10, factor: 2, randomize: true}, (retry, number) => { console.log(`Attempt number ${number}.`); return request({ method: 'POST', uri: `https://${region}-${project}.cloudfunctions.net` + '/httpFunction', body: {foo: 'bar'}, json: true }) .catch(retry); }) .then(res => { console.log(`Success! ${res}`); }) .catch(err => { console.log(`Failure. ${err}`); });