Skip to content

Commit

Permalink
Cleanup content-encoding response header
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBarba committed Mar 16, 2021
1 parent 3c8a548 commit 9c79aae
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const zlib = require('zlib')

exports.compress = (input,headers) => {
const acceptEncodingHeader = headers['accept-encoding'] || ''
const acceptableEncodings = new Set(acceptEncodingHeader.split(',').map(str => str.trim()))
const acceptableEncodings = new Set(acceptEncodingHeader.toLowerCase().split(',').map(str => str.trim()))

// Handle Brotli compression
if (acceptableEncodings.has('br')) {
Expand Down
9 changes: 6 additions & 3 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,12 @@ class RESPONSE {
if (this._compression && this._response.body) {
const { data, contentEncoding } = compression.compress(this._response.body, this._request.headers)
if (contentEncoding) {
this._response.body = data.toString('base64')
this._response.isBase64Encoded = true
this._response.headers['content-encoding'] = [contentEncoding]
Object.assign(this._response, { body: data.toString('base64'), isBase64Encoded: true })
if (this._response.multiValueHeaders) {
this._response.multiValueHeaders['content-encoding'] = [contentEncoding]
} else {
this._response.headers['content-encoding'] = [contentEncoding]
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ const api4 = require('../index')({
return gzipSync(json).toString('base64')
}
})
// Init API with compression
const api5 = require('../index')({
version: 'v1.0',
compression: true
})

let event = {
httpMethod: 'get',
path: '/test',
body: {},
multiValueHeaders: {
'Accept-Encoding': ['deflate, gzip'],
'Content-Type': ['application/json']
}
}
Expand Down Expand Up @@ -123,6 +129,10 @@ api4.get('/testGZIP', function(req,res) {
res.json({ object: true })
})

api5.get('/testGZIP', function(req,res) {
res.json({ object: true })
})

/******************************************************************************/
/*** BEGIN TESTS ***/
/******************************************************************************/
Expand Down Expand Up @@ -278,6 +288,12 @@ describe('Response Tests:', function() {
expect(result).to.deep.equal({ multiValueHeaders: { 'content-encoding': ['gzip'], 'content-type': ['application/json'] }, statusCode: 200, body: 'H4sIAAAAAAAAE6tWyk/KSk0uUbIqKSpN1VGKTy4tLsnPhXOTEotTzUwg3FoAan86iy0AAAA=', isBase64Encoded: true })
}) // end it

it('Compression (GZIP)', async function() {
let _event = Object.assign({},event,{ path: '/testGZIP'})
let result = await new Promise(r => api5.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({ multiValueHeaders: { 'content-encoding': ['gzip'], 'content-type': ['application/json'] }, statusCode: 200, body: 'H4sIAAAAAAAAE6tWyk/KSk0uUbIqKSpNrQUAAQd5Ug8AAAA=', isBase64Encoded: true })
}) // end it

after(function() {
stub.restore()
})
Expand Down

0 comments on commit 9c79aae

Please sign in to comment.