1
1
import context from '@aws-lambda-powertools/testing-utils/context' ;
2
2
import { Router } from 'src/rest/Router.js' ;
3
- import { describe , expect , it } from 'vitest' ;
3
+ import { beforeEach , describe , expect , it } from 'vitest' ;
4
4
import { compress } from '../../../../src/rest/middleware/index.js' ;
5
5
import { createSettingHeadersMiddleware , createTestEvent } from '../helpers.js' ;
6
6
7
7
describe ( 'Compress Middleware' , ( ) => {
8
+ const event = createTestEvent ( '/test' , 'GET' ) ;
9
+ let app : Router ;
10
+ const body = { test : 'x' . repeat ( 2000 ) } ;
11
+
12
+ beforeEach ( ( ) => {
13
+ app = new Router ( ) ;
14
+ app . use ( compress ( ) ) ;
15
+ app . use (
16
+ createSettingHeadersMiddleware ( {
17
+ 'content-length' : '2000' ,
18
+ } )
19
+ ) ;
20
+ } ) ;
21
+
8
22
it ( 'compresses response when conditions are met' , async ( ) => {
9
23
// Prepare
10
- const event = createTestEvent ( '/test' , 'GET' ) ;
11
- const app = new Router ( ) ;
12
- app . get (
13
- '/test' ,
14
- [
15
- compress ( ) ,
16
- createSettingHeadersMiddleware ( {
17
- 'content-length' : '2000' ,
18
- } ) ,
19
- ] ,
20
- async ( ) => {
21
- return { test : 'x' . repeat ( 2000 ) } ;
22
- }
23
- ) ;
24
+ app . get ( '/test' , async ( ) => {
25
+ return body ;
26
+ } ) ;
24
27
25
28
// Act
26
29
const result = await app . resolve ( event , context ) ;
@@ -32,9 +35,8 @@ describe('Compress Middleware', () => {
32
35
33
36
it ( 'skips compression when content is below threshold' , async ( ) => {
34
37
// Prepare
35
- const event = createTestEvent ( '/test' , 'GET' ) ;
36
- const app = new Router ( ) ;
37
- app . get (
38
+ const application = new Router ( ) ;
39
+ application . get (
38
40
'/test' ,
39
41
[
40
42
compress ( { threshold : 1024 } ) ,
@@ -48,41 +50,30 @@ describe('Compress Middleware', () => {
48
50
) ;
49
51
50
52
// Act
51
- const result = await app . resolve ( event , context ) ;
53
+ const result = await application . resolve ( event , context ) ;
52
54
53
55
// Assess
54
56
expect ( result . headers ?. [ 'content-encoding' ] ) . toBeUndefined ( ) ;
55
57
} ) ;
56
58
57
59
it ( 'skips compression for HEAD requests' , async ( ) => {
58
60
// Prepare
59
- const event = createTestEvent ( '/test' , 'HEAD' ) ;
60
- const app = new Router ( ) ;
61
- app . head (
62
- '/test' ,
63
- [
64
- compress ( ) ,
65
- createSettingHeadersMiddleware ( {
66
- 'content-length' : '2000' ,
67
- } ) ,
68
- ] ,
69
- async ( ) => {
70
- return { test : 'x' . repeat ( 2000 ) } ;
71
- }
72
- ) ;
61
+ const headEvent = createTestEvent ( '/test' , 'HEAD' ) ;
62
+ app . head ( '/test' , async ( ) => {
63
+ return body ;
64
+ } ) ;
73
65
74
66
// Act
75
- const result = await app . resolve ( event , context ) ;
67
+ const result = await app . resolve ( headEvent , context ) ;
76
68
77
69
// Assess
78
70
expect ( result . headers ?. [ 'content-encoding' ] ) . toBeUndefined ( ) ;
79
71
} ) ;
80
72
81
73
it ( 'skips compression when already encoded' , async ( ) => {
82
74
// Prepare
83
- const event = createTestEvent ( '/test' , 'GET' ) ;
84
- const app = new Router ( ) ;
85
- app . get (
75
+ const application = new Router ( ) ;
76
+ application . get (
86
77
'/test' ,
87
78
[
88
79
compress ( {
@@ -96,12 +87,12 @@ describe('Compress Middleware', () => {
96
87
} ) ,
97
88
] ,
98
89
async ( ) => {
99
- return { test : 'x' . repeat ( 2000 ) } ;
90
+ return body ;
100
91
}
101
92
) ;
102
93
103
94
// Act
104
- const result = await app . resolve ( event , context ) ;
95
+ const result = await application . resolve ( event , context ) ;
105
96
106
97
// Assess
107
98
expect ( result . headers ?. [ 'content-encoding' ] ) . toEqual ( 'gzip' ) ;
@@ -128,9 +119,8 @@ describe('Compress Middleware', () => {
128
119
'skips compression for non-compressible content types' ,
129
120
async ( contentType ) => {
130
121
// Prepare
131
- const event = createTestEvent ( '/test' , 'GET' ) ;
132
- const app = new Router ( ) ;
133
- app . get (
122
+ const application = new Router ( ) ;
123
+ application . get (
134
124
'/test' ,
135
125
[
136
126
compress ( ) ,
@@ -140,12 +130,12 @@ describe('Compress Middleware', () => {
140
130
} ) ,
141
131
] ,
142
132
async ( ) => {
143
- return { test : 'x' . repeat ( 2000 ) } ;
133
+ return body ;
144
134
}
145
135
) ;
146
136
147
137
// Act
148
- const result = await app . resolve ( event , context ) ;
138
+ const result = await application . resolve ( event , context ) ;
149
139
150
140
// Assess
151
141
expect ( result . headers ?. [ 'content-encoding' ] ) . toBeUndefined ( ) ;
@@ -154,9 +144,8 @@ describe('Compress Middleware', () => {
154
144
155
145
it ( 'skips compression when cache-control no-transform is set' , async ( ) => {
156
146
// Prepare
157
- const event = createTestEvent ( '/test' , 'GET' ) ;
158
- const app = new Router ( ) ;
159
- app . get (
147
+ const application = new Router ( ) ;
148
+ application . get (
160
149
'/test' ,
161
150
[
162
151
compress ( ) ,
@@ -166,22 +155,21 @@ describe('Compress Middleware', () => {
166
155
} ) ,
167
156
] ,
168
157
async ( ) => {
169
- return { test : 'x' . repeat ( 2000 ) } ;
158
+ return body ;
170
159
}
171
160
) ;
172
161
173
162
// Act
174
- const result = await app . resolve ( event , context ) ;
163
+ const result = await application . resolve ( event , context ) ;
175
164
176
165
// Assess
177
166
expect ( result . headers ?. [ 'content-encoding' ] ) . toBeUndefined ( ) ;
178
167
} ) ;
179
168
180
169
it ( 'uses specified encoding when provided' , async ( ) => {
181
170
// Prepare
182
- const event = createTestEvent ( '/test' , 'GET' ) ;
183
- const app = new Router ( ) ;
184
- app . get (
171
+ const application = new Router ( ) ;
172
+ application . get (
185
173
'/test' ,
186
174
[
187
175
compress ( {
@@ -192,38 +180,28 @@ describe('Compress Middleware', () => {
192
180
} ) ,
193
181
] ,
194
182
async ( ) => {
195
- return { test : 'x' . repeat ( 2000 ) } ;
183
+ return body ;
196
184
}
197
185
) ;
198
186
199
187
// Act
200
- const result = await app . resolve ( event , context ) ;
188
+ const result = await application . resolve ( event , context ) ;
201
189
202
190
// Assess
203
191
expect ( result . headers ?. [ 'content-encoding' ] ) . toBe ( 'deflate' ) ;
204
192
} ) ;
205
193
206
194
it ( 'infers encoding from Accept-Encoding header' , async ( ) => {
207
195
// Prepare
208
- const event = createTestEvent ( '/test' , 'GET' , {
196
+ const deflateCompressionEvent = createTestEvent ( '/test' , 'GET' , {
209
197
'Accept-Encoding' : 'deflate' ,
210
198
} ) ;
211
- const app = new Router ( ) ;
212
- app . get (
213
- '/test' ,
214
- [
215
- compress ( ) ,
216
- createSettingHeadersMiddleware ( {
217
- 'content-length' : '2000' ,
218
- } ) ,
219
- ] ,
220
- async ( ) => {
221
- return { test : 'x' . repeat ( 2000 ) } ;
222
- }
223
- ) ;
199
+ app . get ( '/test' , async ( ) => {
200
+ return body ;
201
+ } ) ;
224
202
225
203
// Act
226
- const result = await app . resolve ( event , context ) ;
204
+ const result = await app . resolve ( deflateCompressionEvent , context ) ;
227
205
228
206
// Assess
229
207
expect ( result . headers ?. [ 'content-encoding' ] ) . toBe ( 'deflate' ) ;
0 commit comments