1
1
import context from '@aws-lambda-powertools/testing-utils/context' ;
2
2
import { Router } from 'src/rest/Router.js' ;
3
3
import type { Middleware } from 'src/types/index.js' ;
4
- import { describe , expect , it } from 'vitest' ;
4
+ import { beforeEach , describe , expect , it } from 'vitest' ;
5
5
import { compress } from '../../../../src/rest/middleware/index.js' ;
6
- import { createTestEvent } from '../helpers.js' ;
6
+ import { createSettingHeadersMiddleware , createTestEvent } from '../helpers.js' ;
7
7
8
8
describe ( 'Compress Middleware' , ( ) => {
9
9
it ( 'compresses response when conditions are met' , async ( ) => {
10
10
// Prepare
11
11
const event = createTestEvent ( '/test' , 'GET' ) ;
12
12
const app = new Router ( ) ;
13
- app . get ( '/test' , [ compress ( ) ] , async ( ) => {
14
- return { test : 'x' . repeat ( 2000 ) } ;
15
- } ) ;
13
+ app . get (
14
+ '/test' ,
15
+ [
16
+ compress ( ) ,
17
+ createSettingHeadersMiddleware ( {
18
+ 'content-length' : '2000' ,
19
+ } ) ,
20
+ ] ,
21
+ async ( ) => {
22
+ return { test : 'x' . repeat ( 2000 ) } ;
23
+ }
24
+ ) ;
16
25
17
26
// Act
18
27
const result = await app . resolve ( event , context ) ;
@@ -30,10 +39,9 @@ describe('Compress Middleware', () => {
30
39
'/test' ,
31
40
[
32
41
compress ( { threshold : 1024 } ) ,
33
- ( ) : Middleware => async ( _ , reqCtx , next ) => {
34
- await next ( ) ;
35
- reqCtx . res . headers . set ( 'content-length' , '1' ) ;
36
- } ,
42
+ createSettingHeadersMiddleware ( {
43
+ 'content-length' : '1' ,
44
+ } ) ,
37
45
] ,
38
46
async ( ) => {
39
47
return { test : 'x' } ;
@@ -51,9 +59,18 @@ describe('Compress Middleware', () => {
51
59
// Prepare
52
60
const event = createTestEvent ( '/test' , 'HEAD' ) ;
53
61
const app = new Router ( ) ;
54
- app . head ( '/test' , [ compress ( ) ] , async ( ) => {
55
- return { test : 'x' . repeat ( 2000 ) } ;
56
- } ) ;
62
+ app . head (
63
+ '/test' ,
64
+ [
65
+ compress ( ) ,
66
+ createSettingHeadersMiddleware ( {
67
+ 'content-length' : '2000' ,
68
+ } ) ,
69
+ ] ,
70
+ async ( ) => {
71
+ return { test : 'x' . repeat ( 2000 ) } ;
72
+ }
73
+ ) ;
57
74
58
75
// Act
59
76
const result = await app . resolve ( event , context ) ;
@@ -75,6 +92,9 @@ describe('Compress Middleware', () => {
75
92
compress ( {
76
93
encoding : 'gzip' ,
77
94
} ) ,
95
+ createSettingHeadersMiddleware ( {
96
+ 'content-length' : '2000' ,
97
+ } ) ,
78
98
] ,
79
99
async ( ) => {
80
100
return { test : 'x' . repeat ( 2000 ) } ;
@@ -96,13 +116,13 @@ describe('Compress Middleware', () => {
96
116
'/test' ,
97
117
[
98
118
compress ( ) ,
99
- ( ) : Middleware => async ( _ , reqCtx , next ) => {
100
- await next ( ) ;
101
- reqCtx . res . headers . set ( 'content-type' , 'image/jpeg' ) ;
102
- } ,
119
+ createSettingHeadersMiddleware ( {
120
+ 'content-length' : '2000' ,
121
+ 'content-type' : 'image/jpeg' ,
122
+ } ) ,
103
123
] ,
104
124
async ( ) => {
105
- return { } ;
125
+ return { test : 'x' . repeat ( 2000 ) } ;
106
126
}
107
127
) ;
108
128
@@ -121,13 +141,13 @@ describe('Compress Middleware', () => {
121
141
'/test' ,
122
142
[
123
143
compress ( ) ,
124
- ( ) : Middleware => async ( _ , reqCtx , next ) => {
125
- await next ( ) ;
126
- reqCtx . res . headers . set ( 'cache-control' , 'no-transform' ) ;
127
- } ,
144
+ createSettingHeadersMiddleware ( {
145
+ 'content-length' : '2000' ,
146
+ 'cache-control' : 'no-transform' ,
147
+ } ) ,
128
148
] ,
129
149
async ( ) => {
130
- return { } ;
150
+ return { test : 'x' . repeat ( 2000 ) } ;
131
151
}
132
152
) ;
133
153
@@ -148,6 +168,9 @@ describe('Compress Middleware', () => {
148
168
compress ( {
149
169
encoding : 'deflate' ,
150
170
} ) ,
171
+ createSettingHeadersMiddleware ( {
172
+ 'content-length' : '2000' ,
173
+ } ) ,
151
174
] ,
152
175
async ( ) => {
153
176
return { test : 'x' . repeat ( 2000 ) } ;
@@ -167,9 +190,18 @@ describe('Compress Middleware', () => {
167
190
'Accept-Encoding' : 'deflate' ,
168
191
} ) ;
169
192
const app = new Router ( ) ;
170
- app . get ( '/test' , [ compress ( ) ] , async ( ) => {
171
- return { test : 'x' . repeat ( 2000 ) } ;
172
- } ) ;
193
+ app . get (
194
+ '/test' ,
195
+ [
196
+ compress ( ) ,
197
+ createSettingHeadersMiddleware ( {
198
+ 'content-length' : '2000' ,
199
+ } ) ,
200
+ ] ,
201
+ async ( ) => {
202
+ return { test : 'x' . repeat ( 2000 ) } ;
203
+ }
204
+ ) ;
173
205
174
206
// Act
175
207
const result = await app . resolve ( event , context ) ;
0 commit comments