|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| - |
4 | 3 | const sinon = require('sinon') // Require Sinon.js library
|
5 | 4 | const AWS = require('aws-sdk') // AWS SDK (automatically available in Lambda)
|
6 | 5 | const S3 = require('../lib/s3-service') // Init S3 Service
|
7 |
| -const { gzipSync } = require('zlib') |
| 6 | +const { gzipSync, brotliCompressSync, deflateSync } = require('zlib') |
8 | 7 |
|
9 | 8 | // Init API instance
|
10 | 9 | const api = require('../index')({ version: 'v1.0' })
|
@@ -129,7 +128,7 @@ api4.get('/testGZIP', function(req,res) {
|
129 | 128 | res.json({ object: true })
|
130 | 129 | })
|
131 | 130 |
|
132 |
| -api5.get('/testGZIP', function(req,res) { |
| 131 | +api5.get('/testCompression', function(req,res) { |
133 | 132 | res.json({ object: true })
|
134 | 133 | })
|
135 | 134 |
|
@@ -285,13 +284,32 @@ describe('Response Tests:', function() {
|
285 | 284 | it('Custom serializer (GZIP)', async function() {
|
286 | 285 | let _event = Object.assign({},event,{ path: '/testGZIP'})
|
287 | 286 | let result = await new Promise(r => api4.run(_event,{},(e,res) => { r(res) }))
|
288 |
| - expect(result).toEqual({ multiValueHeaders: { 'content-encoding': ['gzip'], 'content-type': ['application/json'] }, statusCode: 200, body: 'H4sIAAAAAAAAE6tWyk/KSk0uUbIqKSpN1VGKTy4tLsnPhXOTEotTzUwg3FoAan86iy0AAAA=', isBase64Encoded: true }) |
| 287 | + |
| 288 | + let body = gzipSync(`{"object":true,"_custom":true,"_base64":true}`).toString('base64') |
| 289 | + expect(result).toEqual({ multiValueHeaders: { 'content-encoding': ['gzip'], 'content-type': ['application/json'] }, statusCode: 200, body, isBase64Encoded: true }) |
289 | 290 | }) // end it
|
290 | 291 |
|
291 | 292 | it('Compression (GZIP)', async function() {
|
292 |
| - let _event = Object.assign({},event,{ path: '/testGZIP'}) |
| 293 | + let _event = Object.assign({},event,{ path: '/testCompression'}) |
| 294 | + let result = await new Promise(r => api5.run(_event,{},(e,res) => { r(res) })) |
| 295 | + let body = gzipSync(`{"object":true}`).toString('base64') |
| 296 | + expect(result).toEqual({ multiValueHeaders: { 'content-encoding': ['gzip'], 'content-type': ['application/json'] }, statusCode: 200, body, isBase64Encoded: true }) |
| 297 | + }) // end it |
| 298 | + |
| 299 | + it('Compression (Brotli)', async function() { |
| 300 | + let _event = Object.assign({},event,{ path: '/testCompression'}) |
| 301 | + _event.multiValueHeaders['Accept-Encoding'] = ['br','deflate','gzip'] |
| 302 | + let result = await new Promise(r => api5.run(_event,{},(e,res) => { r(res) })) |
| 303 | + let body = brotliCompressSync(`{"object":true}`).toString('base64') |
| 304 | + expect(result).toEqual({ multiValueHeaders: { 'content-encoding': ['br'], 'content-type': ['application/json'] }, statusCode: 200, body, isBase64Encoded: true }) |
| 305 | + }) // end it |
| 306 | + |
| 307 | + it('Compression (Deflate)', async function() { |
| 308 | + let _event = Object.assign({},event,{ path: '/testCompression'}) |
| 309 | + _event.multiValueHeaders['Accept-Encoding'] = ['deflate'] |
293 | 310 | let result = await new Promise(r => api5.run(_event,{},(e,res) => { r(res) }))
|
294 |
| - expect(result).toEqual({ multiValueHeaders: { 'content-encoding': ['gzip'], 'content-type': ['application/json'] }, statusCode: 200, body: 'H4sIAAAAAAAAE6tWyk/KSk0uUbIqKSpNrQUAAQd5Ug8AAAA=', isBase64Encoded: true }) |
| 311 | + let body = deflateSync(`{"object":true}`).toString('base64') |
| 312 | + expect(result).toEqual({ multiValueHeaders: { 'content-encoding': ['deflate'], 'content-type': ['application/json'] }, statusCode: 200, body, isBase64Encoded: true }) |
295 | 313 | }) // end it
|
296 | 314 |
|
297 | 315 | afterEach(function() {
|
|
0 commit comments