Fix server url formatting

This commit is contained in:
wltsmrz
2014-05-30 00:50:06 -07:00
parent 9a349a3d55
commit 1c02166662
5 changed files with 209 additions and 69 deletions

View File

@@ -76,6 +76,7 @@ function Remote(opts, trace) {
EventEmitter.call(this); EventEmitter.call(this);
var self = this; var self = this;
var opts = opts || { };
this.trusted = Boolean(opts.trusted); this.trusted = Boolean(opts.trusted);
this.state = 'offline'; // 'online', 'offline' this.state = 'offline'; // 'online', 'offline'
@@ -142,18 +143,6 @@ function Remote(opts, trace) {
} }
}; };
// Fallback for previous API
if (!opts.hasOwnProperty('servers')) {
opts.servers = [
{
host: opts.websocket_ip,
port: opts.websocket_port,
secure: opts.websocket_ssl,
trusted: opts.trusted
}
];
}
if (typeof this._connection_offset !== 'number') { if (typeof this._connection_offset !== 'number') {
throw new TypeError('Remote "connection_offset" configuration is not a Number'); throw new TypeError('Remote "connection_offset" configuration is not a Number');
} }
@@ -194,16 +183,32 @@ function Remote(opts, trace) {
throw new TypeError('Remote "storage" configuration is not an Object'); throw new TypeError('Remote "storage" configuration is not an Object');
} }
opts.servers.forEach(function(server) { // Fallback for previous API
if (!opts.hasOwnProperty('servers') && opts.websocket_ip) {
opts.servers = [
{
host: opts.websocket_ip,
port: opts.websocket_port,
secure: opts.websocket_ssl,
trusted: opts.trusted
}
];
}
(opts.servers || []).forEach(function(server) {
var pool = Number(server.pool) || 1; var pool = Number(server.pool) || 1;
while (pool--) { self.addServer(server); }; while (pool--) {
self.addServer(server);
};
}); });
// This is used to remove Node EventEmitter warnings // This is used to remove Node EventEmitter warnings
var maxListeners = opts.maxListeners || opts.max_listeners || 0; var maxListeners = opts.maxListeners || opts.max_listeners || 0;
this._servers.concat(this).forEach(function(emitter) { this._servers.concat(this).forEach(function(emitter) {
emitter.setMaxListeners(maxListeners); if (emitter instanceof EventEmitter) {
emitter.setMaxListeners(maxListeners);
}
}); });
function listenerAdded(type, listener) { function listenerAdded(type, listener) {

View File

@@ -90,11 +90,8 @@ function Server(remote, opts) {
response: 1 response: 1
}; };
this._url = this._opts.url = url.format({ this._url = this._opts.url = (this._opts.secure ? 'wss://' : 'ws://')
hostname: self._opts.host, + this._opts.host + ':' + this._opts.port,
port: self._opts.port,
protocol: (self._opts.secure ? 'wss://' : 'ws://')
});
this.on('message', onMessage); this.on('message', onMessage);

View File

@@ -184,14 +184,12 @@ describe('Message', function(){
account: 'rKXCummUHnenhYudNb9UoJ4mGBR75vFcgz' account: 'rKXCummUHnenhYudNb9UoJ4mGBR75vFcgz'
}; };
Remote.prototype.addServer = function(){}; //Remote.prototype.addServer = function(){};
var test_remote = new Remote({}); var test_remote = new Remote();
assert.throws(function(){ assert.throws(function(){
Message.verifyHashSignature(data); Message.verifyHashSignature(data);
}, /(?=.*callback\ function).*/); }, /(?=.*callback\ function).*/);
}); });
it('should respond with an error if the hash is missing or invalid', function(done){ it('should respond with an error if the hash is missing or invalid', function(done){
@@ -202,8 +200,8 @@ describe('Message', function(){
account: 'rKXCummUHnenhYudNb9UoJ4mGBR75vFcgz' account: 'rKXCummUHnenhYudNb9UoJ4mGBR75vFcgz'
}; };
Remote.prototype.addServer = function(){}; //Remote.prototype.addServer = function(){};
var test_remote = new Remote({}); var test_remote = new Remote();
test_remote.state = 'online'; test_remote.state = 'online';
Message.verifyHashSignature(data, test_remote, function(err, valid){ Message.verifyHashSignature(data, test_remote, function(err, valid){
@@ -221,8 +219,8 @@ describe('Message', function(){
signature: 'AAAAHOUJQzG/7BO82fGNt1TNE+GGVXKuQQ0N2nTO+iJETE69PiHnaAkkOzovM177OosxbKjpt3KvwuJflgUB2YGvgjk=' signature: 'AAAAHOUJQzG/7BO82fGNt1TNE+GGVXKuQQ0N2nTO+iJETE69PiHnaAkkOzovM177OosxbKjpt3KvwuJflgUB2YGvgjk='
}; };
Remote.prototype.addServer = function(){}; //Remote.prototype.addServer = function(){};
var test_remote = new Remote({}); var test_remote = new Remote();
test_remote.state = 'online'; test_remote.state = 'online';
Message.verifyHashSignature(data, test_remote, function(err, valid){ Message.verifyHashSignature(data, test_remote, function(err, valid){
@@ -240,8 +238,8 @@ describe('Message', function(){
account: 'rKXCummUHnenhYudNb9UoJ4mGBR75vFcgz' account: 'rKXCummUHnenhYudNb9UoJ4mGBR75vFcgz'
}; };
Remote.prototype.addServer = function(){}; //Remote.prototype.addServer = function(){};
var test_remote = new Remote({}); var test_remote = new Remote();
test_remote.state = 'online'; test_remote.state = 'online';
Message.verifyHashSignature(data, test_remote, function(err, valid){ Message.verifyHashSignature(data, test_remote, function(err, valid){
@@ -260,8 +258,8 @@ describe('Message', function(){
signature: 'AAAAHMIPCQGLgdnpX1Ccv1wHb56H4NggxIM6U08Qkb9mUjN2Vn9pZ3CHvq1yWLBi6NqpW+7kedLnmfu4VG2+y43p4Xs=' signature: 'AAAAHMIPCQGLgdnpX1Ccv1wHb56H4NggxIM6U08Qkb9mUjN2Vn9pZ3CHvq1yWLBi6NqpW+7kedLnmfu4VG2+y43p4Xs='
}; };
Remote.prototype.addServer = function(){}; //Remote.prototype.addServer = function(){};
var test_remote = new Remote({}); var test_remote = new Remote();
test_remote.state = 'online'; test_remote.state = 'online';
test_remote.request_account_info = function(account, callback) { test_remote.request_account_info = function(account, callback) {
if (account === data.account) { if (account === data.account) {
@@ -296,8 +294,8 @@ describe('Message', function(){
signature: 'AAAAG+dB/rAjZ5m8eQ/opcqQOJsFbKxOu9jq9KrOAlNO4OdcBDXyCBlkZqS9Xr8oZI2uh0boVsgYOS3pOLJz+Dh3Otk=' signature: 'AAAAG+dB/rAjZ5m8eQ/opcqQOJsFbKxOu9jq9KrOAlNO4OdcBDXyCBlkZqS9Xr8oZI2uh0boVsgYOS3pOLJz+Dh3Otk='
}; };
Remote.prototype.addServer = function(){}; //Remote.prototype.addServer = function(){};
var test_remote = new Remote({}); var test_remote = new Remote();
test_remote.state = 'online'; test_remote.state = 'online';
test_remote.request_account_info = function(account, callback) { test_remote.request_account_info = function(account, callback) {
if (account === data.account) { if (account === data.account) {
@@ -324,4 +322,4 @@ describe('Message', function(){
}); });
}); });

View File

@@ -13,6 +13,7 @@ describe('Remote', function () {
trace : true, trace : true,
trusted: true, trusted: true,
local_signing: true, local_signing: true,
servers: [ servers: [
{ host: 's-west.ripple.com', port: 443, secure: true }, { host: 's-west.ripple.com', port: 443, secure: true },
{ host: 's-east.ripple.com', port: 443, secure: true } { host: 's-east.ripple.com', port: 443, secure: true }
@@ -24,26 +25,162 @@ describe('Remote', function () {
bridge: { bridge: {
out: { out: {
// 'bitcoin': 'localhost:3000' // 'bitcoin': 'localhost:3000'
// 'bitcoin': 'https://www.bitstamp.net/ripple/bridge/out/bitcoin/' // 'bitcoin': 'https://www.bitstamp.net/ripple/bridge/out/bitcoin/'
} }
}, },
}; };
}) })
describe('initialing a remote with options', function () {
it('should add a server for each specified', function (done) { describe('remote server initialization - url object', function() {
var remote = new Remote(options); it('should construct url', function (done) {
var remote = new Remote({
servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ],
});
assert(Array.isArray(remote._servers));
assert(remote._servers[0] instanceof Server);
assert.strictEqual(remote._servers[0]._url, 'wss://s-west.ripple.com:443');
done(); done();
}) })
});
})
describe('functions that return request objects', function () { describe('remote server initialization - url object - no secure property', function() {
it('should construct url', function (done) {
var remote = new Remote({
servers: [ { host: 's-west.ripple.com', port: 443 } ]
});
assert(Array.isArray(remote._servers));
assert(remote._servers[0] instanceof Server);
assert.strictEqual(remote._servers[0]._url, 'wss://s-west.ripple.com:443');
done();
})
});
describe('remote server initialization - url object - secure: false', function() {
it('should construct url', function (done) {
var remote = new Remote({
servers: [ { host: 's-west.ripple.com', port: 443, secure: false } ]
});
assert(Array.isArray(remote._servers));
assert(remote._servers[0] instanceof Server);
assert.strictEqual(remote._servers[0]._url, 'ws://s-west.ripple.com:443');
done();
})
});
describe('remote server initialization - url object - string port', function() {
it('should construct url', function (done) {
var remote = new Remote({
servers: [ { host: 's-west.ripple.com', port: '443', secure: true } ]
});
assert(Array.isArray(remote._servers));
assert(remote._servers[0] instanceof Server);
assert.strictEqual(remote._servers[0]._url, 'wss://s-west.ripple.com:443');
done();
})
});
describe('remote server initialization - url object - invalid host', function() {
it('should construct url', function (done) {
assert.throws(
function() {
var remote = new Remote({
servers: [ { host: '+', port: 443, secure: true } ]
});
}, Error);
done();
})
});
describe('remote server initialization - url object - invalid port', function() {
it('should construct url', function (done) {
assert.throws(
function() {
var remote = new Remote({
servers: [ { host: 's-west.ripple.com', port: null, secure: true } ]
});
}, TypeError);
done();
})
});
describe('remote server initialization - url object - port out of range', function() {
it('should construct url', function (done) {
assert.throws(
function() {
var remote = new Remote({
servers: [ { host: 's-west.ripple.com', port: 65537, secure: true } ]
});
}, Error);
done();
})
});
describe('remote server initialization - url string', function() {
it('should construct url', function (done) {
var remote = new Remote({
servers: [ 'wss://s-west.ripple.com:443' ]
});
assert(Array.isArray(remote._servers));
assert(remote._servers[0] instanceof Server);
assert.strictEqual(remote._servers[0]._url, 'wss://s-west.ripple.com:443');
done();
})
});
describe('remote server initialization - url string - ws://', function() {
it('should construct url', function (done) {
var remote = new Remote({
servers: [ 'ws://s-west.ripple.com:443' ]
});
assert(Array.isArray(remote._servers));
assert(remote._servers[0] instanceof Server);
assert.strictEqual(remote._servers[0]._url, 'ws://s-west.ripple.com:443');
done();
})
});
describe('remote server initialization - url string - invalid host', function() {
it('should construct url', function (done) {
assert.throws(
function() {
var remote = new Remote({
servers: [ 'ws://+:443' ]
});
}, Error
);
done();
})
});
describe('remote server initialization - url string - invalid port', function() {
it('should construct url', function (done) {
assert.throws(
function() {
var remote = new Remote({
servers: [ 'ws://s-west.ripple.com:null' ]
});
}, Error
);
done();
})
});
describe('remote server initialization - url string - port out of range', function() {
it('should construct url', function (done) {
assert.throws(
function() {
var remote = new Remote({
servers: [ 'ws://s-west.ripple.com:65537:' ]
});
}, Error
);
done();
})
});
describe('request constructors', function () {
beforeEach(function () { beforeEach(function () {
callback = function () {} callback = function () {}
remote = new Remote(options); remote = new Remote(options);
@@ -105,6 +242,7 @@ describe('Remote', function () {
}); });
}); });
}) })
describe('create remote and get pending transactions', function() { describe('create remote and get pending transactions', function() {
before(function() { before(function() {
tx = [{ tx = [{
@@ -121,26 +259,26 @@ describe('Remote', function () {
Paths : [ Paths : [
[ [
{ {
account : "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q", account : "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
currency : "USD", currency : "USD",
issuer : "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q", issuer : "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
type : 49, type : 49,
type_hex : "0000000000000031" type_hex : "0000000000000031"
}, },
{ {
currency : "LTC", currency : "LTC",
issuer : "rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX", issuer : "rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX",
type : 48, type : 48,
type_hex : "0000000000000030" type_hex : "0000000000000030"
}, },
{ {
account : "rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX", account : "rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX",
currency : "LTC", currency : "LTC",
issuer : "rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX", issuer : "rfYv1TXnwgDDK4WQNbFALykYuEBnrR4pDX",
type : 49, type : 49,
type_hex : "0000000000000031" type_hex : "0000000000000031"
} }
] ]
], ],
SendMax : { SendMax : {
currency : "USD", currency : "USD",
@@ -164,6 +302,7 @@ describe('Remote', function () {
} }
} }
}) })
it('should set transaction members correct ', function(done) { it('should set transaction members correct ', function(done) {
remote = new Remote(options); remote = new Remote(options);
remote.storage = database; remote.storage = database;
@@ -181,13 +320,13 @@ describe('Remote', function () {
assert.equal(this.submitIndex, tx[0].submitIndex); assert.equal(this.submitIndex, tx[0].submitIndex);
assert.equal(this.secret, tx[0].secret); assert.equal(this.secret, tx[0].secret);
done(); done();
}, },
parseJson: function(json) {} parseJson: function(json) {}
} }
} }
remote.getPendingTransactions(); remote.getPendingTransactions();
}) })
}) })
}) })

View File

@@ -121,6 +121,7 @@ describe('VaultClient', function() {
describe('#exists', function() { describe('#exists', function() {
it('should determine if a username exists on the domain', function(done) { it('should determine if a username exists on the domain', function(done) {
this.timeout(10000);
client.exists(exampleData.username, function(err, resp) { client.exists(exampleData.username, function(err, resp) {
assert.ifError(err); assert.ifError(err);
assert.strictEqual(typeof resp, 'boolean'); assert.strictEqual(typeof resp, 'boolean');