@@ -417,3 +417,86 @@ describe('Cache Middleware', () => {
417417 expect ( res . headers . get ( 'cache-control' ) ) . toBe ( null )
418418 } )
419419} )
420+
421+ describe ( 'Cache Skipping Logic' , ( ) => {
422+ let putSpy : ReturnType < typeof vi . fn >
423+
424+ beforeEach ( ( ) => {
425+ putSpy = vi . fn ( )
426+ const mockCache = {
427+ match : vi . fn ( ) . mockResolvedValue ( undefined ) , // Always miss
428+ put : putSpy , // We spy on this
429+ keys : vi . fn ( ) . mockResolvedValue ( [ ] ) ,
430+ }
431+
432+ vi . stubGlobal ( 'caches' , {
433+ open : vi . fn ( ) . mockResolvedValue ( mockCache ) ,
434+ } )
435+ } )
436+
437+ afterEach ( ( ) => {
438+ vi . restoreAllMocks ( )
439+ } )
440+
441+ it ( 'Should NOT cache response if Cache-Control contains "private"' , async ( ) => {
442+ const app = new Hono ( )
443+ app . use ( '*' , cache ( { cacheName : 'skip-test' , wait : true } ) )
444+ app . get ( '/' , ( c ) => {
445+ c . header ( 'Cache-Control' , 'private, max-age=3600' )
446+ return c . text ( 'response' )
447+ } )
448+
449+ const res = await app . request ( '/' )
450+ expect ( res . status ) . toBe ( 200 )
451+ // IMPORTANT: put() should NOT be called
452+ expect ( putSpy ) . not . toHaveBeenCalled ( )
453+ } )
454+
455+ it ( 'Should NOT cache response if Cache-Control contains "no-store"' , async ( ) => {
456+ const app = new Hono ( )
457+ app . use ( '*' , cache ( { cacheName : 'skip-test' , wait : true } ) )
458+ app . get ( '/' , ( c ) => {
459+ c . header ( 'Cache-Control' , 'no-store' )
460+ return c . text ( 'response' )
461+ } )
462+
463+ await app . request ( '/' )
464+ expect ( putSpy ) . not . toHaveBeenCalled ( )
465+ } )
466+
467+ it ( 'Should NOT cache response if Cache-Control contains no-cache="Set-Cookie"' , async ( ) => {
468+ const app = new Hono ( )
469+ app . use ( '*' , cache ( { cacheName : 'skip-test' , wait : true } ) )
470+ app . get ( '/' , ( c ) => {
471+ c . header ( 'Cache-Control' , 'no-cache="Set-Cookie"' )
472+ return c . text ( 'response' )
473+ } )
474+
475+ await app . request ( '/' )
476+ expect ( putSpy ) . not . toHaveBeenCalled ( )
477+ } )
478+
479+ it ( 'Should NOT cache response if Set-Cookie header is present' , async ( ) => {
480+ const app = new Hono ( )
481+ app . use ( '*' , cache ( { cacheName : 'skip-test' , wait : true } ) )
482+ app . get ( '/' , ( c ) => {
483+ c . header ( 'Set-Cookie' , 'session=secret' )
484+ return c . text ( 'response' )
485+ } )
486+
487+ await app . request ( '/' )
488+ expect ( putSpy ) . not . toHaveBeenCalled ( )
489+ } )
490+
491+ it ( 'Should cache normal responses (Control Test)' , async ( ) => {
492+ const app = new Hono ( )
493+ app . use ( '*' , cache ( { cacheName : 'skip-test' , wait : true } ) )
494+ app . get ( '/' , ( c ) => {
495+ return c . text ( 'response' )
496+ } )
497+
498+ await app . request ( '/' )
499+ // IMPORTANT: put() SHOULD be called for normal responses
500+ expect ( putSpy ) . toHaveBeenCalled ( )
501+ } )
502+ } )
0 commit comments