check / check (push) Canceled after 0s
The new package internal/imagerequest holds the request type, its two parts Source and Transform, the Format and Fit types, the dimension and fit checks, the /v1/image/ path parsing and the variant key; it imports no other package of pixa. imgcache.ParsedURL, imgcache.ImageRequest, signature.Request, imageprocessor.Request, the second Size, format and fit types and the conversions between them are gone. The image processor takes a Transform only. encurl keeps a private struct with the token's CBOR field names, so tokens made before still decode. VariantKey moves with the function that makes it. Tests follow the moved code; the tests of the removed conversions go with them. Model: opus-5-5
143 lines
3.4 KiB
Go
143 lines
3.4 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|
|
}
|