@@ -220,6 +220,72 @@ describe('Cache Interceptor', () => {
220220 strictEqual ( await response . body . text ( ) , 'asd123' )
221221 } )
222222
223+ test ( 'revalidates request w/ etag when provided' , async ( t ) => {
224+ let requestsToOrigin = 0
225+
226+ const clock = FakeTimers . install ( {
227+ shouldClearNativeTimers : true
228+ } )
229+
230+ const server = createServer ( ( req , res ) => {
231+ res . setHeader ( 'cache-control' , 'public, s-maxage=1, stale-while-revalidate=10' )
232+ requestsToOrigin ++
233+
234+ if ( requestsToOrigin > 1 ) {
235+ equal ( req . headers [ 'etag' ] , '"asd123"' )
236+
237+ if ( requestsToOrigin === 3 ) {
238+ res . end ( 'asd123' )
239+ } else {
240+ res . statusCode = 304
241+ res . end ( )
242+ }
243+ } else {
244+ res . setHeader ( 'etag' , '"asd123"' )
245+ res . end ( 'asd' )
246+ }
247+ } ) . listen ( 0 )
248+
249+ const client = new Client ( `http://localhost:${ server . address ( ) . port } ` )
250+ . compose ( interceptors . cache ( ) )
251+
252+ after ( async ( ) => {
253+ server . close ( )
254+ await client . close ( )
255+ clock . uninstall ( )
256+ } )
257+
258+ await once ( server , 'listening' )
259+
260+ strictEqual ( requestsToOrigin , 0 )
261+
262+ const request = {
263+ origin : 'localhost' ,
264+ method : 'GET' ,
265+ path : '/'
266+ }
267+
268+ // Send initial request. This should reach the origin
269+ let response = await client . request ( request )
270+ strictEqual ( requestsToOrigin , 1 )
271+ strictEqual ( await response . body . text ( ) , 'asd' )
272+
273+ clock . tick ( 1500 )
274+
275+ // Now we send two more requests. Both of these should reach the origin,
276+ // but now with a conditional header asking if the resource has been
277+ // updated. These need to be ran after the response is stale.
278+ // No update for the second request
279+ response = await client . request ( request )
280+ strictEqual ( requestsToOrigin , 2 )
281+ strictEqual ( await response . body . text ( ) , 'asd' )
282+
283+ // This should be updated, even though the value isn't expired.
284+ response = await client . request ( request )
285+ strictEqual ( requestsToOrigin , 3 )
286+ strictEqual ( await response . body . text ( ) , 'asd123' )
287+ } )
288+
223289 test ( 'respects cache store\'s isFull property' , async ( ) => {
224290 const server = createServer ( ( _ , res ) => {
225291 res . end ( 'asd' )
0 commit comments