Skip to content

web: configure payload size #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/services/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ var WebService = function(options) {
this.httpsOptions = options.httpsOptions || this.node.httpsOptions;
this.port = options.port || this.node.port || 3456;

// set the maximum size of json payload, defaults to express default
// see: https://github.com/expressjs/body-parser#limit
this.jsonRequestLimit = options.jsonRequestLimit || '100kb';

this.enableSocketRPC = _.isUndefined(options.enableSocketRPC) ?
WebService.DEFAULT_SOCKET_RPC : options.enableSocketRPC;

Expand All @@ -59,7 +63,7 @@ WebService.DEFAULT_SOCKET_RPC = true;
*/
WebService.prototype.start = function(callback) {
this.app = express();
this.app.use(bodyParser.json());
this.app.use(bodyParser.json({limit: this.jsonRequestLimit}));

if(this.https) {
this.transformHttpsOptions();
Expand Down
37 changes: 37 additions & 0 deletions test/services/web.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ describe('WebService', function() {
var web3 = new WebService({node: defaultNode});
web3.enableSocketRPC.should.equal(WebService.DEFAULT_SOCKET_RPC);
});
it('will set configuration options for max payload', function() {
var web = new WebService({node: defaultNode, jsonRequestLimit: '200kb'});
web.jsonRequestLimit.should.equal('200kb');
});
});

describe('#start', function() {
Expand Down Expand Up @@ -75,6 +79,39 @@ describe('WebService', function() {
done();
});
});
it('should pass json request limit to json body parser', function(done) {
var node = new EventEmitter();
var jsonStub = sinon.stub();
var TestWebService = proxyquire('../../lib/services/web', {
http: {
createServer: sinon.stub()
},
https: {
createServer: sinon.stub()
},
fs: fsStub,
express: sinon.stub().returns({
use: sinon.stub()
}),
'body-parser': {
json: jsonStub
},
'socket.io': {
listen: sinon.stub().returns({
on: sinon.stub()
})
}
});
var web = new TestWebService({node: node});
web.start(function(err) {
if (err) {
return done(err);
}
jsonStub.callCount.should.equal(1);
jsonStub.args[0][0].limit.should.equal('100kb');
done();
});
});
});

describe('#stop', function() {
Expand Down