Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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