2015-02-03 22:00:02 +00:00
|
|
|
var assert = require('assert');
|
2015-03-08 17:18:52 +00:00
|
|
|
var jsonrpc = require('../lib/web3/jsonrpc');
|
2015-03-21 22:34:24 +00:00
|
|
|
jsonrpc = new jsonrpc();
|
2015-02-03 22:00:02 +00:00
|
|
|
|
|
|
|
describe('jsonrpc', function () {
|
|
|
|
describe('isValidResponse', function () {
|
|
|
|
it('should validate basic jsonrpc response', function () {
|
|
|
|
|
|
|
|
// given
|
|
|
|
var response = {
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
id: 1,
|
|
|
|
result: []
|
|
|
|
};
|
|
|
|
|
|
|
|
// when
|
|
|
|
var valid = jsonrpc.isValidResponse(response);
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert.equal(valid, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate basic undefined response', function () {
|
|
|
|
|
|
|
|
// given
|
|
|
|
var response = undefined;
|
|
|
|
|
|
|
|
// when
|
|
|
|
var valid = jsonrpc.isValidResponse(response);
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert.equal(valid, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate jsonrpc response without jsonrpc field', function () {
|
|
|
|
|
|
|
|
// given
|
|
|
|
var response = {
|
|
|
|
id: 1,
|
|
|
|
result: []
|
|
|
|
};
|
|
|
|
|
|
|
|
// when
|
|
|
|
var valid = jsonrpc.isValidResponse(response);
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert.equal(valid, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate jsonrpc response with wrong jsonrpc version', function () {
|
|
|
|
|
|
|
|
// given
|
|
|
|
var response = {
|
|
|
|
jsonrpc: '1.0',
|
|
|
|
id: 1,
|
|
|
|
result: []
|
|
|
|
};
|
|
|
|
|
|
|
|
// when
|
|
|
|
var valid = jsonrpc.isValidResponse(response);
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert.equal(valid, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate jsonrpc response without id number', function () {
|
|
|
|
|
|
|
|
// given
|
|
|
|
var response = {
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
result: []
|
|
|
|
};
|
|
|
|
|
|
|
|
// when
|
|
|
|
var valid = jsonrpc.isValidResponse(response);
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert.equal(valid, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate jsonrpc response with wrong id field', function () {
|
|
|
|
|
|
|
|
// given
|
|
|
|
var response = {
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
id: 'x',
|
|
|
|
result: []
|
|
|
|
};
|
|
|
|
|
|
|
|
// when
|
|
|
|
var valid = jsonrpc.isValidResponse(response);
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert.equal(valid, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate jsonrpc response without result field', function () {
|
|
|
|
|
|
|
|
// given
|
|
|
|
var response = {
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
id: 1
|
|
|
|
};
|
|
|
|
|
|
|
|
// when
|
|
|
|
var valid = jsonrpc.isValidResponse(response);
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert.equal(valid, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate jsonrpc response with result field === false', function () {
|
|
|
|
|
|
|
|
// given
|
|
|
|
var response = {
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
id: 1,
|
|
|
|
result: false
|
|
|
|
};
|
|
|
|
|
|
|
|
// when
|
|
|
|
var valid = jsonrpc.isValidResponse(response);
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert.equal(valid, true);
|
|
|
|
});
|
|
|
|
|
2015-02-04 10:23:23 +00:00
|
|
|
it('should validate jsonrpc response with result field === 0', function () {
|
|
|
|
|
|
|
|
// given
|
|
|
|
var response = {
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
id: 1,
|
|
|
|
result: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
// when
|
|
|
|
var valid = jsonrpc.isValidResponse(response);
|
|
|
|
|
|
|
|
// then
|
|
|
|
assert.equal(valid, true);
|
|
|
|
});
|
2015-02-03 22:00:02 +00:00
|
|
|
});
|
|
|
|
});
|