Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow ignoring host validation to be IP or localhost #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,20 @@ function Client() {
* @param {String} [url] - host URL
* @returns {Client|String} Client instance or host URL
*/
Client.prototype.host = function( url ) {
Client.prototype.host = function( url, options ) {
if ( !arguments.length ) {
return this._host;
}
if ( typeof url !== 'string' ) {
throw new TypeError( 'host()::invalid input argument. URL must be a string.' );
}
if ( !IP.test( url ) ) {
if ( !options ) {
options = {};
}
if ( options.validate == null ) {
options.validate = true;
}
if ( options.validate && !IP.test( url ) ) {
throw new Error( 'host()::invalid input argument. URL is not valid. Ensure valid IP or `localhost`.' );
}
this._host = url;
Expand Down
21 changes: 20 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe( 'opentsdb-client', function tests() {
}
});

it( 'should not allow an invalid host', function test() {
it( 'should not allow an invalid host by default', function test() {
var values = [
'badhost',
'1000.10.10.100'
Expand All @@ -103,6 +103,25 @@ describe( 'opentsdb-client', function tests() {
}
});

it( 'should skip host validation if explicitly asked by client', function test() {
var values = [
'badhost',
'1000.10.10.100'
];

for ( var i = 0; i < values.length; i++ ) {
setHost( values[i], { validate: false } )();
assert.strictEqual(client.host(), values[i]);
expect( setHost( values[i], { validate: true } ) ).to.throw( Error );
}

function setHost( value, options ) {
return function() {
client.host( value, options );
};
}
});

it( 'should set the default host URL to 127.0.0.1', function test() {
assert.strictEqual( client.host(), '127.0.0.1' );
});
Expand Down