Skip to content

Commit abed970

Browse files
committed
Added tests
1 parent 2f1d4b8 commit abed970

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

test/compression.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ var zlib = require('zlib')
99

1010
var compression = require('..')
1111

12+
/**
13+
* @const
14+
* whether current node version has brotli support
15+
*/
16+
var hasBrotliSupport = false
17+
try {
18+
hasBrotliSupport = 'brotli' in require('process').versions
19+
} catch (ignored) {
20+
}
21+
1222
describe('compression()', function () {
1323
it('should skip HEAD', function (done) {
1424
var server = createServer({ threshold: 0 }, function (req, res) {
@@ -465,6 +475,22 @@ describe('compression()', function () {
465475
})
466476
})
467477

478+
if (hasBrotliSupport) {
479+
describe('when "Accept-Encoding: br"', function () {
480+
it('should respond with br', function (done) {
481+
var server = createServer({ threshold: 0 }, function (req, res) {
482+
res.setHeader('Content-Type', 'text/plain')
483+
res.end('hello, world')
484+
})
485+
486+
request(server)
487+
.get('/')
488+
.set('Accept-Encoding', 'br')
489+
.expect('Content-Encoding', 'br', done)
490+
})
491+
})
492+
}
493+
468494
describe('when "Accept-Encoding: gzip, deflate"', function () {
469495
it('should respond with gzip', function (done) {
470496
var server = createServer({ threshold: 0 }, function (req, res) {
@@ -493,6 +519,22 @@ describe('compression()', function () {
493519
})
494520
})
495521

522+
if (hasBrotliSupport) {
523+
describe('when "Accept-Encoding: deflate, br"', function () {
524+
it('should respond with br', function (done) {
525+
var server = createServer({ threshold: 0 }, function (req, res) {
526+
res.setHeader('Content-Type', 'text/plain')
527+
res.end('hello, world')
528+
})
529+
530+
request(server)
531+
.get('/')
532+
.set('Accept-Encoding', 'deflate, br')
533+
.expect('Content-Encoding', 'br', done)
534+
})
535+
})
536+
}
537+
496538
describe('when "Cache-Control: no-transform" response header', function () {
497539
it('should not compress response', function (done) {
498540
var server = createServer({ threshold: 0 }, function (req, res) {
@@ -631,6 +673,34 @@ describe('compression()', function () {
631673
.end()
632674
})
633675

676+
if (hasBrotliSupport) {
677+
it('should flush small chunks for brotli', function (done) {
678+
var chunks = 0
679+
var next
680+
var server = createServer({ threshold: 0 }, function (req, res) {
681+
next = writeAndFlush(res, 2, Buffer.from('..'))
682+
res.setHeader('Content-Type', 'text/plain')
683+
next()
684+
})
685+
686+
function onchunk (chunk) {
687+
assert.ok(chunks++ < 20)
688+
assert.strictEqual(chunk.toString(), '..')
689+
next()
690+
}
691+
692+
request(server)
693+
.get('/')
694+
.set('Accept-Encoding', 'br')
695+
.request()
696+
.on('response', unchunk('br', onchunk, function (err) {
697+
if (err) return done(err)
698+
server.close(done)
699+
}))
700+
.end()
701+
})
702+
}
703+
634704
it('should flush small chunks for deflate', function (done) {
635705
var chunks = 0
636706
var next
@@ -710,6 +780,9 @@ function unchunk (encoding, onchunk, onend) {
710780
case 'gzip':
711781
stream = res.pipe(zlib.createGunzip())
712782
break
783+
case 'br':
784+
stream = res.pipe(zlib.createBrotliDecompress())
785+
break
713786
}
714787

715788
stream.on('data', onchunk)

0 commit comments

Comments
 (0)