@@ -210,6 +210,81 @@ describe('okhttp tests', function() {
210210 assert . equal ( cache . timeToLive ( ) , 260000 ) ;
211211 } ) ;
212212
213+ it ( 'stale-while-revalidate satisfies when stale' , function ( ) {
214+ const cache = new CachePolicy (
215+ { headers : { } } ,
216+ {
217+ headers : {
218+ age : 120 ,
219+ 'cache-control' : 'max-age=60, stale-while-revalidate=200' ,
220+ } ,
221+ } ,
222+ { shared : false }
223+ ) ;
224+
225+ assert ( cache . stale ( ) ) ;
226+ assert ( cache . useStaleWhileRevalidate ( ) ) ;
227+ assert ( cache . satisfiesWithoutRevalidation ( { headers : { } } ) ) ;
228+ } ) ;
229+
230+ it ( 'stale-while-revalidate satisfies when stale and must-revalidate' , function ( ) {
231+ const cache = new CachePolicy (
232+ { headers : { } } ,
233+ {
234+ headers : {
235+ age : 120 ,
236+ 'cache-control' : 'max-age=60, stale-while-revalidate=200, must-revalidate' ,
237+ } ,
238+ } ,
239+ { shared : false }
240+ ) ;
241+
242+ assert ( cache . stale ( ) ) ;
243+ assert ( cache . satisfiesWithoutRevalidation ( { headers : { } } ) ) ;
244+ } ) ;
245+
246+ it ( 'stale-while-revalidate work with max-stale' , function ( ) {
247+ const cache = new CachePolicy (
248+ { headers : { } } ,
249+ {
250+ headers : {
251+ age : 100 ,
252+ 'cache-control' : 'max-age=60, stale-while-revalidate=200' ,
253+ } ,
254+ } ,
255+ { shared : false }
256+ ) ;
257+
258+ assert ( cache . stale ( ) ) ;
259+ assert ( cache . satisfiesWithoutRevalidation ( {
260+ headers : {
261+ 'cache-control' : 'max-stale' ,
262+ }
263+ } ) ) ;
264+ assert ( ! cache . satisfiesWithoutRevalidation ( {
265+ headers : {
266+ 'cache-control' : 'max-stale=40' ,
267+ }
268+ } ) ) ;
269+ } ) ;
270+
271+ it ( 'stale-while-revalidate not satisfies when stale and expired' , function ( ) {
272+ const cache = new CachePolicy (
273+ { headers : { } } ,
274+ {
275+ headers : {
276+ age : 260 ,
277+ 'cache-control' : 'max-age=60, stale-while-revalidate=200' ,
278+ } ,
279+ } ,
280+ { shared : false }
281+ ) ;
282+
283+ assert ( cache . stale ( ) ) ;
284+ assert ( ! cache . useStaleWhileRevalidate ( ) ) ;
285+ assert ( ! cache . satisfiesWithoutRevalidation ( { headers : { } } ) ) ;
286+ } ) ;
287+
213288 it ( 'max age preferred over lower shared max age' , function ( ) {
214289 const cache = new CachePolicy (
215290 { headers : { } } ,
0 commit comments