@@ -144,11 +144,14 @@ describe('headers', () => {
144
144
} )
145
145
146
146
describe ( 'setCacheControlHeaders' , ( ) => {
147
+ const defaultUrl = 'https://example.com'
148
+
147
149
test ( 'should not set any headers if "cache-control" is not set' , ( ) => {
148
150
const headers = new Headers ( )
151
+ const request = new Request ( defaultUrl )
149
152
vi . spyOn ( headers , 'set' )
150
153
151
- setCacheControlHeaders ( headers )
154
+ setCacheControlHeaders ( headers , request )
152
155
153
156
expect ( headers . set ) . toHaveBeenCalledTimes ( 0 )
154
157
} )
@@ -159,9 +162,10 @@ describe('headers', () => {
159
162
'cdn-cache-control' : 'public, max-age=0, must-revalidate' ,
160
163
}
161
164
const headers = new Headers ( givenHeaders )
165
+ const request = new Request ( defaultUrl )
162
166
vi . spyOn ( headers , 'set' )
163
167
164
- setCacheControlHeaders ( headers )
168
+ setCacheControlHeaders ( headers , request )
165
169
166
170
expect ( headers . set ) . toHaveBeenCalledTimes ( 0 )
167
171
} )
@@ -172,21 +176,23 @@ describe('headers', () => {
172
176
'netlify-cdn-cache-control' : 'public, max-age=0, must-revalidate' ,
173
177
}
174
178
const headers = new Headers ( givenHeaders )
179
+ const request = new Request ( defaultUrl )
175
180
vi . spyOn ( headers , 'set' )
176
181
177
- setCacheControlHeaders ( headers )
182
+ setCacheControlHeaders ( headers , request )
178
183
179
184
expect ( headers . set ) . toHaveBeenCalledTimes ( 0 )
180
185
} )
181
186
182
- test ( 'should set expected headers if "cache-control" is set and "cdn-cache-control" and "netlify-cdn-cache-control" are not present' , ( ) => {
187
+ test ( 'should set expected headers if "cache-control" is set and "cdn-cache-control" and "netlify-cdn-cache-control" are not present (GET request) ' , ( ) => {
183
188
const givenHeaders = {
184
189
'cache-control' : 'public, max-age=0, must-revalidate' ,
185
190
}
186
191
const headers = new Headers ( givenHeaders )
192
+ const request = new Request ( defaultUrl )
187
193
vi . spyOn ( headers , 'set' )
188
194
189
- setCacheControlHeaders ( headers )
195
+ setCacheControlHeaders ( headers , request )
190
196
191
197
expect ( headers . set ) . toHaveBeenNthCalledWith (
192
198
1 ,
@@ -200,14 +206,50 @@ describe('headers', () => {
200
206
)
201
207
} )
202
208
209
+ test ( 'should set expected headers if "cache-control" is set and "cdn-cache-control" and "netlify-cdn-cache-control" are not present (HEAD request)' , ( ) => {
210
+ const givenHeaders = {
211
+ 'cache-control' : 'public, max-age=0, must-revalidate' ,
212
+ }
213
+ const headers = new Headers ( givenHeaders )
214
+ const request = new Request ( defaultUrl , { method : 'HEAD' } )
215
+ vi . spyOn ( headers , 'set' )
216
+
217
+ setCacheControlHeaders ( headers , request )
218
+
219
+ expect ( headers . set ) . toHaveBeenNthCalledWith (
220
+ 1 ,
221
+ 'cache-control' ,
222
+ 'public, max-age=0, must-revalidate' ,
223
+ )
224
+ expect ( headers . set ) . toHaveBeenNthCalledWith (
225
+ 2 ,
226
+ 'netlify-cdn-cache-control' ,
227
+ 'public, max-age=0, must-revalidate' ,
228
+ )
229
+ } )
230
+
231
+ test ( 'should not set any headers on POST request' , ( ) => {
232
+ const givenHeaders = {
233
+ 'cache-control' : 'public, max-age=0, must-revalidate' ,
234
+ }
235
+ const headers = new Headers ( givenHeaders )
236
+ const request = new Request ( defaultUrl , { method : 'POST' } )
237
+ vi . spyOn ( headers , 'set' )
238
+
239
+ setCacheControlHeaders ( headers , request )
240
+
241
+ expect ( headers . set ) . toHaveBeenCalledTimes ( 0 )
242
+ } )
243
+
203
244
test ( 'should remove "s-maxage" from "cache-control" header' , ( ) => {
204
245
const givenHeaders = {
205
246
'cache-control' : 'public, s-maxage=604800' ,
206
247
}
207
248
const headers = new Headers ( givenHeaders )
249
+ const request = new Request ( defaultUrl )
208
250
vi . spyOn ( headers , 'set' )
209
251
210
- setCacheControlHeaders ( headers )
252
+ setCacheControlHeaders ( headers , request )
211
253
212
254
expect ( headers . set ) . toHaveBeenNthCalledWith ( 1 , 'cache-control' , 'public' )
213
255
expect ( headers . set ) . toHaveBeenNthCalledWith (
@@ -222,9 +264,10 @@ describe('headers', () => {
222
264
'cache-control' : 'max-age=604800, stale-while-revalidate=86400' ,
223
265
}
224
266
const headers = new Headers ( givenHeaders )
267
+ const request = new Request ( defaultUrl )
225
268
vi . spyOn ( headers , 'set' )
226
269
227
- setCacheControlHeaders ( headers )
270
+ setCacheControlHeaders ( headers , request )
228
271
229
272
expect ( headers . set ) . toHaveBeenNthCalledWith ( 1 , 'cache-control' , 'max-age=604800' )
230
273
expect ( headers . set ) . toHaveBeenNthCalledWith (
@@ -239,9 +282,10 @@ describe('headers', () => {
239
282
'cache-control' : 's-maxage=604800, stale-while-revalidate=86400' ,
240
283
}
241
284
const headers = new Headers ( givenHeaders )
285
+ const request = new Request ( defaultUrl )
242
286
vi . spyOn ( headers , 'set' )
243
287
244
- setCacheControlHeaders ( headers )
288
+ setCacheControlHeaders ( headers , request )
245
289
246
290
expect ( headers . set ) . toHaveBeenNthCalledWith (
247
291
1 ,
0 commit comments