Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement fixes and tests for Node http2 #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function compression (options) {
}

if (!this._header) {
this._implicitHeader()
this.writeHead(this.statusCode)
}

return stream
Expand All @@ -127,7 +127,7 @@ function compression (options) {
length = chunkLength(chunk, encoding)
}

this._implicitHeader()
this.writeHead(this.statusCode)
}

if (!stream) {
Expand Down
74 changes: 74 additions & 0 deletions test/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ var request = require('supertest')

var compression = require('..')

var describeHttp2 = describe.skip
try {
var http2 = require('http2')
describeHttp2 = describe
} catch (err) {
if (err) {
console.log('http2 tests disabled.')
}
}

describe('compression()', function () {
it('should skip HEAD', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
Expand Down Expand Up @@ -296,6 +306,26 @@ describe('compression()', function () {
.expect(200, done)
})

describeHttp2('http2', function () {
it('should work with http2 server', function (done) {
var server = createHttp2Server({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})
server.on('listening', function () {
var client = createHttp2Client(server.address().port)
var request = client.request({
'Accept-Encoding': 'gzip'
})
request.on('response', function (headers) {
assert.equal(headers['content-encoding'], 'gzip')
closeHttp2(request, client, server, done)
})
request.end()
})
})
})

describe('threshold', function () {
it('should not compress responses below the threshold size', function (done) {
var server = createServer({ threshold: '1kb' }, function (req, res) {
Expand Down Expand Up @@ -890,6 +920,50 @@ function createServer (opts, fn) {
})
}

function createHttp2Server (opts, fn) {
var _compression = compression(opts)
var server = http2.createServer(function (req, res) {
_compression(req, res, function (err) {
if (err) {
res.statusCode = err.status || 500
res.end(err.message)
return
}

fn(req, res)
})
})
server.listen(0, '127.0.0.1')
return server
}

function createHttp2Client (port) {
var client = http2.connect('http://127.0.0.1:' + port)
return client
}

function closeHttp2 (request, client, server, callback) {
if (typeof client.shutdown === 'function') {
// this is the node v8.x way of closing the connections
request.destroy(http2.constants.NGHTTP2_NO_ERROR, function () {
client.shutdown({}, function () {
server.close(function () {
callback()
})
})
})
} else {
// this is the node v9.x onwards (hopefully) way of closing the connections
request.close(http2.constants.NGHTTP2_NO_ERROR, function () {
client.close(function () {
server.close(function () {
callback()
})
})
})
}
}

function shouldHaveBodyLength (length) {
return function (res) {
assert.equal(res.text.length, length, 'should have body length of ' + length)
Expand Down