@@ -259,6 +259,36 @@ import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';
259
259
expect ( await makeRequest ( scope , '/refresh/data' ) ) . toEqual ( 'this is refreshed data' ) ;
260
260
serverUpdate . assertNoOtherRequests ( ) ;
261
261
} ) ;
262
+
263
+ it ( 'caches opaque responses on refresh' , async ( ) => {
264
+ // Make the initial request and populate the cache.
265
+ expect ( await makeRequest ( scope , '/fresh/data' ) ) . toBe ( 'this is fresh data' ) ;
266
+ server . assertSawRequestFor ( '/fresh/data' ) ;
267
+ server . clearRequests ( ) ;
268
+
269
+ // Update the server state and pause the server, so the next request times out.
270
+ scope . updateServerState ( serverUpdate ) ;
271
+ serverUpdate . pause ( ) ;
272
+ const [ res , done ] =
273
+ makePendingRequest ( scope , new MockRequest ( '/fresh/data' , { mode : 'no-cors' } ) ) ;
274
+
275
+ // The network request times out after 1,000ms and the cached response is returned.
276
+ await serverUpdate . nextRequest ;
277
+ scope . advance ( 2000 ) ;
278
+ expect ( await res ) . toBe ( 'this is fresh data' ) ;
279
+
280
+ // Unpause the server to allow the network request to complete and be cached.
281
+ serverUpdate . unpause ( ) ;
282
+ await done ;
283
+
284
+ // Pause the server to force the cached (opaque) response to be returned.
285
+ serverUpdate . pause ( ) ;
286
+ const [ res2 ] = makePendingRequest ( scope , '/fresh/data' ) ;
287
+ await serverUpdate . nextRequest ;
288
+ scope . advance ( 2000 ) ;
289
+
290
+ expect ( await res2 ) . toBe ( '' ) ;
291
+ } ) ;
262
292
} ) ;
263
293
} ) ;
264
294
} ) ( ) ;
0 commit comments