solidity/helpers/FakeHttpProvider.js
Marek Kotewicz 572d268f28 Squashed 'libjsqrc/ethereumjs/' changes from c74c854..3b799d1
3b799d1 fixed typos
c5f6379 version 0.3.3
d795d36 fixed trimming call output prefix
a2f561f fixed calling contract only with array param
4912ec6 tests for decoding solidity params
2b4fd21 solidity param encoding tests

git-subtree-dir: libjsqrc/ethereumjs
git-subtree-split: 3b799d128452639463424c657956ee90a28daec6
2015-04-26 17:40:35 +02:00

73 lines
1.9 KiB
JavaScript

var chai = require('chai');
var assert = require('assert');
var utils = require('../../lib/utils/utils');
var getResponseStub = function () {
return {
jsonrpc: '2.0',
id: 1,
result: 0
};
};
var FakeHttpProvider = function () {
this.response = getResponseStub();
this.error = null;
this.validation = null;
};
FakeHttpProvider.prototype.send = function (payload) {
assert.equal(utils.isArray(payload) || utils.isObject(payload), true);
// TODO: validate jsonrpc request
if (this.error) {
throw this.error;
}
if (this.validation) {
// imitate plain json object
this.validation(JSON.parse(JSON.stringify(payload)));
}
return this.getResponse();
};
FakeHttpProvider.prototype.sendAsync = function (payload, callback) {
assert.equal(utils.isArray(payload) || utils.isObject(payload), true);
assert.equal(utils.isFunction(callback), true);
if (this.validation) {
// imitate plain json object
this.validation(JSON.parse(JSON.stringify(payload)), callback);
}
callback(this.error, this.getResponse());
};
FakeHttpProvider.prototype.injectResponse = function (response) {
this.response = response;
};
FakeHttpProvider.prototype.injectResult = function (result) {
this.response = getResponseStub();
this.response.result = result;
};
FakeHttpProvider.prototype.injectBatchResults = function (results) {
this.response = results.map(function (r) {
var response = getResponseStub();
response.result = r;
return response;
});
};
FakeHttpProvider.prototype.getResponse = function () {
return this.response;
};
FakeHttpProvider.prototype.injectError = function (error) {
this.error = error;
};
FakeHttpProvider.prototype.injectValidation = function (callback) {
this.validation = callback;
};
module.exports = FakeHttpProvider;