Skip to content

Commit 5dbc0cd

Browse files
committed
Added tests
1 parent 28c7cf6 commit 5dbc0cd

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

test/compression.js

Lines changed: 67 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,20 @@ describe('compression()', function () {
465475
})
466476
})
467477

478+
describe('when "Accept-Encoding: br"', function () {
479+
it('should respond with br', function (done) {
480+
var server = createServer({ threshold: 0 }, function (req, res) {
481+
res.setHeader('Content-Type', 'text/plain')
482+
res.end('hello, world')
483+
})
484+
485+
request(server)
486+
.get('/')
487+
.set('Accept-Encoding', 'br')
488+
.expect('Content-Encoding', 'br', done)
489+
})
490+
})
491+
468492
describe('when "Accept-Encoding: gzip, deflate"', function () {
469493
it('should respond with gzip', function (done) {
470494
var server = createServer({ threshold: 0 }, function (req, res) {
@@ -493,6 +517,20 @@ describe('compression()', function () {
493517
})
494518
})
495519

520+
describe('when "Accept-Encoding: deflate, br"', function () {
521+
it('should respond with br', function (done) {
522+
var server = createServer({ threshold: 0 }, function (req, res) {
523+
res.setHeader('Content-Type', 'text/plain')
524+
res.end('hello, world')
525+
})
526+
527+
request(server)
528+
.get('/')
529+
.set('Accept-Encoding', 'deflate, br')
530+
.expect('Content-Encoding', 'br', done)
531+
})
532+
})
533+
496534
describe('when "Cache-Control: no-transform" response header', function () {
497535
it('should not compress response', function (done) {
498536
var server = createServer({ threshold: 0 }, function (req, res) {
@@ -631,6 +669,32 @@ describe('compression()', function () {
631669
.end()
632670
})
633671

672+
it('should flush small chunks for br', function (done) {
673+
var chunks = 0
674+
var next
675+
var server = createServer({ threshold: 0 }, function (req, res) {
676+
next = writeAndFlush(res, 2, Buffer.from('..'))
677+
res.setHeader('Content-Type', 'text/plain')
678+
next()
679+
})
680+
681+
function onchunk (chunk) {
682+
assert.ok(chunks++ < 20)
683+
assert.strictEqual(chunk.toString(), '..')
684+
next()
685+
}
686+
687+
request(server)
688+
.get('/')
689+
.set('Accept-Encoding', 'br')
690+
.request()
691+
.on('response', unchunk('br', onchunk, function (err) {
692+
if (err) return done(err)
693+
server.close(done)
694+
}))
695+
.end()
696+
})
697+
634698
it('should flush small chunks for deflate', function (done) {
635699
var chunks = 0
636700
var next
@@ -710,6 +774,9 @@ function unchunk (encoding, onchunk, onend) {
710774
case 'gzip':
711775
stream = res.pipe(zlib.createGunzip())
712776
break
777+
case 'br':
778+
stream = res.pipe(zlib.createBrotliDecompress())
779+
break
713780
}
714781

715782
stream.on('data', onchunk)

0 commit comments

Comments
 (0)