Skip to content

Commit

Permalink
tests: use Buffer.alloc when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 20, 2016
1 parent 1050e7b commit b4dc075
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 112 deletions.
52 changes: 23 additions & 29 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,27 +143,20 @@ 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')
.send(JSON.stringify({ str: buf.toString() }))
.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')
Expand All @@ -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)
Expand All @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
46 changes: 21 additions & 25 deletions test/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,51 +66,41 @@ 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')
test.write(buf)
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')
test.write(buf)
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('/')
Expand All @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
52 changes: 23 additions & 29 deletions test/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,53 +88,41 @@ 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')
.send(buf.toString())
.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')
test.write(buf.toString())
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)
Expand All @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
52 changes: 23 additions & 29 deletions test/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,27 +235,20 @@ 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')
.send('str=' + buf.toString())
.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')
Expand All @@ -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)
Expand All @@ -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')
Expand Down Expand Up @@ -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 = ''

Expand Down

0 comments on commit b4dc075

Please sign in to comment.