From b7f888fc3eec347645b4120686f160c89731ec3a Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Thu, 16 Jun 2016 13:36:30 -0400 Subject: [PATCH] web: configure payload size --- lib/services/web.js | 6 +++++- test/services/web.unit.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/services/web.js b/lib/services/web.js index b050b5d5..850accb0 100644 --- a/lib/services/web.js +++ b/lib/services/web.js @@ -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; @@ -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(); diff --git a/test/services/web.unit.js b/test/services/web.unit.js index 3d0ff593..b57d07b4 100644 --- a/test/services/web.unit.js +++ b/test/services/web.unit.js @@ -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() { @@ -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() {