diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index ea3d7e62556212..169b0c8112e498 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -359,7 +359,7 @@ OutgoingMessage.prototype._send = function _send(data, encoding, callback) { // `this._header` can be null if OutgoingMessage is used without a proper Socket // See: /test/parallel/test-http-outgoing-message-inheritance.js if (typeof data === 'string' && - (encoding === 'utf8' || encoding === 'latin1' || !encoding)) { + (encoding === 'utf8' || encoding === 'latin1')) { data = this._header + data; } else { const header = this._header; diff --git a/test/parallel/test-http-server-non-utf8-header.js b/test/parallel/test-http-server-non-utf8-header.js new file mode 100644 index 00000000000000..f6c0a7e7506946 --- /dev/null +++ b/test/parallel/test-http-server-non-utf8-header.js @@ -0,0 +1,48 @@ +'use strict'; +const common = require('../common'); +const http = require('http'); +const assert = require('assert'); + +const nonUtf8Header = 'bår'; +const nonUtf8ToLatin1 = Buffer.from(nonUtf8Header).toString('latin1'); + +{ + const server = http.createServer(common.mustCall((req, res) => { + res.writeHead(200, [ + 'foo', + Buffer.from(nonUtf8Header).toString('binary'), + ]); + res.end('hello'); + })); + + server.listen(0, common.mustCall(() => { + http.get({ port: server.address().port }, (res) => { + assert.strictEqual(res.statusCode, 200); + assert.strictEqual(res.headers.foo, nonUtf8ToLatin1); + res.resume().on('end', common.mustCall(() => { + server.close(); + })); + }); + })); +} + +{ + const server = http.createServer(common.mustCall((req, res) => { + res.writeHead(200, [ + 'Content-Length', '5', + 'foo', + Buffer.from(nonUtf8Header).toString('binary'), + ]); + res.end('hello'); + })); + + server.listen(0, common.mustCall(() => { + http.get({ port: server.address().port }, (res) => { + assert.strictEqual(res.statusCode, 200); + assert.strictEqual(res.headers.foo, nonUtf8ToLatin1); + res.resume().on('end', common.mustCall(() => { + server.close(); + })); + }); + })); +} diff --git a/test/parallel/test-http-server-response-standalone.js b/test/parallel/test-http-server-response-standalone.js index ec6d1e89e38525..68f037b3e3a9a6 100644 --- a/test/parallel/test-http-server-response-standalone.js +++ b/test/parallel/test-http-server-response-standalone.js @@ -20,13 +20,11 @@ let firstChunk = true; const ws = new Writable({ write: common.mustCall((chunk, encoding, callback) => { if (firstChunk) { - assert(chunk.toString().endsWith('hello world')); + assert(chunk.toString().startsWith('HTTP/1.1 200 OK')); firstChunk = false; - } else { - assert.strictEqual(chunk.length, 0); } setImmediate(callback); - }, 2) + }, 3) }); res.assignSocket(ws);