diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 7681222cf37fc6..f819c067e35a47 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -355,7 +355,8 @@ OutgoingMessage.prototype.setHeader = function(name, value) { if (this._header) throw new Error('Can\'t set headers after they are sent.'); if (common._checkInvalidHeaderChar(value) === true) { - throw new TypeError('The header content contains invalid characters'); + throw new TypeError(`The header content for ${JSON.stringify(name)} ` + + 'contains invalid characters'); } if (this._headers === null) this._headers = {}; diff --git a/test/parallel/test-http-client-invalid-header.js b/test/parallel/test-http-client-invalid-header.js new file mode 100644 index 00000000000000..610bbab7fbb229 --- /dev/null +++ b/test/parallel/test-http-client-invalid-header.js @@ -0,0 +1,14 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const http = require('http'); + +assert.throws(function() { + // Header value with CRLF should throw + http.get({ + path: '/', + headers: { + a: 'bad value\r\n' + } + }, common.fail); +}, /header content for "a" contains invalid characters/);