@@ -465,3 +465,139 @@ func TestGetCacheKey(t *testing.T) {
465465 })
466466 }
467467}
468+
469+ func TestGetCacheKeyWithGlobalKeys (t * testing.T ) {
470+ tests := []struct {
471+ name string
472+ path string
473+ method string
474+ query string
475+ headers map [string ]string
476+ globalCacheKeys map [string ]string
477+ want string
478+ }{
479+ {
480+ name : "global key for assets path" ,
481+ path : "/assets/script.js" ,
482+ method : "GET" ,
483+ globalCacheKeys : map [string ]string {
484+ "/assets" : "static-assets" ,
485+ },
486+ want : "static-assets" ,
487+ },
488+ {
489+ name : "global key for _next path" ,
490+ path : "/_next/static/chunk.js" ,
491+ method : "GET" ,
492+ globalCacheKeys : map [string ]string {
493+ "/_next" : "nextjs-assets" ,
494+ },
495+ want : "nextjs-assets" ,
496+ },
497+ {
498+ name : "global key for static path" ,
499+ path : "/static/images/logo.png" ,
500+ method : "GET" ,
501+ globalCacheKeys : map [string ]string {
502+ "/static" : "static-files" ,
503+ },
504+ want : "static-files" ,
505+ },
506+ {
507+ name : "multiple global keys - first match wins" ,
508+ path : "/assets/css/style.css" ,
509+ method : "GET" ,
510+ globalCacheKeys : map [string ]string {
511+ "/assets" : "assets-key" ,
512+ "/assets/css" : "css-key" ,
513+ },
514+ want : "assets-key" , // First match based on map iteration
515+ },
516+ {
517+ name : "no matching global key - falls back to hash" ,
518+ path : "/api/users" ,
519+ method : "GET" ,
520+ query : "id=1" ,
521+ globalCacheKeys : map [string ]string {
522+ "/assets" : "static-assets" ,
523+ "/_next" : "nextjs-assets" ,
524+ },
525+ want : "hashed" , // Will be calculated as hash
526+ },
527+ {
528+ name : "global key ignores query parameters and headers" ,
529+ path : "/assets/script.js" ,
530+ method : "GET" ,
531+ query : "version=1.0&cache=false" ,
532+ headers : map [string ]string {
533+ "Authorization" : "Bearer token123" ,
534+ },
535+ globalCacheKeys : map [string ]string {
536+ "/assets" : "static-assets" ,
537+ },
538+ want : "static-assets" ,
539+ },
540+ {
541+ name : "partial path match - should not match" ,
542+ path : "/asset" , // Missing 's' from '/assets'
543+ method : "GET" ,
544+ globalCacheKeys : map [string ]string {
545+ "/assets" : "static-assets" ,
546+ },
547+ want : "hashed" , // Will be calculated as hash
548+ },
549+ {
550+ name : "exact prefix match" ,
551+ path : "/assets" ,
552+ method : "GET" ,
553+ globalCacheKeys : map [string ]string {
554+ "/assets" : "static-assets" ,
555+ },
556+ want : "static-assets" ,
557+ },
558+ }
559+
560+ for _ , tt := range tests {
561+ t .Run (tt .name , func (t * testing.T ) {
562+ // Create request
563+ req , err := http .NewRequest (tt .method , tt .path , nil )
564+ if err != nil {
565+ t .Fatal (err )
566+ }
567+
568+ // Add query parameters if any
569+ if tt .query != "" {
570+ req .URL .RawQuery = tt .query
571+ }
572+
573+ // Add headers
574+ for key , value := range tt .headers {
575+ req .Header .Set (key , value )
576+ }
577+
578+ // Create handler with config
579+ h := handler {
580+ cfg : config.CacheConfig {
581+ ShouldHashQuery : true ,
582+ HashQueryIgnore : make (map [string ]bool ),
583+ HashHeaders : []string {},
584+ GlobalCacheKeys : tt .globalCacheKeys ,
585+ },
586+ }
587+
588+ // Get cache key
589+ got := h .getCacheKey (req )
590+
591+ if tt .want == "hashed" {
592+ // For non-global keys, verify it's a proper hash
593+ assert .Len (t , got , 64 , "should be a SHA256 hash (64 chars)" )
594+ assert .NotEqual (t , "static-assets" , got )
595+ assert .NotEqual (t , "nextjs-assets" , got )
596+ assert .NotEqual (t , "static-files" , got )
597+ } else {
598+ // For global keys, should match exactly
599+ assert .Equal (t , tt .want , got , "cache key mismatch" )
600+ }
601+ })
602+ }
603+ }
0 commit comments