From d192cc52705d0308f7554947e381007a55a48b03 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 7 Jul 2014 18:12:58 -0300 Subject: [PATCH 1/3] handle empty Insight responsed --- js/models/blockchain/Insight.js | 13 +++++++++---- test/test.blockchain.Insight.js | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/js/models/blockchain/Insight.js b/js/models/blockchain/Insight.js index 46afa817b..d98d5d1d9 100644 --- a/js/models/blockchain/Insight.js +++ b/js/models/blockchain/Insight.js @@ -2,6 +2,7 @@ var imports = require('soop').imports(); var bitcore = require('bitcore'); +var preconditions = require('preconditions').singleton(); var http; if (process.version) { @@ -78,8 +79,10 @@ Insight.prototype._getOptions = function(method, path, data) { }; Insight.prototype.getTransactions = function(addresses, cb) { - var self = this; + preconditions.shouldBeArray(addresses); + preconditions.shouldBeFunction(cb); + var self = this; if (!addresses || !addresses.length) return cb([]); var txids = []; @@ -88,9 +91,11 @@ Insight.prototype.getTransactions = function(addresses, cb) { _asyncForEach(addresses, function(addr, callback) { var options = self._getOptions('GET', '/api/addr/' + addr); self._request(options, function(err, res) { - var txids_tmp = res.transactions; - for (var i = 0; i < txids_tmp.length; i++) { - txids.push(txids_tmp[i]); + if (res.transactions) { + var txids_tmp = res.transactions; + for (var i = 0; i < txids_tmp.length; i++) { + txids.push(txids_tmp[i]); + } } callback(); }); diff --git a/test/test.blockchain.Insight.js b/test/test.blockchain.Insight.js index 2a54751a7..b96dc9cf8 100644 --- a/test/test.blockchain.Insight.js +++ b/test/test.blockchain.Insight.js @@ -126,6 +126,8 @@ describe('Insight model', function() { }); } + + it('#checkActivity for innactive addreses', function(done) { var w = new Insight(); w.getTransactions = function(addresses, cb) { @@ -176,4 +178,36 @@ describe('Insight model', function() { done(); }); }); + + + it('should handle getTransaction errors', function(done) { + + var http = require('http'); + var request = { + statusCode: 200 + }; + + request.on = function(event, cb) { + if (event === 'error') return; + if (event === 'data') return cb('{ "txid": "1234" }'); + return cb(); + }; + + var req = {}; + req.write = function() {}; + req.end = function() {}; + + sinon + .stub(http, 'request') + .returns(req) + .yields(request); + + var w = new Insight(); + w.getTransactions(['asdasd'], function(ret) { + ret.length.should.equal(0); + done(); + }); + }); + + }); From 1cb10219e9468172b60df97abe4d6b685d3c0657 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 7 Jul 2014 18:46:12 -0300 Subject: [PATCH 2/3] more handles --- js/models/blockchain/Insight.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/js/models/blockchain/Insight.js b/js/models/blockchain/Insight.js index d98d5d1d9..e509fc523 100644 --- a/js/models/blockchain/Insight.js +++ b/js/models/blockchain/Insight.js @@ -90,8 +90,9 @@ Insight.prototype.getTransactions = function(addresses, cb) { _asyncForEach(addresses, function(addr, callback) { var options = self._getOptions('GET', '/api/addr/' + addr); + self._request(options, function(err, res) { - if (res.transactions) { + if (res && res.transactions) { var txids_tmp = res.transactions; for (var i = 0; i < txids_tmp.length; i++) { txids.push(txids_tmp[i]); From b9b09d5f118236cbfbf76fcf5df415cc7e72c711 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 7 Jul 2014 19:33:59 -0300 Subject: [PATCH 3/3] add stub to _request --- test/test.blockchain.Insight.js | 35 ++++++++++++--------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/test/test.blockchain.Insight.js b/test/test.blockchain.Insight.js index b96dc9cf8..18a35471f 100644 --- a/test/test.blockchain.Insight.js +++ b/test/test.blockchain.Insight.js @@ -180,29 +180,20 @@ describe('Insight model', function() { }); - it('should handle getTransaction errors', function(done) { - - var http = require('http'); - var request = { - statusCode: 200 - }; - - request.on = function(event, cb) { - if (event === 'error') return; - if (event === 'data') return cb('{ "txid": "1234" }'); - return cb(); - }; - - var req = {}; - req.write = function() {}; - req.end = function() {}; - - sinon - .stub(http, 'request') - .returns(req) - .yields(request); - + it('should handle getTransaction null response', function(done) { var w = new Insight(); + w._request = sinon.stub().yields(); + w.getTransactions(['asdasd'], function(ret) { + ret.length.should.equal(0); + done(); + }); + }); + + + + it('should handle getTransaction empty response', function(done) { + var w = new Insight(); + w._request = sinon.stub().yields([]); w.getTransactions(['asdasd'], function(ret) { ret.length.should.equal(0); done();