1
1
/* eslint-env serviceworker */
2
- import { strictEqual , assertRejects } from './assertions.js' ;
2
+ import { strictEqual , deepStrictEqual , assertRejects } from './assertions.js' ;
3
3
import { routes } from './routes.js' ;
4
4
import { CacheOverride } from 'fastly:cache-override' ;
5
5
import { Backend } from 'fastly:backend' ;
@@ -77,75 +77,6 @@ const httpBinBackend = () =>
77
77
) ;
78
78
} ) ;
79
79
80
- // Test invalid transform stream
81
- routes . set ( '/http-cache/invalid-transform' , async ( ) => {
82
- const url = getTestUrl ( ) ;
83
-
84
- await assertRejects (
85
- ( ) =>
86
- fetch ( url , {
87
- cacheOverride : new CacheOverride ( {
88
- afterSend ( ) {
89
- return {
90
- bodyTransform : 'not a transform stream' ,
91
- } ;
92
- } ,
93
- } ) ,
94
- } ) ,
95
- TypeError ,
96
- ) ;
97
- } ) ;
98
- }
99
-
100
- // Test suite: Response Property Coverage
101
- {
102
- // Test readonly properties
103
- routes . set ( '/http-cache/readonly-properties' , async ( ) => {
104
- const url = getTestUrl ( ) ;
105
- const backend = new Backend ( {
106
- name : 'test_backend' ,
107
- target : 'httpbin.org' ,
108
- } ) ;
109
-
110
- const cacheOverride = new CacheOverride ( {
111
- afterSend ( res ) {
112
- res . ttl = 3600 ;
113
- return { cache : true } ;
114
- } ,
115
- } ) ;
116
-
117
- // Initial request
118
- const res1 = await fetch ( url , { backend, cacheOverride } ) ;
119
- strictEqual ( res1 . backend , backend ) ;
120
- strictEqual ( res1 . age , 0 ) ;
121
-
122
- // Wait a bit and verify age increased
123
- await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
124
- const res2 = await fetch ( url , { backend, cacheOverride } ) ;
125
- strictEqual ( res2 . age > 0 , true ) ;
126
- strictEqual ( res2 . backend , backend ) ;
127
-
128
- // Verify readonly properties cannot be modified
129
- assertRejects ( ( ) => {
130
- res2 . cached = false ;
131
- } , TypeError ) ;
132
- assertRejects ( ( ) => {
133
- res2 . isCacheable = false ;
134
- } , TypeError ) ;
135
- assertRejects ( ( ) => {
136
- res2 . isStale = true ;
137
- } , TypeError ) ;
138
- assertRejects ( ( ) => {
139
- res2 . age = 0 ;
140
- } , TypeError ) ;
141
- assertRejects ( ( ) => {
142
- res2 . backend = null ;
143
- } , TypeError ) ;
144
- } ) ;
145
- }
146
-
147
- // Test suite: Property Error Handling
148
- {
149
80
// Test invalid property assignments
150
81
routes . set ( '/http-cache/property-errors' , async ( ) => {
151
82
const url = getTestUrl ( ) ;
@@ -169,7 +100,7 @@ const httpBinBackend = () =>
169
100
fetch ( url , {
170
101
cacheOverride : new CacheOverride ( {
171
102
afterSend ( res ) {
172
- res . vary = [ 'not-a-set' ] ;
103
+ res . vary = new Set ( [ 'not-an-array' ] ) ;
173
104
} ,
174
105
} ) ,
175
106
} ) ,
@@ -182,7 +113,7 @@ const httpBinBackend = () =>
182
113
fetch ( url , {
183
114
cacheOverride : new CacheOverride ( {
184
115
afterSend ( res ) {
185
- res . surrogateKeys = [ 'not-a-set' ] ;
116
+ res . surrogateKeys = new Set ( [ 'not-an-array' ] ) ;
186
117
} ,
187
118
} ) ,
188
119
} ) ,
@@ -208,7 +139,7 @@ const httpBinBackend = () =>
208
139
fetch ( url , {
209
140
cacheOverride : new CacheOverride ( {
210
141
afterSend ( res ) {
211
- res . vary = new Set ( [ 1 , 2 , 3 ] ) ; // Should only accept strings
142
+ res . vary = [ 1 , 2 , 3 ] ; // Should only accept strings
212
143
} ,
213
144
} ) ,
214
145
} ) ,
@@ -221,7 +152,7 @@ const httpBinBackend = () =>
221
152
fetch ( url , {
222
153
cacheOverride : new CacheOverride ( {
223
154
afterSend ( res ) {
224
- res . surrogateKeys = new Set ( [ 1 , 2 , 3 ] ) ; // Should only accept strings
155
+ res . surrogateKeys = [ 1 , 2 , 3 ] ; // Should only accept strings
225
156
} ,
226
157
} ) ,
227
158
} ) ,
@@ -230,25 +161,93 @@ const httpBinBackend = () =>
230
161
} ) ;
231
162
232
163
// Test property access on invalid states
233
- routes . set ( '/http-cache/property-access-errors ' , async ( ) => {
164
+ routes . set ( '/http-cache/candidate-response-properties ' , async ( ) => {
234
165
const url = getTestUrl ( ) ;
235
166
236
167
// Test accessing cache properties on non-cached response
168
+ {
169
+ let candidateRes ;
170
+ const cacheOverride = new CacheOverride ( {
171
+ afterSend ( res ) {
172
+ candidateRes = res ;
173
+ return { cache : false } ;
174
+ } ,
175
+ } ) ;
176
+
177
+ await fetch ( url , { cacheOverride } ) ;
178
+ strictEqual ( candidateRes . cached , false ) ;
179
+
180
+ strictEqual ( candidateRes . isStale , false ) ;
181
+ strictEqual ( candidateRes . ttl , 3600 ) ;
182
+ strictEqual ( candidateRes . age , 0 ) ;
183
+ deepStrictEqual ( candidateRes . vary , [ ] ) ;
184
+ strictEqual ( candidateRes . surrogateKeys . length , 1 ) ;
185
+ strictEqual ( typeof candidateRes . surrogateKeys [ 0 ] , 'string' ) ;
186
+ strictEqual ( candidateRes . surrogateKeys [ 0 ] . length > 10 , true ) ;
187
+ }
188
+
189
+ // Test accessing cache properties on non-cached candidate response
190
+ {
191
+ const cacheOverride = new CacheOverride ( {
192
+ afterSend ( candidateRes ) {
193
+ strictEqual ( candidateRes . cached , false ) ;
194
+ strictEqual ( candidateRes . isStale , false ) ;
195
+ strictEqual ( candidateRes . ttl , 3600 ) ;
196
+ strictEqual ( candidateRes . age , 0 ) ;
197
+ deepStrictEqual ( candidateRes . vary , [ ] ] ) ;
198
+ strictEqual ( candidateRes . surrogateKeys . length , 1 ) ;
199
+ strictEqual ( typeof candidateRes . surrogateKeys [ 0 ] , 'string' ) ;
200
+ strictEqual ( candidateRes . surrogateKeys [ 0 ] . length > 10 , true ) ;
201
+ return { cache : false } ;
202
+ } ,
203
+ } ) ;
204
+
205
+ await fetch ( url , { cacheOverride } ) ;
206
+ }
207
+ } ) ;
208
+
209
+ // Test readonly properties
210
+ routes . set ( '/http-cache/readonly-properties' , async ( ) => {
211
+ const url = getTestUrl ( ) ;
212
+ const backend = new Backend ( {
213
+ name : 'test_backend' ,
214
+ target : 'httpbin.org' ,
215
+ } ) ;
216
+
237
217
const cacheOverride = new CacheOverride ( {
238
218
afterSend ( res ) {
239
- return { cache : false } ;
219
+ res . ttl = 3600 ;
220
+ return { cache : true } ;
240
221
} ,
241
222
} ) ;
242
223
243
- const res = await fetch ( url , { cacheOverride } ) ;
244
- strictEqual ( res . cached , false ) ;
224
+ // Initial request
225
+ const res1 = await fetch ( url , { backend, cacheOverride } ) ;
226
+ strictEqual ( res1 . backend , backend ) ;
227
+ strictEqual ( res1 . age , 0 ) ;
228
+
229
+ // Wait a bit and verify age increased
230
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
231
+ const res2 = await fetch ( url , { backend, cacheOverride } ) ;
232
+ strictEqual ( res2 . age > 0 , true ) ;
233
+ strictEqual ( res2 . backend , backend ) ;
245
234
246
- // These should all be undefined for non-cached responses
247
- strictEqual ( res . isStale , undefined ) ;
248
- strictEqual ( res . ttl , undefined ) ;
249
- strictEqual ( res . age , undefined ) ;
250
- strictEqual ( res . vary , undefined ) ;
251
- strictEqual ( res . surrogateKeys , undefined ) ;
235
+ // Verify readonly properties cannot be modified
236
+ assertRejects ( ( ) => {
237
+ res2 . cached = false ;
238
+ } , TypeError ) ;
239
+ assertRejects ( ( ) => {
240
+ res2 . isCacheable = false ;
241
+ } , TypeError ) ;
242
+ assertRejects ( ( ) => {
243
+ res2 . isStale = true ;
244
+ } , TypeError ) ;
245
+ assertRejects ( ( ) => {
246
+ res2 . age = 0 ;
247
+ } , TypeError ) ;
248
+ assertRejects ( ( ) => {
249
+ res2 . backend = null ;
250
+ } , TypeError ) ;
252
251
} ) ;
253
252
}
254
253
@@ -458,6 +457,25 @@ const httpBinBackend = () =>
458
457
459
458
// Test suite: Body transform
460
459
{
460
+ // Test invalid transform stream
461
+ routes . set ( '/http-cache/invalid-transform' , async ( ) => {
462
+ const url = getTestUrl ( ) ;
463
+
464
+ await assertRejects (
465
+ ( ) =>
466
+ fetch ( url , {
467
+ cacheOverride : new CacheOverride ( {
468
+ afterSend ( ) {
469
+ return {
470
+ bodyTransform : 'not a transform stream' ,
471
+ } ;
472
+ } ,
473
+ } ) ,
474
+ } ) ,
475
+ TypeError ,
476
+ ) ;
477
+ } ) ;
478
+
461
479
routes . set ( '/http-cache/body-transform' , async ( ) => {
462
480
const url = getTestUrl ( ) ;
463
481
0 commit comments