Skip to content

Commit 4bd00d3

Browse files
committed
tests: fix flush tests to validate chunks
1 parent b980b94 commit 4bd00d3

File tree

1 file changed

+56
-51
lines changed

1 file changed

+56
-51
lines changed

test/compression.js

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var bytes = require('bytes')
33
var crypto = require('crypto')
44
var http = require('http')
55
var request = require('supertest')
6+
var zlib = require('zlib')
67

78
var compression = require('..')
89

@@ -581,98 +582,71 @@ describe('compression()', function () {
581582

582583
it('should flush the response', function (done) {
583584
var chunks = 0
584-
var resp
585+
var next
585586
var server = createServer({ threshold: 0 }, function (req, res) {
586-
resp = res
587+
next = writeAndFlush(res, 2, new Buffer(1024))
587588
res.setHeader('Content-Type', 'text/plain')
588589
res.setHeader('Content-Length', '2048')
589-
write()
590+
next()
590591
})
591592

592-
function write () {
593-
chunks++
594-
if (chunks === 2) return resp.end()
595-
if (chunks > 2) return chunks--
596-
resp.write(new Buffer(1024))
597-
resp.flush()
593+
function onchunk (chunk) {
594+
assert.ok(chunks++ < 2)
595+
assert.equal(chunk.length, 1024)
596+
next()
598597
}
599598

600599
request(server)
601600
.get('/')
602601
.set('Accept-Encoding', 'gzip')
603602
.request()
604-
.on('response', function (res) {
605-
assert.equal(res.headers['content-encoding'], 'gzip')
606-
res.on('data', write)
607-
res.on('end', function () {
608-
assert.equal(chunks, 2)
609-
done()
610-
})
611-
})
603+
.on('response', unchunk('gzip', onchunk, done))
612604
.end()
613605
})
614606

615607
it('should flush small chunks for gzip', function (done) {
616608
var chunks = 0
617-
var resp
609+
var next
618610
var server = createServer({ threshold: 0 }, function (req, res) {
619-
resp = res
611+
next = writeAndFlush(res, 2, new Buffer('..'))
620612
res.setHeader('Content-Type', 'text/plain')
621-
write()
613+
next()
622614
})
623615

624-
function write () {
625-
chunks++
626-
if (chunks === 20) return resp.end()
627-
if (chunks > 20) return chunks--
628-
resp.write('..')
629-
resp.flush()
616+
function onchunk (chunk) {
617+
assert.ok(chunks++ < 20)
618+
assert.equal(chunk.toString(), '..')
619+
next()
630620
}
631621

632622
request(server)
633623
.get('/')
634624
.set('Accept-Encoding', 'gzip')
635625
.request()
636-
.on('response', function (res) {
637-
assert.equal(res.headers['content-encoding'], 'gzip')
638-
res.on('data', write)
639-
res.on('end', function () {
640-
assert.equal(chunks, 20)
641-
done()
642-
})
643-
})
626+
.on('response', unchunk('gzip', onchunk, done))
644627
.end()
645628
})
646629

647630
it('should flush small chunks for deflate', function (done) {
648631
var chunks = 0
649-
var resp
632+
var next
650633
var server = createServer({ threshold: 0 }, function (req, res) {
651-
resp = res
634+
next = writeAndFlush(res, 2, new Buffer('..'))
652635
res.setHeader('Content-Type', 'text/plain')
653-
write()
636+
next()
654637
})
655638

656-
function write () {
657-
chunks++
658-
if (chunks === 20) return resp.end()
659-
if (chunks > 20) return chunks--
660-
resp.write('..')
661-
resp.flush()
639+
function onchunk (chunk) {
640+
assert.ok(chunks++ < 20)
641+
assert.equal(chunk.toString(), '..')
642+
next()
662643
}
663644

664645
request(server)
665646
.get('/')
666647
.set('Accept-Encoding', 'deflate')
667648
.request()
668-
.on('response', function (res) {
669-
assert.equal(res.headers['content-encoding'], 'deflate')
670-
res.on('data', write)
671-
res.on('end', function () {
672-
assert.equal(chunks, 20)
673-
done()
674-
})
675-
})
649+
.on('response', unchunk('deflate', onchunk, done))
676650
.end()
677651
})
678652
})
@@ -704,3 +678,34 @@ function shouldNotHaveHeader (header) {
704678
assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header)
705679
}
706680
}
681+
682+
function writeAndFlush (stream, count, buf) {
683+
var writes = 0
684+
685+
return function () {
686+
if (writes++ >= count) return
687+
if (writes === count) return stream.end(buf)
688+
stream.write(buf)
689+
stream.flush()
690+
}
691+
}
692+
693+
function unchunk (encoding, onchunk, onend) {
694+
return function (res) {
695+
var stream
696+
697+
assert.equal(res.headers['content-encoding'], encoding)
698+
699+
switch (encoding) {
700+
case 'deflate':
701+
stream = res.pipe(zlib.createInflate())
702+
break
703+
case 'gzip':
704+
stream = res.pipe(zlib.createGunzip())
705+
break
706+
}
707+
708+
stream.on('data', onchunk)
709+
stream.on('end', onend)
710+
}
711+
}

0 commit comments

Comments
 (0)