node-sandbox, tests for httpprovider, request manager refactor in progress

This commit is contained in:
Marek Kotewicz 2015-03-21 21:59:38 +01:00
parent de5de9eec8
commit 288caf32be
8 changed files with 156 additions and 107 deletions

91
dist/ethereum.js vendored
View File

@ -1119,31 +1119,34 @@ var web3Properties = [
var setupMethods = function (obj, methods) {
methods.forEach(function (method) {
// allow for object methods 'myObject.method'
var objectMethods = method.name.split('.'),
callFunction = function () {
var callFunction = function () {
/*jshint maxcomplexity:8 */
var callback = null,
args = Array.prototype.slice.call(arguments),
call = typeof method.call === 'function' ? method.call(args) : method.call;
// show deprecated warning
if (method.newMethod)
console.warn('This method is deprecated please use web3.'+ method.newMethod +'() instead.');
var callback = null;
var args = Array.prototype.slice.call(arguments);
var call = utils.isFunction(method.call) ? method.call(args) : method.call;
// get the callback if one is available
if(typeof args[args.length-1] === 'function'){
callback = args[args.length-1];
Array.prototype.pop.call(args);
if (utils.isFunction(args[args.length - 1])) {
callback = args[args.length - 1];
args.pop();
}
// add the defaultBlock if not given
if(method.addDefaultblock) {
if(args.length !== method.addDefaultblock)
Array.prototype.push.call(args, (isFinite(c.ETH_DEFAULTBLOCK) ? utils.fromDecimal(c.ETH_DEFAULTBLOCK) : c.ETH_DEFAULTBLOCK));
else
// TODO: having this here is REALLY BAD. We should not have separate logic only for default block
// it should be handled by formatter
if (method.addDefaultblock) {
if (args.length !== method.addDefaultblock) {
args.push(isFinite(c.ETH_DEFAULTBLOCK) ? utils.fromDecimal(c.ETH_DEFAULTBLOCK) : c.ETH_DEFAULTBLOCK);
} else {
args[args.length-1] = isFinite(args[args.length-1]) ? utils.fromDecimal(args[args.length-1]) : args[args.length-1];
}
// show deprecated warning
if(method.newMethod)
console.warn('This method is deprecated please use web3.'+ method.newMethod +'() instead.');
}
return web3.manager.send({
method: call,
@ -1154,15 +1157,12 @@ var setupMethods = function (obj, methods) {
}, callback);
};
if(objectMethods.length > 1) {
if(!obj[objectMethods[0]])
obj[objectMethods[0]] = {};
obj[objectMethods[0]][objectMethods[1]] = callFunction;
var name = method.name.split('.');
if (name.length > 1) {
obj[name[0]] = obj[name[0]] || {};
obj[name[0]][name[1]] = callFunction;
} else {
obj[objectMethods[0]] = callFunction;
obj[name[0]] = callFunction;
}
});
@ -2349,44 +2349,39 @@ module.exports = {
* @date 2014
*/
"use strict";
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
var HttpProvider = function (host) {
this.name = 'HTTP';
this.host = host || 'http://localhost:8080';
};
HttpProvider.prototype.send = function (payload, callback) {
HttpProvider.prototype.send = function (payload) {
var request = new XMLHttpRequest();
// ASYNC
if(typeof callback === 'function') {
request.open('POST', this.host, false);
request.send(JSON.stringify(payload));
// check request.status
// TODO: throw an error here! it cannot silently fail!!!
//if (request.status !== 200) {
//return;
//}
return JSON.parse(request.responseText);
};
HttpProvider.prototype.sendAsync = function (payload, callback) {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if(request.readyState === 4) {
var result = '';
try {
result = JSON.parse(request.responseText);
} catch(error) {
result = error;
}
callback(result, request.status);
if (request.readyState === 4) {
// TODO: handle the error properly here!!!
callback(null, JSON.parse(request.responseText));
}
};
request.open('POST', this.host, true);
request.send(JSON.stringify(payload));
// SYNC
} else {
request.open('POST', this.host, false);
request.send(JSON.stringify(payload));
// check request.status
if(request.status !== 200)
return;
return JSON.parse(request.responseText);
}
};
module.exports = HttpProvider;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -51,31 +51,34 @@ var web3Properties = [
var setupMethods = function (obj, methods) {
methods.forEach(function (method) {
// allow for object methods 'myObject.method'
var objectMethods = method.name.split('.'),
callFunction = function () {
var callFunction = function () {
/*jshint maxcomplexity:8 */
var callback = null,
args = Array.prototype.slice.call(arguments),
call = typeof method.call === 'function' ? method.call(args) : method.call;
// show deprecated warning
if (method.newMethod)
console.warn('This method is deprecated please use web3.'+ method.newMethod +'() instead.');
var callback = null;
var args = Array.prototype.slice.call(arguments);
var call = utils.isFunction(method.call) ? method.call(args) : method.call;
// get the callback if one is available
if(typeof args[args.length-1] === 'function'){
callback = args[args.length-1];
Array.prototype.pop.call(args);
if (utils.isFunction(args[args.length - 1])) {
callback = args[args.length - 1];
args.pop();
}
// add the defaultBlock if not given
if(method.addDefaultblock) {
if(args.length !== method.addDefaultblock)
Array.prototype.push.call(args, (isFinite(c.ETH_DEFAULTBLOCK) ? utils.fromDecimal(c.ETH_DEFAULTBLOCK) : c.ETH_DEFAULTBLOCK));
else
// TODO: having this here is REALLY BAD. We should not have separate logic only for default block
// it should be handled by formatter
if (method.addDefaultblock) {
if (args.length !== method.addDefaultblock) {
args.push(isFinite(c.ETH_DEFAULTBLOCK) ? utils.fromDecimal(c.ETH_DEFAULTBLOCK) : c.ETH_DEFAULTBLOCK);
} else {
args[args.length-1] = isFinite(args[args.length-1]) ? utils.fromDecimal(args[args.length-1]) : args[args.length-1];
}
// show deprecated warning
if(method.newMethod)
console.warn('This method is deprecated please use web3.'+ method.newMethod +'() instead.');
}
return web3.manager.send({
method: call,
@ -86,15 +89,12 @@ var setupMethods = function (obj, methods) {
}, callback);
};
if(objectMethods.length > 1) {
if(!obj[objectMethods[0]])
obj[objectMethods[0]] = {};
obj[objectMethods[0]][objectMethods[1]] = callFunction;
var name = method.name.split('.');
if (name.length > 1) {
obj[name[0]] = obj[name[0]] || {};
obj[name[0]][name[1]] = callFunction;
} else {
obj[objectMethods[0]] = callFunction;
obj[name[0]] = callFunction;
}
});

View File

@ -22,44 +22,39 @@
* @date 2014
*/
"use strict";
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
var HttpProvider = function (host) {
this.name = 'HTTP';
this.host = host || 'http://localhost:8080';
};
HttpProvider.prototype.send = function (payload, callback) {
HttpProvider.prototype.send = function (payload) {
var request = new XMLHttpRequest();
// ASYNC
if(typeof callback === 'function') {
request.open('POST', this.host, false);
request.send(JSON.stringify(payload));
// check request.status
// TODO: throw an error here! it cannot silently fail!!!
//if (request.status !== 200) {
//return;
//}
return JSON.parse(request.responseText);
};
HttpProvider.prototype.sendAsync = function (payload, callback) {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if(request.readyState === 4) {
var result = '';
try {
result = JSON.parse(request.responseText);
} catch(error) {
result = error;
}
callback(result, request.status);
if (request.readyState === 4) {
// TODO: handle the error properly here!!!
callback(null, JSON.parse(request.responseText));
}
};
request.open('POST', this.host, true);
request.send(JSON.stringify(payload));
// SYNC
} else {
request.open('POST', this.host, false);
request.send(JSON.stringify(payload));
// check request.status
if(request.status !== 200)
return;
return JSON.parse(request.responseText);
}
};
module.exports = HttpProvider;

View File

@ -36,6 +36,7 @@
"karma-mocha": "^0.1.10",
"karma-safari-launcher": "^0.1.1",
"mocha": ">=2.1.0",
"sandboxed-module": "^2.0.0",
"vinyl-source-stream": "^1.0.0"
},
"scripts": {

View File

@ -0,0 +1,31 @@
var chai = require('chai');
var assert = chai.assert;
var FakeXMLHttpRequest = function () {
this.responseText = "{}";
this.readyState = 4;
this.onreadystatechange = null;
this.async = false;
};
FakeXMLHttpRequest.prototype.open = function (method, host, async) {
assert.equal(method, 'POST');
assert.notEqual(host, null);
assert.equal(async === false || async === true, true);
this.async = async;
};
FakeXMLHttpRequest.prototype.send = function (payload) {
assert.equal(typeof payload, 'string');
if (this.async) {
assert.equal(typeof this.onreadystatechange, 'function');
this.onreadystatechange();
return;
}
return this.responseText;
};
module.exports = {
XMLHttpRequest: FakeXMLHttpRequest
};

27
test/httpprovider.js Normal file
View File

@ -0,0 +1,27 @@
var chai = require('chai');
var assert = chai.assert;
var SandboxedModule = require('sandboxed-module');
var HttpProvider = SandboxedModule.require('../lib/web3/httpprovider', {
requires: {
'xmlhttprequest': require('./FakeXMLHttpRequest')
}
});
describe('httpprovider', function () {
describe('send', function () {
var provider = new HttpProvider();
var result = provider.send({});
assert.equal(typeof result, "object");
});
describe('sendAsync', function (done) {
var provider = new HttpProvider();
provider.send({}, function (err, result) {
assert.equal(typeof result, "string");
});
});
});