package imgcache import ( "strings" "testing" "time" ) func TestGenerateSignedURL_WithQueryString(t *testing.T) { signer := NewSigner("test-secret-key-for-testing!") req := &ImageRequest{ SourceHost: "cdn.example.com", SourcePath: "/photos/cat.jpg", SourceQuery: "token=abc&v=2", Size: Size{Width: 800, Height: 600}, Format: FormatWebP, } path, _, _ := signer.GenerateSignedURL(req, time.Hour) // The path must NOT contain a bare "?" that would be interpreted as a query string delimiter. // The size segment must appear as the last path component. if strings.Contains(path, "?token=abc") { t.Errorf("GenerateSignedURL() produced bare query string in path: %q", path) } // The size segment must be present in the path if !strings.Contains(path, "/800x600.webp") { t.Errorf("GenerateSignedURL() missing size segment in path: %q", path) } // Path should end with the size.format, not with query params if !strings.HasSuffix(path, "/800x600.webp") { t.Errorf("GenerateSignedURL() path should end with size.format: %q", path) } } func TestGenerateSignedURL_WithoutQueryString(t *testing.T) { signer := NewSigner("test-secret-key-for-testing!") req := &ImageRequest{ SourceHost: "cdn.example.com", SourcePath: "/photos/cat.jpg", Size: Size{Width: 800, Height: 600}, Format: FormatWebP, } path, _, _ := signer.GenerateSignedURL(req, time.Hour) expected := "/v1/image/cdn.example.com/photos/cat.jpg/800x600.webp" if path != expected { t.Errorf("GenerateSignedURL() path = %q, want %q", path, expected) } }