From 6afa231ec6bb30f47b19e08c7d4887637eef5d8e Mon Sep 17 00:00:00 2001 From: Julien Giovaresco Date: Thu, 17 Sep 2020 23:38:43 +0200 Subject: [PATCH] fix: don't convert header names to lowercase (#178) * test(headers): failing test for case sensitive headers * fix: convert header names to lowercase only for HTTP/2 request Closes #74 Co-authored-by: Darren Jennings --- src/index.js | 9 +++++++-- test/index.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index c943e7adb..8ec9394ac 100644 --- a/src/index.js +++ b/src/index.js @@ -73,9 +73,14 @@ HTTPSnippet.prototype.prepare = function (request) { // construct headers objects if (request.headers && request.headers.length) { - // loweCase header keys + var http2VersionRegex = /^HTTP\/2/ request.headersObj = request.headers.reduce(function (headers, header) { - headers[header.name.toLowerCase()] = header.value + var headerName = header.name + if (request.httpVersion.match(http2VersionRegex)) { + headerName = headerName.toLowerCase() + } + + headers[headerName] = header.value return headers }, {}) } diff --git a/test/index.js b/test/index.js index 6974b4d3a..29ba5cf56 100644 --- a/test/index.js +++ b/test/index.js @@ -166,6 +166,44 @@ describe('HTTPSnippet', function () { done() }) + it('should add "headersObj" to source object case insensitive when HTTP/1.0', function (done) { + var fixture = Object.assign({}, fixtures.requests.headers) + fixture.httpVersion = 'HTTP/1.1' + fixture.headers = fixture.headers.concat({ + name: 'Kong-Admin-Token', + value: 'Hunter1' + }) + + var req = new HTTPSnippet(fixture).requests[0] + req.headersObj.should.be.an.Object() + req.headersObj.should.eql({ + 'Kong-Admin-Token': 'Hunter1', + 'accept': 'application/json', + 'x-foo': 'Bar' + }) + + done() + }) + + it('should add "headersObj" to source object in lowercase when HTTP/2.x', function (done) { + var fixture = Object.assign({}, fixtures.requests.headers) + fixture.httpVersion = 'HTTP/2' + fixture.headers = fixture.headers.concat({ + name: 'Kong-Admin-Token', + value: 'Hunter1' + }) + + var req = new HTTPSnippet(fixture).requests[0] + req.headersObj.should.be.an.Object() + req.headersObj.should.eql({ + 'kong-admin-token': 'Hunter1', + 'accept': 'application/json', + 'x-foo': 'Bar' + }) + + done() + }) + it('should modify orignal url to strip query string', function (done) { var req = new HTTPSnippet(fixtures.requests.query).requests[0]