Refactor common url check code
Project: http://git-wip-us.apache.org/repos/asf/couchdb-nmo/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-nmo/commit/308a4ada Tree: http://git-wip-us.apache.org/repos/asf/couchdb-nmo/tree/308a4ada Diff: http://git-wip-us.apache.org/repos/asf/couchdb-nmo/diff/308a4ada Branch: refs/heads/master Commit: 308a4ada0cc200ea7226fa09c4c8a8966b260ac4 Parents: 320105f Author: Garren Smith <[email protected]> Authored: Thu Sep 10 10:18:55 2015 +0200 Committer: Garren Smith <[email protected]> Committed: Thu Sep 10 13:36:32 2015 +0200 ---------------------------------------------------------------------- src/couch-config.js | 21 ++++++++++----------- src/isonline.js | 9 ++------- src/utils.js | 10 ++++++++++ test/utils.js | 17 +++++++++++++++++ 4 files changed, 39 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/308a4ada/src/couch-config.js ---------------------------------------------------------------------- diff --git a/src/couch-config.js b/src/couch-config.js index 4bc2733..4012f12 100644 --- a/src/couch-config.js +++ b/src/couch-config.js @@ -10,7 +10,14 @@ export function cli (cmd, cluster, section, key, value) { return new Promise((resolve, reject) => { if (!cmd || !cluster || !exports[cmd]) { - const err = new Error('Usage: nmo couch-config add/set <cluster> ...'); + const msg = [ + 'Usage:', + '', + 'nmo couch-config get <cluster> [<section>] [--json]', + 'nmo config-config set <cluster> <section>, <key>, <value>' + ].join('\n'); + + const err = new Error(msg); err.type = 'EUSAGE'; return reject(err); } @@ -88,11 +95,7 @@ export function set(cluster, nodes, section, key, value) { export function setConfig (node, url, value) { return new Promise((resolve, reject) => { - let er = utils.checkUrl(url); - - if (!er && !/^(http:|https:)/.test(url)) { - er = new Error('invalid protocol, must be https or http'); - } + let er = utils.validUrl(url); if (er) { er.type = 'EUSAGE'; @@ -134,11 +137,7 @@ export function buildConfigUrl (node, url, section, key) { export function getConfig (node, url) { return new Promise((resolve, reject) => { - let er = utils.checkUrl(url); - - if (!er && !/^(http:|https:)/.test(url)) { - er = new Error('invalid protocol, must be https or http'); - } + let er = utils.validUrl(url); if (er) { er.type = 'EUSAGE'; http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/308a4ada/src/isonline.js ---------------------------------------------------------------------- diff --git a/src/isonline.js b/src/isonline.js index a3a88aa..5fbce1d 100644 --- a/src/isonline.js +++ b/src/isonline.js @@ -73,11 +73,7 @@ function isonline (...args) { function isNodeOnline (url) { return new Promise((resolve, reject) => { - let er = utils.checkUrl(url); - - if (!er && !/^(http:|https:)/.test(url)) { - er = new Error('invalid protocol, must be https or http'); - } + const er = utils.validUrl(url); if (er) { er.type = 'EUSAGE'; @@ -87,8 +83,7 @@ function isNodeOnline (url) { log.http('request', 'GET', cleanedUrl); Wreck.get(url, (err, res, payload) => { - if (err && (err.code === 'ECONNREFUSED' - || err.code === 'ENOTFOUND')) { + if (err && (err.code === 'ECONNREFUSED' || err.code === 'ENOTFOUND')) { return resolve({[url]: false}); } http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/308a4ada/src/utils.js ---------------------------------------------------------------------- diff --git a/src/utils.js b/src/utils.js index 1c02195..885dedb 100644 --- a/src/utils.js +++ b/src/utils.js @@ -8,6 +8,16 @@ import Promise from 'bluebird'; import log from 'npmlog'; import url from 'url'; +export function validUrl (url) { + let er = checkUrl(url); + + if (!er && !/^(http:|https:)/.test(url)) { + er = new Error('invalid protocol, must be https or http'); + } + + return er; +} + export function isUri (url) { return !!checkUri(url); } http://git-wip-us.apache.org/repos/asf/couchdb-nmo/blob/308a4ada/test/utils.js ---------------------------------------------------------------------- diff --git a/test/utils.js b/test/utils.js index 41a31ea..8770e64 100644 --- a/test/utils.js +++ b/test/utils.js @@ -7,6 +7,23 @@ export const lab = Lab.script(); import * as utils from '../src/utils.js'; import * as common from './common.js'; +lab.experiment('utils: validUrl', () => { + + lab.test('checks protocol of url', done => { + const err = utils.validUrl('ftp://wrong.com'); + + assert.ok(/invalid protocol/.test(err.message)); + done(); + }); + + lab.test('returns null if url is valid', done => { + const err = utils.validUrl('http://good.com'); + + assert.ok(err === null); + done(); + }); + +}); lab.experiment('utils: uri', () => {
