Remove the code nothing in production calls (closes #312)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
Service.Warm, the ImageCache interface, both GenerateSignedURL methods, signature.ParseParams, three functions of internal/magic, the image processor's lists of supported formats, IsEmpty and Count of the allowlist, ssrfSafeDialer, CacheStale, ErrCacheMiss, ErrNegativeCache, ImageResponse.LastModified and VariantStorage.Load had no caller outside tests. They are gone, with the tests that tested only them. The handlers tests sign their image URLs with generateSignedURL, a test helper that builds the URL as the removed generator did. The SSRF dialer tests call dialSSRFSafe with no operator blocklist, which is all ssrfSafeDialer did. ParseImageURL, MIMEToImageFormat, ContentStorage.Store and CleanExpired stay, as pixa calls them. Model: opus-5-5
This commit is contained in:
@@ -2,7 +2,6 @@ package signature_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -18,8 +17,6 @@ const (
|
||||
testFormatPNG = "png"
|
||||
testFitCover = "cover"
|
||||
testFitContain = "contain"
|
||||
testSignedPath = "/v1/image/cdn.example.com/photos/cat.jpg/800x600.webp"
|
||||
testSig = "abc123"
|
||||
)
|
||||
|
||||
func TestSigner_Sign(t *testing.T) {
|
||||
@@ -378,140 +375,10 @@ func TestSigner_DifferentKeys(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateSignedURL(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
signer := signature.New("test-secret-key")
|
||||
|
||||
req := &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
Query: "",
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
},
|
||||
}
|
||||
|
||||
ttl := 1 * time.Hour
|
||||
path, sig, exp := signer.GenerateSignedURL(req, ttl)
|
||||
|
||||
// Path should be correct format
|
||||
if path != testSignedPath {
|
||||
t.Errorf("GenerateSignedURL() path = %q, want %q", path, testSignedPath)
|
||||
}
|
||||
|
||||
// Signature should be non-empty
|
||||
if sig == "" {
|
||||
t.Error("GenerateSignedURL() produced empty signature")
|
||||
}
|
||||
|
||||
// Expiration should be approximately now + TTL
|
||||
expTime := time.Unix(exp, 0)
|
||||
|
||||
expectedExp := time.Now().Add(ttl)
|
||||
if expTime.Sub(expectedExp) > time.Second {
|
||||
t.Errorf("GenerateSignedURL() exp time off by too much")
|
||||
}
|
||||
|
||||
// Request should have been updated with signature and expiration
|
||||
if req.Signature != sig {
|
||||
t.Errorf("GenerateSignedURL() didn't update request signature")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateSignedURL_OrigSize(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
signer := signature.New("test-secret-key")
|
||||
|
||||
req := &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 0, Height: 0}, // Original size
|
||||
Format: testFormatPNG,
|
||||
},
|
||||
}
|
||||
|
||||
path, _, _ := signer.GenerateSignedURL(req, time.Hour)
|
||||
|
||||
expectedPath := "/v1/image/cdn.example.com/photos/cat.jpg/orig.png"
|
||||
if path != expectedPath {
|
||||
t.Errorf("GenerateSignedURL() path = %q, want %q", path, expectedPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateSignedURL_WithQueryString(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
signer := signature.New("test-secret-key-for-testing!")
|
||||
|
||||
req := &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
Query: "token=abc&v=2",
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
},
|
||||
}
|
||||
|
||||
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) {
|
||||
t.Parallel()
|
||||
|
||||
signer := signature.New("test-secret-key-for-testing!")
|
||||
|
||||
req := &imagerequest.Request{
|
||||
Source: imagerequest.Source{
|
||||
Host: testHost,
|
||||
Path: testPath,
|
||||
},
|
||||
Transform: imagerequest.Transform{
|
||||
Size: imagerequest.Size{Width: 800, Height: 600},
|
||||
Format: testFormatWebP,
|
||||
},
|
||||
}
|
||||
|
||||
path, _, _ := signer.GenerateSignedURL(req, time.Hour)
|
||||
|
||||
if path != testSignedPath {
|
||||
t.Errorf("GenerateSignedURL() path = %q, want %q", path, testSignedPath)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGenerateSignedURL_ReadmeQueryExample checks the README's example of a
|
||||
// source URL with a query, the image at
|
||||
// https://cdn.example.com/img?url=https://origin.example.com/my%20cat.jpg: its
|
||||
// signature with the README's key, and the path GenerateSignedURL writes for
|
||||
// it, with the "?" written as %3F and the "%" as %25.
|
||||
func TestGenerateSignedURL_ReadmeQueryExample(t *testing.T) {
|
||||
// TestSign_ReadmeQueryExample checks the signature, with the README's key, of
|
||||
// the README's example of a source URL with a query, the image at
|
||||
// https://cdn.example.com/img?url=https://origin.example.com/my%20cat.jpg.
|
||||
func TestSign_ReadmeQueryExample(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
signer := signature.New("example-signing-key-for-documentation")
|
||||
@@ -537,76 +404,4 @@ func TestGenerateSignedURL_ReadmeQueryExample(t *testing.T) {
|
||||
if gotSig != wantSig {
|
||||
t.Errorf("Sign() = %q, want %q", gotSig, wantSig)
|
||||
}
|
||||
|
||||
const wantPath = "/v1/image/cdn.example.com" +
|
||||
"/img%3Furl=https://origin.example.com/my%2520cat.jpg/800x600.webp"
|
||||
|
||||
gotPath, _, _ := signer.GenerateSignedURL(&req, time.Hour)
|
||||
if gotPath != wantPath {
|
||||
t.Errorf("GenerateSignedURL() path = %q, want %q", gotPath, wantPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseParams(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
sig string
|
||||
expStr string
|
||||
wantSig string
|
||||
wantErr bool
|
||||
checkTime bool
|
||||
}{
|
||||
{
|
||||
name: "valid params",
|
||||
sig: testSig,
|
||||
expStr: "1704067200",
|
||||
wantSig: testSig,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "empty expiration",
|
||||
sig: testSig,
|
||||
expStr: "",
|
||||
wantSig: testSig,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid expiration",
|
||||
sig: testSig,
|
||||
expStr: "not-a-number",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
sig, exp, err := signature.ParseParams(tt.sig, tt.expStr)
|
||||
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Error("ParseParams() expected error, got nil")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("ParseParams() unexpected error = %v", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if sig != tt.wantSig {
|
||||
t.Errorf("sig = %q, want %q", sig, tt.wantSig)
|
||||
}
|
||||
|
||||
if tt.expStr != "" && exp.IsZero() {
|
||||
t.Error("exp should not be zero when expStr is provided")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user