web3.js/test/helpers/FakeHttpProvider.js

108 lines
2.7 KiB
JavaScript
Raw Normal View History

2015-03-22 09:20:24 +01:00
var chai = require('chai');
var assert = require('assert');
var utils = require('../../lib/utils/utils');
2015-03-22 09:20:24 +01:00
countId = 1;
2015-03-22 09:20:24 +01:00
var getResponseStub = function () {
return {
jsonrpc: '2.0',
id: countId++,
2015-09-15 11:05:38 +02:00
result: null
2015-03-22 09:20:24 +01:00
};
};
2015-07-15 17:49:55 +02:00
var getErrorStub = function () {
return {
jsonrpc: '2.0',
countId: countId++,
2015-07-15 17:49:55 +02:00
error: {
code: 1234,
2015-09-15 11:05:38 +02:00
message: 'Stub error'
2015-07-15 17:49:55 +02:00
}
};
};
2015-03-22 09:20:24 +01:00
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) {
2015-03-25 21:46:45 +01:00
// imitate plain json object
this.validation(JSON.parse(JSON.stringify(payload)));
2015-03-22 09:20:24 +01:00
}
return this.getResponse(payload);
2015-03-22 09:20:24 +01:00
};
FakeHttpProvider.prototype.sendAsync = function (payload, callback) {
assert.equal(utils.isArray(payload) || utils.isObject(payload), true);
assert.equal(utils.isFunction(callback), true);
if (this.validation) {
2015-03-25 21:46:45 +01:00
// imitate plain json object
this.validation(JSON.parse(JSON.stringify(payload)), callback);
2015-03-22 09:20:24 +01:00
}
2015-09-15 11:05:38 +02:00
var response = this.getResponse(payload);
var error = this.error;
setTimeout(function(){
callback(error, response);
2015-09-15 11:05:38 +02:00
},1);
2015-03-22 09:20:24 +01:00
};
FakeHttpProvider.prototype.injectResponse = function (response) {
this.response = response;
};
FakeHttpProvider.prototype.injectResult = function (result) {
this.response = getResponseStub();
this.response.result = result;
};
2015-07-15 17:49:55 +02:00
FakeHttpProvider.prototype.injectBatchResults = function (results, error) {
2015-03-25 13:17:21 +01:00
this.response = results.map(function (r) {
2015-07-15 17:49:55 +02:00
if(error) {
var response = getErrorStub();
response.error.message = r;
} else {
var response = getResponseStub();
response.result = r;
}
2015-03-25 13:17:21 +01:00
return response;
});
};
FakeHttpProvider.prototype.getResponse = function (payload) {
if(this.response) {
if(utils.isArray(this.response)) {
this.response = this.response.map(function(response, index) {
response.id = payload[index] ? payload[index].id : countId++;
return response;
});
} else
this.response.id = payload.id;
}
return this.response;
};
2015-03-22 09:20:24 +01:00
FakeHttpProvider.prototype.injectError = function (error) {
this.error = error;
};
FakeHttpProvider.prototype.injectValidation = function (callback) {
this.validation = callback;
};
module.exports = FakeHttpProvider;