From f1f9f7bd20c95bf6abefb124d5d8794766aed2cd Mon Sep 17 00:00:00 2001 From: Evan Lucas Date: Mon, 10 Oct 2016 12:05:57 -0500 Subject: [PATCH] http: improve invalid header character error This commit includes the header name in the error message when invalid characters are in the value. --- lib/_http_outgoing.js | 3 ++- test/parallel/test-http-client-invalid-header.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-http-client-invalid-header.js 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/);