add tests

This commit is contained in:
Fabian Vogelsteller 2015-07-15 17:49:55 +02:00
parent c776c8b61e
commit ebcff900f7
8 changed files with 176 additions and 24 deletions

51
dist/web3-light.js vendored
View File

@ -1682,6 +1682,8 @@ module.exports = AllSolidityEvents;
*/
var RequestManager = require('./requestmanager');
var Jsonrpc = require('./jsonrpc');
var errors = require('./errors');
var Batch = function () {
this.requests = [];
@ -1708,11 +1710,14 @@ Batch.prototype.execute = function () {
results = results || [];
requests.map(function (request, index) {
return results[index] || {};
}).map(function (result, index) {
return requests[index].format ? requests[index].format(result.result) : result.result;
}).forEach(function (result, index) {
if (requests[index].callback) {
requests[index].callback(err, result);
if (!Jsonrpc.getInstance().isValidResponse(result)) {
return requests[index].callback(errors.InvalidResponse(result));
}
requests[index].callback(null, (requests[index].format ? requests[index].format(result.result) : result.result));
}
});
});
@ -1721,7 +1726,7 @@ Batch.prototype.execute = function () {
module.exports = Batch;
},{"./requestmanager":28}],12:[function(require,module,exports){
},{"./errors":14,"./jsonrpc":22,"./requestmanager":28}],12:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3882,6 +3887,7 @@ module.exports = {
*/
var RequestManager = require('./requestmanager');
var utils = require('../utils/utils');
var Property = function (options) {
this.name = options.name;
@ -3913,6 +3919,19 @@ Property.prototype.formatOutput = function (result) {
return this.outputFormatter && result !== null ? this.outputFormatter(result) : result;
};
/**
* Should be used to extract callback from array of arguments. Modifies input param
*
* @method extractCallback
* @param {Array} arguments
* @return {Function|Null} callback, if exists
*/
Property.prototype.extractCallback = function (args) {
if (utils.isFunction(args[args.length - 1])) {
return args.pop(); // modify the args array!
}
};
/**
* Should attach function to method
*
@ -3939,7 +3958,10 @@ Property.prototype.attachToObject = function (obj) {
return prefix + name.charAt(0).toUpperCase() + name.slice(1);
};
obj[toAsyncName('get', name)] = this.getAsync.bind(this);
var func = this.getAsync.bind(this);
func.request = this.request.bind(this);
obj[toAsyncName('get', name)] = func;
};
/**
@ -3972,10 +3994,27 @@ Property.prototype.getAsync = function (callback) {
});
};
/**
* Should be called to create pure JSONRPC request which can be used in batch request
*
* @method request
* @param {...} params
* @return {Object} jsonrpc request
*/
Property.prototype.request = function () {
var payload = {
method: this.getter,
params: [],
callback: this.extractCallback(Array.prototype.slice.call(arguments))
};
payload.format = this.formatOutput.bind(this);
return payload;
};
module.exports = Property;
},{"./requestmanager":28}],27:[function(require,module,exports){
},{"../utils/utils":7,"./requestmanager":28}],27:[function(require,module,exports){
/*
This file is part of ethereum.js.

File diff suppressed because one or more lines are too long

51
dist/web3.js vendored
View File

@ -1682,6 +1682,8 @@ module.exports = AllSolidityEvents;
*/
var RequestManager = require('./requestmanager');
var Jsonrpc = require('./jsonrpc');
var errors = require('./errors');
var Batch = function () {
this.requests = [];
@ -1708,11 +1710,14 @@ Batch.prototype.execute = function () {
results = results || [];
requests.map(function (request, index) {
return results[index] || {};
}).map(function (result, index) {
return requests[index].format ? requests[index].format(result.result) : result.result;
}).forEach(function (result, index) {
if (requests[index].callback) {
requests[index].callback(err, result);
if (!Jsonrpc.getInstance().isValidResponse(result)) {
return requests[index].callback(errors.InvalidResponse(result));
}
requests[index].callback(null, (requests[index].format ? requests[index].format(result.result) : result.result));
}
});
});
@ -1721,7 +1726,7 @@ Batch.prototype.execute = function () {
module.exports = Batch;
},{"./requestmanager":28}],12:[function(require,module,exports){
},{"./errors":14,"./jsonrpc":22,"./requestmanager":28}],12:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -3882,6 +3887,7 @@ module.exports = {
*/
var RequestManager = require('./requestmanager');
var utils = require('../utils/utils');
var Property = function (options) {
this.name = options.name;
@ -3913,6 +3919,19 @@ Property.prototype.formatOutput = function (result) {
return this.outputFormatter && result !== null ? this.outputFormatter(result) : result;
};
/**
* Should be used to extract callback from array of arguments. Modifies input param
*
* @method extractCallback
* @param {Array} arguments
* @return {Function|Null} callback, if exists
*/
Property.prototype.extractCallback = function (args) {
if (utils.isFunction(args[args.length - 1])) {
return args.pop(); // modify the args array!
}
};
/**
* Should attach function to method
*
@ -3939,7 +3958,10 @@ Property.prototype.attachToObject = function (obj) {
return prefix + name.charAt(0).toUpperCase() + name.slice(1);
};
obj[toAsyncName('get', name)] = this.getAsync.bind(this);
var func = this.getAsync.bind(this);
func.request = this.request.bind(this);
obj[toAsyncName('get', name)] = func;
};
/**
@ -3972,10 +3994,27 @@ Property.prototype.getAsync = function (callback) {
});
};
/**
* Should be called to create pure JSONRPC request which can be used in batch request
*
* @method request
* @param {...} params
* @return {Object} jsonrpc request
*/
Property.prototype.request = function () {
var payload = {
method: this.getter,
params: [],
callback: this.extractCallback(Array.prototype.slice.call(arguments))
};
payload.format = this.formatOutput.bind(this);
return payload;
};
module.exports = Property;
},{"./requestmanager":28}],27:[function(require,module,exports){
},{"../utils/utils":7,"./requestmanager":28}],27:[function(require,module,exports){
/*
This file is part of ethereum.js.

6
dist/web3.js.map vendored

File diff suppressed because one or more lines are too long

6
dist/web3.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -53,7 +53,7 @@ Batch.prototype.execute = function () {
if (requests[index].callback) {
if (!Jsonrpc.getInstance().isValidResponse(result)) {
requests[index].callback(errors.InvalidResponse(result));
return requests[index].callback(errors.InvalidResponse(result));
}
requests[index].callback(null, (requests[index].format ? requests[index].format(result.result) : result.result));

View File

@ -100,6 +100,64 @@ describe('lib/web3/batch', function () {
provider.injectBatchResults([result, result2]);
batch.execute();
});
it('should execute batch requests and receive errors', function (done) {
var provider = new FakeHttpProvider();
web3.setProvider(provider);
web3.reset();
var abi = [{
"name": "balance(address)",
"type": "function",
"inputs": [{
"name": "who",
"type": "address"
}],
"constant": true,
"outputs": [{
"name": "value",
"type": "uint256"
}]
}];
var address = '0x1000000000000000000000000000000000000001';
var result = 'Something went wrong';
var result2 = 'Something went wrong 2';
var counter = 0;
var callback = function (err, r) {
counter++;
assert.isNotNull(err);
};
var callback2 = function (err, r) {
assert.equal(counter, 1);
assert.isNotNull(err);
done();
};
provider.injectValidation(function (payload) {
var first = payload[0];
var second = payload[1];
assert.equal(first.method, 'eth_getBalance');
assert.deepEqual(first.params, ['0x0000000000000000000000000000000000000000', 'latest']);
assert.equal(second.method, 'eth_call');
assert.deepEqual(second.params, [{
'to': '0x1000000000000000000000000000000000000001',
'data': '0xe3d670d70000000000000000000000001000000000000000000000000000000000000001'
}]);
});
var batch = web3.createBatch();
batch.add(web3.eth.getBalance.request('0x0000000000000000000000000000000000000000', 'latest', callback));
batch.add(web3.eth.contract(abi).at(address).balance.request(address, callback2));
provider.injectBatchResults([result, result2], true); // injects error
batch.execute();
});
});
});

View File

@ -10,6 +10,17 @@ var getResponseStub = function () {
};
};
var getErrorStub = function () {
return {
jsonrpc: '2.0',
id: 1,
error: {
code: 1234,
message: ''
}
};
};
var FakeHttpProvider = function () {
this.response = getResponseStub();
this.error = null;
@ -48,10 +59,15 @@ FakeHttpProvider.prototype.injectResult = function (result) {
this.response.result = result;
};
FakeHttpProvider.prototype.injectBatchResults = function (results) {
FakeHttpProvider.prototype.injectBatchResults = function (results, error) {
this.response = results.map(function (r) {
var response = getResponseStub();
response.result = r;
if(error) {
var response = getErrorStub();
response.error.message = r;
} else {
var response = getResponseStub();
response.result = r;
}
return response;
});
};