2015-03-22 08:20:24 +00:00
|
|
|
var chai = require('chai');
|
|
|
|
var assert = require('assert');
|
2015-03-23 14:57:36 +00:00
|
|
|
var utils = require('../../lib/utils/utils');
|
2015-03-22 08:20:24 +00:00
|
|
|
|
|
|
|
var getResponseStub = function () {
|
|
|
|
return {
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
id: 1,
|
|
|
|
result: 0
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-07-15 15:49:55 +00:00
|
|
|
var getErrorStub = function () {
|
|
|
|
return {
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
id: 1,
|
|
|
|
error: {
|
|
|
|
code: 1234,
|
|
|
|
message: ''
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-03-22 08:20:24 +00: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 20:46:45 +00:00
|
|
|
// imitate plain json object
|
|
|
|
this.validation(JSON.parse(JSON.stringify(payload)));
|
2015-03-22 08:20:24 +00:00
|
|
|
}
|
2015-04-26 08:08:58 +00:00
|
|
|
return this.getResponse();
|
2015-03-22 08:20:24 +00: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 20:46:45 +00:00
|
|
|
// imitate plain json object
|
|
|
|
this.validation(JSON.parse(JSON.stringify(payload)), callback);
|
2015-03-22 08:20:24 +00:00
|
|
|
}
|
2015-04-26 08:08:58 +00:00
|
|
|
callback(this.error, this.getResponse());
|
2015-03-22 08:20:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
FakeHttpProvider.prototype.injectResponse = function (response) {
|
|
|
|
this.response = response;
|
|
|
|
};
|
|
|
|
|
|
|
|
FakeHttpProvider.prototype.injectResult = function (result) {
|
|
|
|
this.response = getResponseStub();
|
|
|
|
this.response.result = result;
|
|
|
|
};
|
|
|
|
|
2015-07-15 15:49:55 +00:00
|
|
|
FakeHttpProvider.prototype.injectBatchResults = function (results, error) {
|
2015-03-25 12:17:21 +00:00
|
|
|
this.response = results.map(function (r) {
|
2015-07-15 15:49:55 +00:00
|
|
|
if(error) {
|
|
|
|
var response = getErrorStub();
|
|
|
|
response.error.message = r;
|
|
|
|
} else {
|
|
|
|
var response = getResponseStub();
|
|
|
|
response.result = r;
|
|
|
|
}
|
2015-03-25 12:17:21 +00:00
|
|
|
return response;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-04-26 08:08:58 +00:00
|
|
|
FakeHttpProvider.prototype.getResponse = function () {
|
|
|
|
return this.response;
|
|
|
|
};
|
|
|
|
|
2015-03-22 08:20:24 +00:00
|
|
|
FakeHttpProvider.prototype.injectError = function (error) {
|
|
|
|
this.error = error;
|
|
|
|
};
|
|
|
|
|
|
|
|
FakeHttpProvider.prototype.injectValidation = function (callback) {
|
|
|
|
this.validation = callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = FakeHttpProvider;
|
|
|
|
|