Skip to content

Commit b7d5d77

Browse files
IcehunterMoritz Petersmaritzlamweilibjohansebas
authored
Bugfix/use write head instead of implicit header (#170)
* Add failing tests for http2 * Add support for http2 The change just removes the usage of undocumented http API and instead uses the proper writeHead method * Remove arrow function in tests * Conditionally run http2 tests if http2 is available * Fix port in tests to be assigned automatically * Change http2 test usage to describe.skip if no http2 available * Fix closing the http2 connections to prevent possible exceptions * Fix closing the request first, then the client, then the server * Fix closing for v8.x and v9.x * fix tests not draining data for http2 requests, resulting in timeouts in node v10.4 onwards * fix: 🐛 assert.equal error * fix: 🐛 remove console.log's and timeout, let build fail * Apply suggestions from code review Co-authored-by: Lam Wei Li <[email protected]> * fixed lint * fix: an issue where test hangs when assertion fails in http2 as http2server is not closed * refactor: use http2.constants instead of hard-coded strings in http2 test * Node.js 0.8 compatible * fix lint issue --------- Co-authored-by: Moritz Peters <[email protected]> Co-authored-by: Moritz Peters <[email protected]> Co-authored-by: Lam Wei Li <[email protected]> Co-authored-by: Sebastian Beltran <[email protected]>
1 parent 8e5641c commit b7d5d77

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function compression (options) {
8181
}
8282

8383
if (!headersSent(res)) {
84-
this._implicitHeader()
84+
this.writeHead(this.statusCode)
8585
}
8686

8787
return stream
@@ -100,7 +100,7 @@ function compression (options) {
100100
length = chunkLength(chunk, encoding)
101101
}
102102

103-
this._implicitHeader()
103+
this.writeHead(this.statusCode)
104104
}
105105

106106
if (!stream) {

test/compression.js

+86
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ var http = require('http')
77
var request = require('supertest')
88
var zlib = require('zlib')
99

10+
var describeHttp2 = describe.skip
11+
try {
12+
var http2 = require('http2')
13+
describeHttp2 = describe
14+
} catch (err) {
15+
if (err) {
16+
console.log('http2 tests disabled.')
17+
}
18+
}
19+
1020
var compression = require('..')
1121

1222
describe('compression()', function () {
@@ -305,6 +315,41 @@ describe('compression()', function () {
305315
.expect(200, done)
306316
})
307317

318+
describeHttp2('http2', function () {
319+
it('should work with http2 server', function (done) {
320+
var server = createHttp2Server({ threshold: 0 }, function (req, res) {
321+
res.setHeader(http2.constants.HTTP2_HEADER_CONTENT_TYPE, 'text/plain')
322+
res.end('hello, world')
323+
})
324+
server.on('listening', function () {
325+
var client = createHttp2Client(server.address().port)
326+
// using ES5 as Node.js <=4.0.0 does not have Computed Property Names
327+
var reqHeaders = {}
328+
reqHeaders[http2.constants.HTTP2_HEADER_ACCEPT_ENCODING] = 'gzip'
329+
var request = client.request(reqHeaders)
330+
request.on('response', function (headers) {
331+
assert.strictEqual(headers[http2.constants.HTTP2_HEADER_STATUS], 200)
332+
assert.strictEqual(headers[http2.constants.HTTP2_HEADER_CONTENT_TYPE], 'text/plain')
333+
assert.strictEqual(headers[http2.constants.HTTP2_HEADER_CONTENT_ENCODING], 'gzip')
334+
})
335+
var chunks = []
336+
request.on('data', function (chunk) {
337+
chunks.push(chunk)
338+
})
339+
request.on('end', function () {
340+
closeHttp2(client, server, function () {
341+
zlib.gunzip(Buffer.concat(chunks), function (err, data) {
342+
assert.ok(!err)
343+
assert.strictEqual(data.toString(), 'hello, world')
344+
done()
345+
})
346+
})
347+
})
348+
request.end()
349+
})
350+
})
351+
})
352+
308353
describe('threshold', function () {
309354
it('should not compress responses below the threshold size', function (done) {
310355
var server = createServer({ threshold: '1kb' }, function (req, res) {
@@ -674,6 +719,47 @@ function createServer (opts, fn) {
674719
})
675720
}
676721

722+
function createHttp2Server (opts, fn) {
723+
var _compression = compression(opts)
724+
var server = http2.createServer(function (req, res) {
725+
_compression(req, res, function (err) {
726+
if (err) {
727+
res.statusCode = err.status || 500
728+
res.end(err.message)
729+
return
730+
}
731+
732+
fn(req, res)
733+
})
734+
})
735+
server.listen(0, '127.0.0.1')
736+
return server
737+
}
738+
739+
function createHttp2Client (port) {
740+
return http2.connect('http://127.0.0.1:' + port)
741+
}
742+
743+
function closeHttp2 (client, server, callback) {
744+
if (typeof client.shutdown === 'function') {
745+
// this is the node v8.x way of closing the connections
746+
client.shutdown({}, function () {
747+
server.close(function () {
748+
callback()
749+
})
750+
})
751+
} else {
752+
// this is the node v9.x onwards way of closing the connections
753+
client.close(function () {
754+
// force existing connections to time out after 1ms.
755+
// this is done to force the server to close in some cases where it wouldn't do it otherwise.
756+
server.close(function () {
757+
callback()
758+
})
759+
})
760+
}
761+
}
762+
677763
function shouldHaveBodyLength (length) {
678764
return function (res) {
679765
assert.strictEqual(res.text.length, length, 'should have body length of ' + length)

0 commit comments

Comments
 (0)