mirror of https://github.com/BTCPrivate/copay.git
Merge pull request #708 from matiu/bug/fix-post-in-node
fixes POST request in node
This commit is contained in:
commit
94b72655ae
|
@ -3,6 +3,11 @@
|
|||
var imports = require('soop').imports();
|
||||
var bitcore = require('bitcore');
|
||||
|
||||
var http;
|
||||
if (process.version) {
|
||||
http = require('http');
|
||||
};
|
||||
|
||||
function Insight(opts) {
|
||||
opts = opts || {};
|
||||
this.host = opts.host || 'localhost';
|
||||
|
@ -130,7 +135,6 @@ Insight.prototype.getUnspent = function(addresses, cb) {
|
|||
return cb(err);
|
||||
}
|
||||
|
||||
|
||||
if (res && res.length > 0) {
|
||||
all = all.concat(res);
|
||||
}
|
||||
|
@ -159,6 +163,7 @@ Insight.prototype.sendRawTransaction = function(rawtx, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
|
||||
Insight.prototype.checkActivity = function(addresses, cb) {
|
||||
if (!addresses) throw new Error('address must be set');
|
||||
|
||||
|
@ -180,11 +185,52 @@ Insight.prototype.checkActivity = function(addresses, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
Insight.prototype._request = function(options, callback) {
|
||||
Insight.prototype._requestNode = function(options, callback) {
|
||||
if (options.method === 'POST') {
|
||||
options.headers = {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Content-Length': options.data.length,
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
var self = this;
|
||||
if (typeof process === 'undefined' || !process.version) {
|
||||
var req = http.request(options, function(response) {
|
||||
var ret, errTxt, e;
|
||||
if (response.statusCode == 200 || response.statusCode === 304) {
|
||||
response.on('data', function(chunk) {
|
||||
try {
|
||||
ret = JSON.parse(chunk);
|
||||
} catch (e2) {
|
||||
errTxt = 'CRITICAL: Wrong response from insight' + e2;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
errTxt = "INSIGHT ERROR:" + response.statusCode;
|
||||
console.log(errTxt);
|
||||
e = new Error(errTxt);
|
||||
return callback(e);
|
||||
}
|
||||
response.on('end', function() {
|
||||
if (errTxt) {
|
||||
console.log("INSIGHT ERROR:" + errTxt);
|
||||
e = new Error(errTxt);
|
||||
}
|
||||
return callback(e, ret);
|
||||
});
|
||||
response.on('error', function(e) {
|
||||
|
||||
console.log('[Insight.js.201]'); //TODO
|
||||
return callback(e, ret);
|
||||
});
|
||||
});
|
||||
|
||||
if (options.data) {
|
||||
req.write(options.data);
|
||||
}
|
||||
req.end();
|
||||
};
|
||||
|
||||
Insight.prototype._requestBrowser = function(options, callback) {
|
||||
var request = new XMLHttpRequest();
|
||||
|
||||
// TODO: Normalize URL
|
||||
|
@ -240,35 +286,13 @@ Insight.prototype._request = function(options, callback) {
|
|||
}
|
||||
|
||||
request.send(options.data || null);
|
||||
} else {
|
||||
var http = require('http');
|
||||
var req = http.request(options, function(response) {
|
||||
var ret;
|
||||
if (response.statusCode == 200 || response.status === 304) {
|
||||
response.on('data', function(chunk) {
|
||||
try {
|
||||
ret = JSON.parse(chunk);
|
||||
} catch (e) {
|
||||
return callback({
|
||||
message: 'Wrong response from insight'
|
||||
});
|
||||
}
|
||||
});
|
||||
response.on('end', function() {
|
||||
return callback(null, ret);
|
||||
});
|
||||
} else {
|
||||
return callback({
|
||||
message: 'Error ' + response.statusCode
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
if (options.data) {
|
||||
req.write(options.data);
|
||||
}
|
||||
|
||||
req.end();
|
||||
Insight.prototype._request = function(options, callback) {
|
||||
if (typeof process === 'undefined' || !process.version) {
|
||||
this._requestBrowser(options, callback);
|
||||
} else {
|
||||
this._requestNode(options, callback);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
var chai = chai || require('chai');
|
||||
var should = chai.should();
|
||||
var sinon = require('sinon');
|
||||
var bitcore = bitcore || require('bitcore');
|
||||
try {
|
||||
var copay = require('copay'); //browser
|
||||
|
@ -29,8 +30,7 @@ var addresses = [
|
|||
'2N9EdxU3co5XKTyj3yhFBeU3qw3EM1rrgzE'
|
||||
];
|
||||
|
||||
var unspent = [
|
||||
{
|
||||
var unspent = [{
|
||||
address: "2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb",
|
||||
txid: "d5597c6cf7f72507af63a4d5a2f9f84edb45fb42452cc8c514435b7a93158915",
|
||||
vout: 0,
|
||||
|
@ -38,8 +38,7 @@ var unspent = [
|
|||
scriptPubKey: "a914e54f125244a0bf91f9c5d861dc28343ccf19883d87",
|
||||
amount: 41,
|
||||
confirmations: 7007
|
||||
},
|
||||
{
|
||||
}, {
|
||||
address: "2N9D5bcCQ2bPWUDByQ6Qb5bMgMtgsk1rw3x",
|
||||
txid: "90d0e1f993fc41596e7b0a7a3be8ef65d606164e13ce538bd3f48136b60eff5a",
|
||||
vout: 0,
|
||||
|
@ -47,31 +46,86 @@ var unspent = [
|
|||
scriptPubKey: "a914af1a2d1a9c0fa172ed70bc1c50ea6b66994e9abf87",
|
||||
amount: 50,
|
||||
confirmations: 6728
|
||||
}
|
||||
];
|
||||
}];
|
||||
|
||||
var rawtx = '01000000010c2a03ed71ee18148e8c99c5ff66d5ffb75e5def46cdea2acc6f30103f33bfb5010000006a47304402207f960aeefdfad270dd77d1acca7af17d3a2e47e2059034ff5d6305cf63635e1d02202f061ee196cc4459cdecae6559beac696a9ecde9a17520849f319fa2a627e64f012103870465f9b4efb90b5d186a7a5eacd7081e601020dacd68d942e5918a56ed0bfcffffffff02a086010000000000ad532102a9495c64323cd8c3354dbf0b3400d830ee680da493acbccc3c2c356d1b20fabf21028233cf8bc6112ae2c36468bd447732c5586b52e1ba3284a2319cadfac6367f99210279fd856e5ed13ab6807e85ed7c0cd6f80613be042240fd731c43f5aba3dcae9821021380858a67a4f99eda52ce2d72c300911f9d3eb9d7a45102a2133f14f7b2dc14210215739b613ce42106a11ce433342c13c610bf68a1bc934f607ad7aeb4178e04cf55ae2044d200000000001976a9146917322f0010aaf7ec136a34b476dfc5eb7a331288ac00000000';
|
||||
|
||||
|
||||
describe('Insight model', function() {
|
||||
|
||||
|
||||
it('should create an instance', function() {
|
||||
var w = new Insight();
|
||||
should.exist(w);
|
||||
var i = new Insight();
|
||||
should.exist(i);
|
||||
});
|
||||
it.skip('should return array of unspent output', function(done) {
|
||||
var w = new Insight();
|
||||
w.listUnspent(addresses, function(a) {
|
||||
should.exist(a);
|
||||
|
||||
// Tests for Node
|
||||
if (process.version) {
|
||||
it('should return array of unspent output', function(done) {
|
||||
var i = new Insight();
|
||||
|
||||
|
||||
var http = require('http');
|
||||
var request = {
|
||||
statusCode: 200
|
||||
};
|
||||
|
||||
request.on = function(event, cb) {
|
||||
if (event === 'error') return;
|
||||
if (event === 'data') return cb(JSON.stringify(unspent));
|
||||
return cb();
|
||||
};
|
||||
|
||||
var req = {};
|
||||
req.write = function() {};
|
||||
req.end = function() {};
|
||||
|
||||
|
||||
sinon
|
||||
.stub(http, 'request')
|
||||
.returns(req)
|
||||
.yields(request);
|
||||
|
||||
i.getUnspent(['2MuD5LnZSViZZYwZbpVsagwrH8WWvCztdmV', '2NBSLoMvsHsf2Uv3LA17zV4beH6Gze6RovA'], function(e, ret) {
|
||||
should.not.exist(e);
|
||||
ret.should.deep.equal(unspent);
|
||||
http.request.restore();
|
||||
done();
|
||||
});
|
||||
});
|
||||
it.skip('should return txid', function (done) {
|
||||
var w = new Insight();
|
||||
w.sendRawTransaction(rawtx, function(a) {
|
||||
|
||||
it('should return txid', function(done) {
|
||||
var i = new Insight();
|
||||
|
||||
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);
|
||||
|
||||
i.sendRawTransaction(rawtx, function(a) {
|
||||
should.exist(a);
|
||||
a.should.equal('1234');
|
||||
http.request.restore();
|
||||
done();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
it('#checkActivity for innactive addreses', function(done) {
|
||||
var w = new Insight();
|
||||
w.getTransactions = function(addresses, cb) {
|
||||
|
@ -103,4 +157,3 @@ describe('Insight model', function() {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue