Skip to content

Commit 5351284

Browse files
authored
Merge pull request #458 from braydonf/config-payload
web: configure payload size
2 parents fa79e69 + b7f888f commit 5351284

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/services/web.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ var WebService = function(options) {
3737
this.httpsOptions = options.httpsOptions || this.node.httpsOptions;
3838
this.port = options.port || this.node.port || 3456;
3939

40+
// set the maximum size of json payload, defaults to express default
41+
// see: https://github.com/expressjs/body-parser#limit
42+
this.jsonRequestLimit = options.jsonRequestLimit || '100kb';
43+
4044
this.enableSocketRPC = _.isUndefined(options.enableSocketRPC) ?
4145
WebService.DEFAULT_SOCKET_RPC : options.enableSocketRPC;
4246

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

6468
if(this.https) {
6569
this.transformHttpsOptions();

test/services/web.unit.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ describe('WebService', function() {
4646
var web3 = new WebService({node: defaultNode});
4747
web3.enableSocketRPC.should.equal(WebService.DEFAULT_SOCKET_RPC);
4848
});
49+
it('will set configuration options for max payload', function() {
50+
var web = new WebService({node: defaultNode, jsonRequestLimit: '200kb'});
51+
web.jsonRequestLimit.should.equal('200kb');
52+
});
4953
});
5054

5155
describe('#start', function() {
@@ -75,6 +79,39 @@ describe('WebService', function() {
7579
done();
7680
});
7781
});
82+
it('should pass json request limit to json body parser', function(done) {
83+
var node = new EventEmitter();
84+
var jsonStub = sinon.stub();
85+
var TestWebService = proxyquire('../../lib/services/web', {
86+
http: {
87+
createServer: sinon.stub()
88+
},
89+
https: {
90+
createServer: sinon.stub()
91+
},
92+
fs: fsStub,
93+
express: sinon.stub().returns({
94+
use: sinon.stub()
95+
}),
96+
'body-parser': {
97+
json: jsonStub
98+
},
99+
'socket.io': {
100+
listen: sinon.stub().returns({
101+
on: sinon.stub()
102+
})
103+
}
104+
});
105+
var web = new TestWebService({node: node});
106+
web.start(function(err) {
107+
if (err) {
108+
return done(err);
109+
}
110+
jsonStub.callCount.should.equal(1);
111+
jsonStub.args[0][0].limit.should.equal('100kb');
112+
done();
113+
});
114+
});
78115
});
79116

80117
describe('#stop', function() {

0 commit comments

Comments
 (0)