Skip to content

Commit

Permalink
fix: don't convert header names to lowercase (#178)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
jgiovaresco and darrenjennings authored Sep 17, 2020
1 parent 7da7657 commit 6afa231
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}, {})
}
Expand Down
38 changes: 38 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down

0 comments on commit 6afa231

Please sign in to comment.