diff --git a/test/json.js b/test/json.js index a69cc767..a5b3d8f3 100644 --- a/test/json.js +++ b/test/json.js @@ -143,14 +143,10 @@ describe('bodyParser.json()', function(){ }) }) - describe('with limit option', function(){ - it('should 413 when over limit with Content-Length', function(done){ - var buf = new Buffer(1024) - var server = createServer({ limit: '1kb' }) - - buf.fill('.') - - request(server) + describe('with limit option', function () { + it('should 413 when over limit with Content-Length', function (done) { + var buf = allocBuffer(1024, '.') + request(createServer({ limit: '1kb' })) .post('/') .set('Content-Type', 'application/json') .set('Content-Length', '1034') @@ -158,12 +154,9 @@ describe('bodyParser.json()', function(){ .expect(413, done) }) - it('should 413 when over limit with chunked encoding', function(done){ - var buf = new Buffer(1024) + it('should 413 when over limit with chunked encoding', function (done) { + var buf = allocBuffer(1024, '.') var server = createServer({ limit: '1kb' }) - - buf.fill('.') - var test = request(server).post('/') test.set('Content-Type', 'application/json') test.set('Transfer-Encoding', 'chunked') @@ -172,25 +165,20 @@ describe('bodyParser.json()', function(){ test.expect(413, done) }) - it('should accept number of bytes', function(done){ - var buf = new Buffer(1024) - var server = createServer({ limit: 1024 }) - - buf.fill('.') - - request(server) + it('should accept number of bytes', function (done) { + var buf = allocBuffer(1024, '.') + request(createServer({ limit: 1024 })) .post('/') .set('Content-Type', 'application/json') .send(JSON.stringify({ str: buf.toString() })) .expect(413, done) }) - it('should not change when options altered', function(done){ - var buf = new Buffer(1024) + it('should not change when options altered', function (done) { + var buf = allocBuffer(1024, '.') var options = { limit: '1kb' } var server = createServer(options) - buf.fill('.') options.limit = '100kb' request(server) @@ -200,12 +188,8 @@ describe('bodyParser.json()', function(){ .expect(413, done) }) - it('should not hang response', function(done){ - var buf = new Buffer(1024 * 10) - var server = createServer({ limit: '1kb' }) - - buf.fill('.') - + it('should not hang response', function (done) { + var buf = allocBuffer(10240, '.') var server = createServer({ limit: '8kb' }) var test = request(server).post('/') test.set('Content-Type', 'application/json') @@ -498,6 +482,16 @@ describe('bodyParser.json()', function(){ }) }) +function allocBuffer (size, fill) { + if (Buffer.alloc) { + return Buffer.alloc(size, fill) + } + + var buf = new Buffer(size) + buf.fill(fill) + return buf +} + function createServer(opts){ var _bodyParser = typeof opts !== 'function' ? bodyParser.json(opts) diff --git a/test/raw.js b/test/raw.js index 7a023411..a15ffeb8 100644 --- a/test/raw.js +++ b/test/raw.js @@ -66,13 +66,10 @@ describe('bodyParser.raw()', function(){ .expect(200, 'buf:746865207573657220697320746f6269', done) }) - describe('with limit option', function(){ - it('should 413 when over limit with Content-Length', function(done){ - var buf = new Buffer(1028) + describe('with limit option', function () { + it('should 413 when over limit with Content-Length', function (done) { + var buf = allocBuffer(1028, '.') var server = createServer({ limit: '1kb' }) - - buf.fill('.') - var test = request(server).post('/') test.set('Content-Type', 'application/octet-stream') test.set('Content-Length', '1028') @@ -80,12 +77,9 @@ describe('bodyParser.raw()', function(){ test.expect(413, done) }) - it('should 413 when over limit with chunked encoding', function(done){ - var buf = new Buffer(1028) + it('should 413 when over limit with chunked encoding', function (done) { + var buf = allocBuffer(1028, '.') var server = createServer({ limit: '1kb' }) - - buf.fill('.') - var test = request(server).post('/') test.set('Content-Type', 'application/octet-stream') test.set('Transfer-Encoding', 'chunked') @@ -93,24 +87,20 @@ describe('bodyParser.raw()', function(){ test.expect(413, done) }) - it('should accept number of bytes', function(done){ - var buf = new Buffer(1028) + it('should accept number of bytes', function (done) { + var buf = allocBuffer(1028, '.') var server = createServer({ limit: 1024 }) - - buf.fill('.') - var test = request(server).post('/') test.set('Content-Type', 'application/octet-stream') test.write(buf) test.expect(413, done) }) - it('should not change when options altered', function(done){ - var buf = new Buffer(1028) + it('should not change when options altered', function (done) { + var buf = allocBuffer(1028, '.') var options = { limit: '1kb' } var server = createServer(options) - buf.fill('.') options.limit = '100kb' var test = request(server).post('/') @@ -119,12 +109,8 @@ describe('bodyParser.raw()', function(){ test.expect(413, done) }) - it('should not hang response', function(done){ - var buf = new Buffer(1024 * 10) - var server = createServer({ limit: '1kb' }) - - buf.fill('.') - + it('should not hang response', function (done) { + var buf = allocBuffer(10240, '.') var server = createServer({ limit: '8kb' }) var test = request(server).post('/') test.set('Content-Type', 'application/octet-stream') @@ -341,6 +327,16 @@ describe('bodyParser.raw()', function(){ }) }) +function allocBuffer (size, fill) { + if (Buffer.alloc) { + return Buffer.alloc(size, fill) + } + + var buf = new Buffer(size) + buf.fill(fill) + return buf +} + function createServer(opts){ var _bodyParser = typeof opts !== 'function' ? bodyParser.raw(opts) diff --git a/test/text.js b/test/text.js index ae2e00c7..e6de2e32 100644 --- a/test/text.js +++ b/test/text.js @@ -88,14 +88,10 @@ describe('bodyParser.text()', function(){ }) }) - describe('with limit option', function(){ - it('should 413 when over limit with Content-Length', function(done){ - var buf = new Buffer(1028) - var server = createServer({ limit: '1kb' }) - - buf.fill('.') - - request(server) + describe('with limit option', function () { + it('should 413 when over limit with Content-Length', function (done) { + var buf = allocBuffer(1028, '.') + request(createServer({ limit: '1kb' })) .post('/') .set('Content-Type', 'text/plain') .set('Content-Length', '1028') @@ -103,12 +99,9 @@ describe('bodyParser.text()', function(){ .expect(413, done) }) - it('should 413 when over limit with chunked encoding', function(done){ - var buf = new Buffer(1028) + it('should 413 when over limit with chunked encoding', function (done) { + var buf = allocBuffer(1028, '.') var server = createServer({ limit: '1kb' }) - - buf.fill('.') - var test = request(server).post('/') test.set('Content-Type', 'text/plain') test.set('Transfer-Encoding', 'chunked') @@ -116,25 +109,20 @@ describe('bodyParser.text()', function(){ test.expect(413, done) }) - it('should accept number of bytes', function(done){ - var buf = new Buffer(1028) - var server = createServer({ limit: 1024 }) - - buf.fill('.') - - request(server) + it('should accept number of bytes', function (done) { + var buf = allocBuffer(1028, '.') + request(createServer({ limit: 1024 })) .post('/') .set('Content-Type', 'text/plain') .send(buf.toString()) .expect(413, done) }) - it('should not change when options altered', function(done){ - var buf = new Buffer(1028) + it('should not change when options altered', function (done) { + var buf = allocBuffer(1028, '.') var options = { limit: '1kb' } var server = createServer(options) - buf.fill('.') options.limit = '100kb' request(server) @@ -144,12 +132,8 @@ describe('bodyParser.text()', function(){ .expect(413, done) }) - it('should not hang response', function(done){ - var buf = new Buffer(1024 * 10) - var server = createServer({ limit: '1kb' }) - - buf.fill('.') - + it('should not hang response', function (done) { + var buf = allocBuffer(10240, '.') var server = createServer({ limit: '8kb' }) var test = request(server).post('/') test.set('Content-Type', 'text/plain') @@ -412,6 +396,16 @@ describe('bodyParser.text()', function(){ }) }) +function allocBuffer (size, fill) { + if (Buffer.alloc) { + return Buffer.alloc(size, fill) + } + + var buf = new Buffer(size) + buf.fill(fill) + return buf +} + function createServer(opts){ var _bodyParser = typeof opts !== 'function' ? bodyParser.text(opts) diff --git a/test/urlencoded.js b/test/urlencoded.js index a2ff2b12..250b8f35 100644 --- a/test/urlencoded.js +++ b/test/urlencoded.js @@ -235,14 +235,10 @@ describe('bodyParser.urlencoded()', function(){ }) }) - describe('with limit option', function(){ - it('should 413 when over limit with Content-Length', function(done){ - var buf = new Buffer(1024) - var server = createServer({ limit: '1kb' }) - - buf.fill('.') - - request(server) + describe('with limit option', function () { + it('should 413 when over limit with Content-Length', function (done) { + var buf = allocBuffer(1024, '.') + request(createServer({ limit: '1kb' })) .post('/') .set('Content-Type', 'application/x-www-form-urlencoded') .set('Content-Length', '1028') @@ -250,12 +246,9 @@ describe('bodyParser.urlencoded()', function(){ .expect(413, done) }) - it('should 413 when over limit with chunked encoding', function(done){ - var buf = new Buffer(1024) + it('should 413 when over limit with chunked encoding', function (done) { + var buf = allocBuffer(1024, '.') var server = createServer({ limit: '1kb' }) - - buf.fill('.') - var test = request(server).post('/') test.set('Content-Type', 'application/x-www-form-urlencoded') test.set('Transfer-Encoding', 'chunked') @@ -264,25 +257,20 @@ describe('bodyParser.urlencoded()', function(){ test.expect(413, done) }) - it('should accept number of bytes', function(done){ - var buf = new Buffer(1024) - var server = createServer({ limit: 1024 }) - - buf.fill('.') - - request(server) + it('should accept number of bytes', function (done) { + var buf = allocBuffer(1024, '.') + request(createServer({ limit: 1024 })) .post('/') .set('Content-Type', 'application/x-www-form-urlencoded') .send('str=' + buf.toString()) .expect(413, done) }) - it('should not change when options altered', function(done){ - var buf = new Buffer(1024) + it('should not change when options altered', function (done) { + var buf = allocBuffer(1024, '.') var options = { limit: '1kb' } var server = createServer(options) - buf.fill('.') options.limit = '100kb' request(server) @@ -292,12 +280,8 @@ describe('bodyParser.urlencoded()', function(){ .expect(413, done) }) - it('should not hang response', function(done){ - var buf = new Buffer(1024 * 10) - var server = createServer({ limit: '1kb' }) - - buf.fill('.') - + it('should not hang response', function (done) { + var buf = allocBuffer(10240, '.') var server = createServer({ limit: '8kb' }) var test = request(server).post('/') test.set('Content-Type', 'application/x-www-form-urlencoded') @@ -629,6 +613,16 @@ describe('bodyParser.urlencoded()', function(){ }) }) +function allocBuffer (size, fill) { + if (Buffer.alloc) { + return Buffer.alloc(size, fill) + } + + var buf = new Buffer(size) + buf.fill(fill) + return buf +} + function createManyParams(count) { var str = ''