package imagerequest import "testing" func TestCacheKey(t *testing.T) { t.Parallel() req1 := &Request{ Source: Source{Host: testHostCDN, Path: testPathCat, Query: ""}, Transform: Transform{ Size: Size{Width: 800, Height: 600}, Format: FormatWebP, Quality: 85, FitMode: FitCover, }, } req2 := &Request{ Source: Source{Host: testHostCDN, Path: testPathCat, Query: ""}, Transform: Transform{ Size: Size{Width: 800, Height: 600}, Format: FormatWebP, Quality: 85, FitMode: FitCover, }, } // Same request should produce same key key1 := CacheKey(req1) key2 := CacheKey(req2) if key1 != key2 { t.Errorf("CacheKey() not deterministic: %s vs %s", key1, key2) } // Key should be 64 hex chars if len(string(key1)) != 64 { t.Errorf("CacheKey() length = %d, want 64", len(string(key1))) } // Different size should produce different key req3 := &Request{ Source: Source{Host: testHostCDN, Path: testPathCat, Query: ""}, Transform: Transform{ Size: Size{Width: 400, Height: 300}, Format: FormatWebP, // Different size Quality: 85, FitMode: FitCover, }, } key3 := CacheKey(req3) if key1 == key3 { t.Error("CacheKey() produced same key for different sizes") } // Different format should produce different key req4 := &Request{ Source: Source{Host: testHostCDN, Path: testPathCat, Query: ""}, Transform: Transform{ Size: Size{Width: 800, Height: 600}, Format: FormatPNG, // Different format Quality: 85, FitMode: FitCover, }, } key4 := CacheKey(req4) if key1 == key4 { t.Error("CacheKey() produced same key for different formats") } // Different quality should produce different key req5 := &Request{ Source: Source{Host: testHostCDN, Path: testPathCat, Query: ""}, Transform: Transform{ Size: Size{Width: 800, Height: 600}, Format: FormatWebP, Quality: 50, FitMode: FitCover, // Different quality }, } key5 := CacheKey(req5) if key1 == key5 { t.Error("CacheKey() produced same key for different quality") } } // TestCacheKey_ColonOrPercentInPathOrQuery checks that two different source // URLs whose path or query holds ":" or "%" get different keys. func TestCacheKey_ColonOrPercentInPathOrQuery(t *testing.T) { t.Parallel() cases := []struct { name string path1, query1 string path2, query2 string }{ { name: "colon moved from path to query", path1: "/a:b", query1: "", path2: "/a", query2: "b:", }, { name: "colon in path and its percent-encoding", path1: "/a:b", query1: "", path2: "/a%3Ab", query2: "", }, } for _, tt := range cases { t.Run(tt.name, func(t *testing.T) { t.Parallel() key1 := CacheKey(&Request{ Source: Source{ Host: testHostCDN, Path: tt.path1, Query: tt.query1, }, Transform: Transform{ Size: Size{Width: 800, Height: 600}, Format: FormatWebP, Quality: 85, FitMode: FitCover, }, }) key2 := CacheKey(&Request{ Source: Source{ Host: testHostCDN, Path: tt.path2, Query: tt.query2, }, Transform: Transform{ Size: Size{Width: 800, Height: 600}, Format: FormatWebP, Quality: 85, FitMode: FitCover, }, }) t.Logf("CacheKey() of path %q query %q = %s", tt.path1, tt.query1, key1) t.Logf("CacheKey() of path %q query %q = %s", tt.path2, tt.query2, key2) if key1 == key2 { t.Errorf("CacheKey() gave path %q query %q and path %q query %q "+ "the same key", tt.path1, tt.query1, tt.path2, tt.query2) } }) } }