diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js index 19e0c095df2b52..100e08a25df92b 100644 --- a/lib/internal/http2/compat.js +++ b/lib/internal/http2/compat.js @@ -328,16 +328,12 @@ class Http2ServerResponse extends Stream { addTrailers(headers) { let trailers = this[kTrailers]; const keys = Object.keys(headers); - let key = ''; - if (keys.length > 0) + if (keys.length === 0) return; if (trailers === undefined) trailers = this[kTrailers] = Object.create(null); for (var i = 0; i < keys.length; i++) { - key = String(keys[i]).trim().toLowerCase(); - const value = headers[key]; - assertValidHeader(key, value); - trailers[key] = String(value); + trailers[keys[i]] = String(headers[keys[i]]); } } @@ -508,12 +504,16 @@ class Http2ServerResponse extends Stream { [kBeginSend](options) { const stream = this[kStream]; + options = options || Object.create(null); if (stream !== undefined && stream.headersSent === false) { const state = this[kState]; const headers = this[kHeaders] || Object.create(null); headers[constants.HTTP2_HEADER_STATUS] = state.statusCode; if (stream.finished === true) options.endStream = true; + options.getTrailers = (trailers) => { + Object.assign(trailers, this[kTrailers]); + }; if (stream.destroyed === false) { stream.respond(headers, options); } diff --git a/test/parallel/test-http2-compat-serverresponse-trailers.js b/test/parallel/test-http2-compat-serverresponse-trailers.js new file mode 100644 index 00000000000000..d1ab4f9946c01c --- /dev/null +++ b/test/parallel/test-http2-compat-serverresponse-trailers.js @@ -0,0 +1,30 @@ +// Flags: --expose-http2 +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const http2 = require('http2'); + +const server = http2.createServer(); +server.listen(0, common.mustCall(() => { + const port = server.address().port; + server.once('request', common.mustCall((request, response) => { + response.addTrailers({ + ABC: 123 + }); + response.end('hello'); + })); + + const url = `http://localhost:${port}`; + const client = http2.connect(url, common.mustCall(() => { + const request = client.request(); + request.on('trailers', common.mustCall((headers) => { + assert.strictEqual(headers.abc, '123'); + })); + request.resume(); + request.on('end', common.mustCall(() => { + client.destroy(); + server.close(); + })); + })); +}));