|
1 | | -import { deflateSync, gzipSync } from 'node:zlib'; |
| 1 | +import { deflateSync, gunzipSync, gzipSync } from 'node:zlib'; |
2 | 2 | import context from '@aws-lambda-powertools/testing-utils/context'; |
3 | 3 | import { beforeEach, describe, expect, it } from 'vitest'; |
4 | 4 | import { streamify } from '../../../../src/http/index.js'; |
@@ -155,6 +155,43 @@ describe('Compress Middleware', () => { |
155 | 155 | expect(result.isBase64Encoded).toBe(false); |
156 | 156 | }); |
157 | 157 |
|
| 158 | + it('compresses early return middleware', async () => { |
| 159 | + // Prepare |
| 160 | + const application = new Router(); |
| 161 | + const largeMwBody = { test: 'y'.repeat(2000) }; |
| 162 | + |
| 163 | + application.get( |
| 164 | + '/test', |
| 165 | + [ |
| 166 | + compress({ |
| 167 | + encoding: 'gzip', |
| 168 | + }), |
| 169 | + async () => { |
| 170 | + await 10; |
| 171 | + // return {message: 'Middleware applied'} |
| 172 | + return largeMwBody; |
| 173 | + }, |
| 174 | + ], |
| 175 | + () => { |
| 176 | + return body; |
| 177 | + } |
| 178 | + ); |
| 179 | + |
| 180 | + // Act |
| 181 | + const result = await application.resolve(event, context); |
| 182 | + |
| 183 | + // Assess |
| 184 | + expect(result.statusCode).toBe(200); |
| 185 | + expect(result.isBase64Encoded).toBe(true); |
| 186 | + expect(result.headers).toEqual({ |
| 187 | + 'content-encoding': 'gzip', |
| 188 | + 'content-type': 'application/json', |
| 189 | + }); |
| 190 | + expect( |
| 191 | + gunzipSync(Buffer.from(result.body, 'base64')).toString('utf8') |
| 192 | + ).toEqual(JSON.stringify(largeMwBody)); |
| 193 | + }); |
| 194 | + |
158 | 195 | it('uses specified encoding when provided', async () => { |
159 | 196 | // Prepare |
160 | 197 | const application = new Router(); |
|
0 commit comments