From 8e536c00b9b189c81789c5e4c0fbc941c5a01178 Mon Sep 17 00:00:00 2001 From: Chris Clark Date: Tue, 4 Aug 2015 11:51:27 -0700 Subject: [PATCH 1/2] Add test case for createPathFind and convert snake case function calls to camel case --- src/core/account.js | 2 +- src/core/pathfind.js | 2 +- src/core/remote.js | 18 ++++++++---------- src/core/transactionmanager.js | 2 +- test/remote-test.js | 27 +++++++++++++++++++++++++++ test/transaction-test.js | 2 +- 6 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/core/account.js b/src/core/account.js index 7d5c406a..3f262bcb 100644 --- a/src/core/account.js +++ b/src/core/account.js @@ -68,7 +68,7 @@ function Account(remote, account) { function attachAccount(request) { if (self._account.is_valid() && self._subs) { - request.add_account(self._account_id); + request.addAccount(self._account_id); } } diff --git a/src/core/pathfind.js b/src/core/pathfind.js index b65f70c3..681584fe 100644 --- a/src/core/pathfind.js +++ b/src/core/pathfind.js @@ -35,7 +35,7 @@ util.inherits(PathFind, EventEmitter); PathFind.prototype.create = function () { var self = this; - var req = this.remote.request_path_find_create( + var req = this.remote.requestPathFindCreate( this.src_account, this.dst_account, this.dst_amount, diff --git a/src/core/remote.js b/src/core/remote.js index abeda995..bb67588e 100644 --- a/src/core/remote.js +++ b/src/core/remote.js @@ -469,7 +469,7 @@ Remote.prototype.disconnect = function(callback_) { server.disconnect(); }); - this._set_state('offline'); + this._setState('offline'); return this; }; @@ -744,20 +744,18 @@ Remote.prototype.getServer = function() { * @param {Request} request */ -Remote.prototype.request = function(request_) { - let request = request_; - +Remote.prototype.request = function(request) { if (typeof request === 'string') { - if (!/^request_/.test(request)) { - request = 'request_' + request; - } + const prefix = /^request_/.test(request) ? '' : 'request_'; + const requestName = prefix + request; + const methodName = requestName.replace(/(\_\w)/g, m => m[1].toUpperCase()); - if (typeof this[request] === 'function') { + if (typeof this[methodName] === 'function') { const args = _.slice(arguments, 1); - return this[request].apply(this, args); + return this[methodName].apply(this, args); } - throw new Error('Command does not exist: ' + request); + throw new Error('Command does not exist: ' + requestName); } if (!(request instanceof Request)) { diff --git a/src/core/transactionmanager.js b/src/core/transactionmanager.js index f00f3e25..e15a0af2 100644 --- a/src/core/transactionmanager.js +++ b/src/core/transactionmanager.js @@ -524,7 +524,7 @@ TransactionManager.prototype._prepareRequest = function(tx) { tx.sign(); const serialized = tx.serialize(); - submitRequest.tx_blob(serialized.to_hex()); + submitRequest.txBlob(serialized.to_hex()); const hash = tx.hash(null, null, serialized); tx.addId(hash); diff --git a/test/remote-test.js b/test/remote-test.js index 47e93fd2..f671d5b0 100644 --- a/test/remote-test.js +++ b/test/remote-test.js @@ -36,6 +36,12 @@ const TX_JSON = { } }; +function makeServer(url) { + const server = new Server(new process.EventEmitter(), url); + server._connected = true; + return server; +} + describe('Remote', function() { const initialLogEngine = Log.getEngine(); @@ -1941,6 +1947,27 @@ describe('Remote', function() { }); }); + it('createPathFind', function() { + const servers = [ + makeServer('wss://localhost:5006'), + makeServer('wss://localhost:5007') + ]; + + remote._servers = servers; + + const pathfind = remote.createPathFind({ + src_account: 'rGr9PjmVe7MqEXTSbd3njhgJc2s5vpHV54', + dst_account: 'rwxBjBC9fPzyQ9GgPZw6YYLNeRTSx5c2W6', + dst_amount: '1/USD/rGr9PjmVe7MqEXTSbd3njhgJc2s5vpHV54', + src_currencies: [{ + currency: 'BTC', issuer: 'rwxBjBC9fPzyQ9GgPZw6YYLNeRTSx5c2W6' + }] + }); + + // TODO: setup a mock server to provide a response + pathfind.on('update', message => console.log(message)); + }); + it('Construct path_find create request', function() { const request = remote.requestPathFindCreate({ src_account: 'rGr9PjmVe7MqEXTSbd3njhgJc2s5vpHV54', diff --git a/test/transaction-test.js b/test/transaction-test.js index c05e58df..7d8ba611 100644 --- a/test/transaction-test.js +++ b/test/transaction-test.js @@ -345,7 +345,7 @@ describe('Transaction', function() { const dst = 'rGihwhaqU8g7ahwAvTq6iX5rvsfcbgZw6v'; transaction.payment(src, dst, '100'); - remote.set_secret(src, 'masterpassphrase'); + remote.setSecret(src, 'masterpassphrase'); assert(transaction.complete()); const json = transaction.serialize().to_json(); From 2cdb23f0dd5104f52f90ddb83e493258a1749b16 Mon Sep 17 00:00:00 2001 From: Chris Clark Date: Tue, 4 Aug 2015 15:46:34 -0700 Subject: [PATCH 2/2] Fix lint errors in pathfind.js --- src/core/pathfind.js | 48 ++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/core/pathfind.js b/src/core/pathfind.js index 681584fe..a1e994d1 100644 --- a/src/core/pathfind.js +++ b/src/core/pathfind.js @@ -1,7 +1,7 @@ -var EventEmitter = require('events').EventEmitter; -var util = require('util'); -var Amount = require('./amount').Amount; -var extend = require('extend'); +'use strict'; +const EventEmitter = require('events').EventEmitter; +const util = require('util'); +const Amount = require('./amount').Amount; /** * Represents a persistent path finding request. @@ -10,16 +10,18 @@ var extend = require('extend'); * find request is triggered it will supercede the existing one, making it emit * the 'end' and 'superceded' events. */ -function PathFind(remote, src_account, dst_account, dst_amount, src_currencies) { + +function PathFind(remote, src_account, dst_account, + dst_amount, src_currencies) { EventEmitter.call(this); this.remote = remote; - this.src_account = src_account; - this.dst_account = dst_account; - this.dst_amount = dst_amount; + this.src_account = src_account; + this.dst_account = dst_account; + this.dst_amount = dst_amount; this.src_currencies = src_currencies; -}; +} util.inherits(PathFind, EventEmitter); @@ -32,14 +34,16 @@ util.inherits(PathFind, EventEmitter); * so you should only have to call it if the path find was closed or superceded * and you wish to restart it. */ -PathFind.prototype.create = function () { - var self = this; - var req = this.remote.requestPathFindCreate( - this.src_account, - this.dst_account, - this.dst_amount, - this.src_currencies); +PathFind.prototype.create = function() { + const self = this; + + const req = this.remote.requestPathFindCreate({ + source_account: this.src_account, + destination_account: this.dst_account, + destination_amount: this.dst_amount, + source_currencies: this.src_currencies + }); req.once('error', function(err) { self.emit('error', err); @@ -54,16 +58,16 @@ PathFind.prototype.create = function () { req.broadcast().request(); }; -PathFind.prototype.close = function () { +PathFind.prototype.close = function() { this.remote.request_path_find_close().broadcast().request(); this.emit('end'); this.emit('close'); }; -PathFind.prototype.notify_update = function (message) { - var src_account = message.source_account; - var dst_account = message.destination_account; - var dst_amount = Amount.from_json(message.destination_amount); +PathFind.prototype.notify_update = function(message) { + const src_account = message.source_account; + const dst_account = message.destination_account; + const dst_amount = Amount.from_json(message.destination_amount); // Only pass the event along if this path find response matches what we were // looking for. @@ -74,7 +78,7 @@ PathFind.prototype.notify_update = function (message) { } }; -PathFind.prototype.notify_superceded = function () { +PathFind.prototype.notify_superceded = function() { // XXX If we're set to re-subscribe whenever we connect to a new server, then // we should cancel that behavior here. See PathFind#create.