Allow to specify server url without explicit port number

This commit is contained in:
Ivan Tivonenko
2015-08-21 05:28:22 +03:00
parent e3b688d1dd
commit 23653f67f0
3 changed files with 23 additions and 7 deletions

View File

@@ -49,6 +49,14 @@ function Server(remote, opts_) {
'Server host is malformed, use "host" and "port" server configuration'); 'Server host is malformed, use "host" and "port" server configuration');
} }
if (typeof opts.secure !== 'boolean') {
opts.secure = true;
}
if (!Boolean(opts.port)) {
opts.port = opts.secure ? 443 : 80;
}
// We want to allow integer strings as valid port numbers for backward // We want to allow integer strings as valid port numbers for backward
// compatibility // compatibility
opts.port = Number(opts.port); opts.port = Number(opts.port);
@@ -60,10 +68,6 @@ function Server(remote, opts_) {
throw new TypeError('Server "port" must be an integer in range 1-65535'); throw new TypeError('Server "port" must be an integer in range 1-65535');
} }
if (typeof opts.secure !== 'boolean') {
opts.secure = true;
}
this._remote = remote; this._remote = remote;
this._opts = opts; this._opts = opts;
this._ws = undefined; this._ws = undefined;
@@ -126,7 +130,9 @@ function Server(remote, opts_) {
self._updateScore('ledgerclose', ledger); self._updateScore('ledgerclose', ledger);
}); });
/* eslint-disable no-unused-vars */
this.on('response_ping', function onPingResponse(message, request) { this.on('response_ping', function onPingResponse(message, request) {
/* eslint-enable no-unused-vars */
self._updateScore('response', request); self._updateScore('response', request);
}); });

View File

@@ -818,4 +818,9 @@ describe('RippleAPI - offline', function() {
assert(!api.isValidAddress(hex)); assert(!api.isValidAddress(hex));
}); });
/* eslint-disable no-unused-vars */
it('RippleAPI - implicit server port', function() {
const api = new RippleAPI({servers: ['wss://s1.ripple.com']});
});
/* eslint-enable no-unused-vars */
}); });

View File

@@ -106,7 +106,7 @@ describe('Remote', function() {
assert.throws( assert.throws(
function() { function() {
new Remote({ new Remote({
servers: [{host: 's-west.ripple.com', port: null, secure: true}] servers: [{host: 's-west.ripple.com', port: 'null', secure: true}]
}); });
}, TypeError); }, TypeError);
}); });
@@ -148,21 +148,26 @@ describe('Remote', function() {
); );
}); });
/*
"url" module used in server parses such urls with error, it return
null for port, so in this case default port will be used
it('Server initialization -- url string -- invalid port', function() { it('Server initialization -- url string -- invalid port', function() {
assert.throws( assert.throws(
function() { function() {
new Remote({ new Remote({
servers: ['ws://s-west.ripple.com:null'] servers: ['ws://s-west.ripple.com:invalid']
}); });
}, Error }, Error
); );
}); });
*/
it('Server initialization -- url string -- port out of range', function() { it('Server initialization -- url string -- port out of range', function() {
assert.throws( assert.throws(
function() { function() {
new Remote({ new Remote({
servers: ['ws://s-west.ripple.com:65537:'] servers: ['ws://s-west.ripple.com:65537']
}); });
}, Error }, Error
); );