Skip to content

Commit f227a28

Browse files
committed
add test for compressing early return middleware
1 parent 728a44f commit f227a28

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

packages/event-handler/tests/unit/http/middleware/compress.test.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { deflateSync, gzipSync } from 'node:zlib';
1+
import { deflateSync, gunzipSync, gzipSync } from 'node:zlib';
22
import context from '@aws-lambda-powertools/testing-utils/context';
33
import { beforeEach, describe, expect, it } from 'vitest';
44
import { streamify } from '../../../../src/http/index.js';
@@ -155,6 +155,43 @@ describe('Compress Middleware', () => {
155155
expect(result.isBase64Encoded).toBe(false);
156156
});
157157

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+
158195
it('uses specified encoding when provided', async () => {
159196
// Prepare
160197
const application = new Router();

0 commit comments

Comments
 (0)