Skip to content

Commit b4dc075

Browse files
committed
tests: use Buffer.alloc when possible
1 parent 1050e7b commit b4dc075

File tree

4 files changed

+90
-112
lines changed

4 files changed

+90
-112
lines changed

test/json.js

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -143,27 +143,20 @@ describe('bodyParser.json()', function(){
143143
})
144144
})
145145

146-
describe('with limit option', function(){
147-
it('should 413 when over limit with Content-Length', function(done){
148-
var buf = new Buffer(1024)
149-
var server = createServer({ limit: '1kb' })
150-
151-
buf.fill('.')
152-
153-
request(server)
146+
describe('with limit option', function () {
147+
it('should 413 when over limit with Content-Length', function (done) {
148+
var buf = allocBuffer(1024, '.')
149+
request(createServer({ limit: '1kb' }))
154150
.post('/')
155151
.set('Content-Type', 'application/json')
156152
.set('Content-Length', '1034')
157153
.send(JSON.stringify({ str: buf.toString() }))
158154
.expect(413, done)
159155
})
160156

161-
it('should 413 when over limit with chunked encoding', function(done){
162-
var buf = new Buffer(1024)
157+
it('should 413 when over limit with chunked encoding', function (done) {
158+
var buf = allocBuffer(1024, '.')
163159
var server = createServer({ limit: '1kb' })
164-
165-
buf.fill('.')
166-
167160
var test = request(server).post('/')
168161
test.set('Content-Type', 'application/json')
169162
test.set('Transfer-Encoding', 'chunked')
@@ -172,25 +165,20 @@ describe('bodyParser.json()', function(){
172165
test.expect(413, done)
173166
})
174167

175-
it('should accept number of bytes', function(done){
176-
var buf = new Buffer(1024)
177-
var server = createServer({ limit: 1024 })
178-
179-
buf.fill('.')
180-
181-
request(server)
168+
it('should accept number of bytes', function (done) {
169+
var buf = allocBuffer(1024, '.')
170+
request(createServer({ limit: 1024 }))
182171
.post('/')
183172
.set('Content-Type', 'application/json')
184173
.send(JSON.stringify({ str: buf.toString() }))
185174
.expect(413, done)
186175
})
187176

188-
it('should not change when options altered', function(done){
189-
var buf = new Buffer(1024)
177+
it('should not change when options altered', function (done) {
178+
var buf = allocBuffer(1024, '.')
190179
var options = { limit: '1kb' }
191180
var server = createServer(options)
192181

193-
buf.fill('.')
194182
options.limit = '100kb'
195183

196184
request(server)
@@ -200,12 +188,8 @@ describe('bodyParser.json()', function(){
200188
.expect(413, done)
201189
})
202190

203-
it('should not hang response', function(done){
204-
var buf = new Buffer(1024 * 10)
205-
var server = createServer({ limit: '1kb' })
206-
207-
buf.fill('.')
208-
191+
it('should not hang response', function (done) {
192+
var buf = allocBuffer(10240, '.')
209193
var server = createServer({ limit: '8kb' })
210194
var test = request(server).post('/')
211195
test.set('Content-Type', 'application/json')
@@ -498,6 +482,16 @@ describe('bodyParser.json()', function(){
498482
})
499483
})
500484

485+
function allocBuffer (size, fill) {
486+
if (Buffer.alloc) {
487+
return Buffer.alloc(size, fill)
488+
}
489+
490+
var buf = new Buffer(size)
491+
buf.fill(fill)
492+
return buf
493+
}
494+
501495
function createServer(opts){
502496
var _bodyParser = typeof opts !== 'function'
503497
? bodyParser.json(opts)

test/raw.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,51 +66,41 @@ describe('bodyParser.raw()', function(){
6666
.expect(200, 'buf:746865207573657220697320746f6269', done)
6767
})
6868

69-
describe('with limit option', function(){
70-
it('should 413 when over limit with Content-Length', function(done){
71-
var buf = new Buffer(1028)
69+
describe('with limit option', function () {
70+
it('should 413 when over limit with Content-Length', function (done) {
71+
var buf = allocBuffer(1028, '.')
7272
var server = createServer({ limit: '1kb' })
73-
74-
buf.fill('.')
75-
7673
var test = request(server).post('/')
7774
test.set('Content-Type', 'application/octet-stream')
7875
test.set('Content-Length', '1028')
7976
test.write(buf)
8077
test.expect(413, done)
8178
})
8279

83-
it('should 413 when over limit with chunked encoding', function(done){
84-
var buf = new Buffer(1028)
80+
it('should 413 when over limit with chunked encoding', function (done) {
81+
var buf = allocBuffer(1028, '.')
8582
var server = createServer({ limit: '1kb' })
86-
87-
buf.fill('.')
88-
8983
var test = request(server).post('/')
9084
test.set('Content-Type', 'application/octet-stream')
9185
test.set('Transfer-Encoding', 'chunked')
9286
test.write(buf)
9387
test.expect(413, done)
9488
})
9589

96-
it('should accept number of bytes', function(done){
97-
var buf = new Buffer(1028)
90+
it('should accept number of bytes', function (done) {
91+
var buf = allocBuffer(1028, '.')
9892
var server = createServer({ limit: 1024 })
99-
100-
buf.fill('.')
101-
10293
var test = request(server).post('/')
10394
test.set('Content-Type', 'application/octet-stream')
10495
test.write(buf)
10596
test.expect(413, done)
10697
})
10798

108-
it('should not change when options altered', function(done){
109-
var buf = new Buffer(1028)
99+
it('should not change when options altered', function (done) {
100+
var buf = allocBuffer(1028, '.')
110101
var options = { limit: '1kb' }
111102
var server = createServer(options)
112103

113-
buf.fill('.')
114104
options.limit = '100kb'
115105

116106
var test = request(server).post('/')
@@ -119,12 +109,8 @@ describe('bodyParser.raw()', function(){
119109
test.expect(413, done)
120110
})
121111

122-
it('should not hang response', function(done){
123-
var buf = new Buffer(1024 * 10)
124-
var server = createServer({ limit: '1kb' })
125-
126-
buf.fill('.')
127-
112+
it('should not hang response', function (done) {
113+
var buf = allocBuffer(10240, '.')
128114
var server = createServer({ limit: '8kb' })
129115
var test = request(server).post('/')
130116
test.set('Content-Type', 'application/octet-stream')
@@ -341,6 +327,16 @@ describe('bodyParser.raw()', function(){
341327
})
342328
})
343329

330+
function allocBuffer (size, fill) {
331+
if (Buffer.alloc) {
332+
return Buffer.alloc(size, fill)
333+
}
334+
335+
var buf = new Buffer(size)
336+
buf.fill(fill)
337+
return buf
338+
}
339+
344340
function createServer(opts){
345341
var _bodyParser = typeof opts !== 'function'
346342
? bodyParser.raw(opts)

test/text.js

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -88,53 +88,41 @@ describe('bodyParser.text()', function(){
8888
})
8989
})
9090

91-
describe('with limit option', function(){
92-
it('should 413 when over limit with Content-Length', function(done){
93-
var buf = new Buffer(1028)
94-
var server = createServer({ limit: '1kb' })
95-
96-
buf.fill('.')
97-
98-
request(server)
91+
describe('with limit option', function () {
92+
it('should 413 when over limit with Content-Length', function (done) {
93+
var buf = allocBuffer(1028, '.')
94+
request(createServer({ limit: '1kb' }))
9995
.post('/')
10096
.set('Content-Type', 'text/plain')
10197
.set('Content-Length', '1028')
10298
.send(buf.toString())
10399
.expect(413, done)
104100
})
105101

106-
it('should 413 when over limit with chunked encoding', function(done){
107-
var buf = new Buffer(1028)
102+
it('should 413 when over limit with chunked encoding', function (done) {
103+
var buf = allocBuffer(1028, '.')
108104
var server = createServer({ limit: '1kb' })
109-
110-
buf.fill('.')
111-
112105
var test = request(server).post('/')
113106
test.set('Content-Type', 'text/plain')
114107
test.set('Transfer-Encoding', 'chunked')
115108
test.write(buf.toString())
116109
test.expect(413, done)
117110
})
118111

119-
it('should accept number of bytes', function(done){
120-
var buf = new Buffer(1028)
121-
var server = createServer({ limit: 1024 })
122-
123-
buf.fill('.')
124-
125-
request(server)
112+
it('should accept number of bytes', function (done) {
113+
var buf = allocBuffer(1028, '.')
114+
request(createServer({ limit: 1024 }))
126115
.post('/')
127116
.set('Content-Type', 'text/plain')
128117
.send(buf.toString())
129118
.expect(413, done)
130119
})
131120

132-
it('should not change when options altered', function(done){
133-
var buf = new Buffer(1028)
121+
it('should not change when options altered', function (done) {
122+
var buf = allocBuffer(1028, '.')
134123
var options = { limit: '1kb' }
135124
var server = createServer(options)
136125

137-
buf.fill('.')
138126
options.limit = '100kb'
139127

140128
request(server)
@@ -144,12 +132,8 @@ describe('bodyParser.text()', function(){
144132
.expect(413, done)
145133
})
146134

147-
it('should not hang response', function(done){
148-
var buf = new Buffer(1024 * 10)
149-
var server = createServer({ limit: '1kb' })
150-
151-
buf.fill('.')
152-
135+
it('should not hang response', function (done) {
136+
var buf = allocBuffer(10240, '.')
153137
var server = createServer({ limit: '8kb' })
154138
var test = request(server).post('/')
155139
test.set('Content-Type', 'text/plain')
@@ -412,6 +396,16 @@ describe('bodyParser.text()', function(){
412396
})
413397
})
414398

399+
function allocBuffer (size, fill) {
400+
if (Buffer.alloc) {
401+
return Buffer.alloc(size, fill)
402+
}
403+
404+
var buf = new Buffer(size)
405+
buf.fill(fill)
406+
return buf
407+
}
408+
415409
function createServer(opts){
416410
var _bodyParser = typeof opts !== 'function'
417411
? bodyParser.text(opts)

test/urlencoded.js

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -235,27 +235,20 @@ describe('bodyParser.urlencoded()', function(){
235235
})
236236
})
237237

238-
describe('with limit option', function(){
239-
it('should 413 when over limit with Content-Length', function(done){
240-
var buf = new Buffer(1024)
241-
var server = createServer({ limit: '1kb' })
242-
243-
buf.fill('.')
244-
245-
request(server)
238+
describe('with limit option', function () {
239+
it('should 413 when over limit with Content-Length', function (done) {
240+
var buf = allocBuffer(1024, '.')
241+
request(createServer({ limit: '1kb' }))
246242
.post('/')
247243
.set('Content-Type', 'application/x-www-form-urlencoded')
248244
.set('Content-Length', '1028')
249245
.send('str=' + buf.toString())
250246
.expect(413, done)
251247
})
252248

253-
it('should 413 when over limit with chunked encoding', function(done){
254-
var buf = new Buffer(1024)
249+
it('should 413 when over limit with chunked encoding', function (done) {
250+
var buf = allocBuffer(1024, '.')
255251
var server = createServer({ limit: '1kb' })
256-
257-
buf.fill('.')
258-
259252
var test = request(server).post('/')
260253
test.set('Content-Type', 'application/x-www-form-urlencoded')
261254
test.set('Transfer-Encoding', 'chunked')
@@ -264,25 +257,20 @@ describe('bodyParser.urlencoded()', function(){
264257
test.expect(413, done)
265258
})
266259

267-
it('should accept number of bytes', function(done){
268-
var buf = new Buffer(1024)
269-
var server = createServer({ limit: 1024 })
270-
271-
buf.fill('.')
272-
273-
request(server)
260+
it('should accept number of bytes', function (done) {
261+
var buf = allocBuffer(1024, '.')
262+
request(createServer({ limit: 1024 }))
274263
.post('/')
275264
.set('Content-Type', 'application/x-www-form-urlencoded')
276265
.send('str=' + buf.toString())
277266
.expect(413, done)
278267
})
279268

280-
it('should not change when options altered', function(done){
281-
var buf = new Buffer(1024)
269+
it('should not change when options altered', function (done) {
270+
var buf = allocBuffer(1024, '.')
282271
var options = { limit: '1kb' }
283272
var server = createServer(options)
284273

285-
buf.fill('.')
286274
options.limit = '100kb'
287275

288276
request(server)
@@ -292,12 +280,8 @@ describe('bodyParser.urlencoded()', function(){
292280
.expect(413, done)
293281
})
294282

295-
it('should not hang response', function(done){
296-
var buf = new Buffer(1024 * 10)
297-
var server = createServer({ limit: '1kb' })
298-
299-
buf.fill('.')
300-
283+
it('should not hang response', function (done) {
284+
var buf = allocBuffer(10240, '.')
301285
var server = createServer({ limit: '8kb' })
302286
var test = request(server).post('/')
303287
test.set('Content-Type', 'application/x-www-form-urlencoded')
@@ -629,6 +613,16 @@ describe('bodyParser.urlencoded()', function(){
629613
})
630614
})
631615

616+
function allocBuffer (size, fill) {
617+
if (Buffer.alloc) {
618+
return Buffer.alloc(size, fill)
619+
}
620+
621+
var buf = new Buffer(size)
622+
buf.fill(fill)
623+
return buf
624+
}
625+
632626
function createManyParams(count) {
633627
var str = ''
634628

0 commit comments

Comments
 (0)