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, imagerequest.ParseImagePath, four symbols of internal/magic, the image processor's lists of supported formats, IsEmpty and Count of the allowlist, ssrfSafeDialer, CacheStale, ErrCacheMiss, ErrNegativeCache, ImageResponse.LastModified and the Load methods of ContentStorage and VariantStorage 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. The tests of ParseImagePath and ContentStorage.Load call ParseImageURL and LoadWithSize instead. MIMEToImageFormat, ContentStorage.Store and CleanExpired stay, as pixa calls them. Model: opus-5-5
This commit is contained in:
@@ -3,9 +3,6 @@ package magic
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -155,119 +152,6 @@ func TestValidateMagicBytes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsSupportedMIMEType(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
mimeType string
|
||||
want bool
|
||||
}{
|
||||
{testMIMEJPEG, true},
|
||||
{testMIMEPNG, true},
|
||||
{testMIMEWebP, true},
|
||||
{testMIMEGIF, true},
|
||||
{testMIMEAVIF, true},
|
||||
{"image/svg+xml", true},
|
||||
{"IMAGE/JPEG", true},
|
||||
{testMIMEJPEGParams, true},
|
||||
{"image/tiff", false},
|
||||
{"image/bmp", false},
|
||||
{mimeOctetStream, false},
|
||||
{"text/plain", false},
|
||||
{"", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.mimeType, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if got := IsSupportedMIMEType(tt.mimeType); got != tt.want {
|
||||
t.Errorf("IsSupportedMIMEType(%q) = %v, want %v", tt.mimeType, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPeekAndValidate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
jpegMagic := []byte{
|
||||
0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01,
|
||||
}
|
||||
pngMagic := []byte{
|
||||
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D,
|
||||
}
|
||||
jpegData := slices.Concat(jpegMagic, []byte("rest of jpeg data"))
|
||||
pngData := slices.Concat(pngMagic, []byte("rest of png data"))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
data []byte
|
||||
declaredType string
|
||||
wantErr bool
|
||||
wantData []byte
|
||||
}{
|
||||
{
|
||||
name: "valid JPEG",
|
||||
data: jpegData,
|
||||
declaredType: testMIMEJPEG,
|
||||
wantErr: false,
|
||||
wantData: jpegData,
|
||||
},
|
||||
{
|
||||
name: "valid PNG",
|
||||
data: pngData,
|
||||
declaredType: testMIMEPNG,
|
||||
wantErr: false,
|
||||
wantData: pngData,
|
||||
},
|
||||
{
|
||||
name: "mismatched type",
|
||||
data: jpegData,
|
||||
declaredType: testMIMEPNG,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
r := bytes.NewReader(tt.data)
|
||||
|
||||
result, err := PeekAndValidate(r, tt.declaredType)
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Error("PeekAndValidate() expected error, got nil")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("PeekAndValidate() unexpected error = %v", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Read all data from result reader
|
||||
got, err := io.ReadAll(result)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to read result: %v", err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if !bytes.Equal(got, tt.wantData) {
|
||||
t.Errorf(
|
||||
"PeekAndValidate() data mismatch: got %d bytes, want %d bytes",
|
||||
len(got), len(tt.wantData),
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMIMEToImageFormat(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -303,35 +187,6 @@ func TestMIMEToImageFormat(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageFormatToMIME(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
format ImageFormat
|
||||
wantMIME string
|
||||
}{
|
||||
{FormatJPEG, testMIMEJPEG},
|
||||
{FormatPNG, testMIMEPNG},
|
||||
{FormatWebP, testMIMEWebP},
|
||||
{FormatGIF, testMIMEGIF},
|
||||
{FormatAVIF, testMIMEAVIF},
|
||||
{FormatOriginal, mimeOctetStream},
|
||||
{"unknown", mimeOctetStream},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(string(tt.format), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
got := ImageFormatToMIME(tt.format)
|
||||
|
||||
if got != tt.wantMIME {
|
||||
t.Errorf("ImageFormatToMIME(%q) = %v, want %v", tt.format, got, tt.wantMIME)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeMIMEType(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -493,34 +348,3 @@ func TestDetectFormatFtypNotAVIF(t *testing.T) {
|
||||
t.Errorf("DetectFormat(MP4) error = %v, want %v", err, ErrUnknownFormat)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPeekAndValidatePreservesReader(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Ensure that after PeekAndValidate, we can read the complete
|
||||
// original content
|
||||
originalContent := append(
|
||||
[]byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D},
|
||||
[]byte(strings.Repeat("PNG IDAT chunk data here ", 100))...,
|
||||
)
|
||||
|
||||
r := bytes.NewReader(originalContent)
|
||||
|
||||
validated, err := PeekAndValidate(r, testMIMEPNG)
|
||||
if err != nil {
|
||||
t.Fatalf("PeekAndValidate() error = %v", err)
|
||||
}
|
||||
|
||||
// Read everything from the validated reader
|
||||
got, err := io.ReadAll(validated)
|
||||
if err != nil {
|
||||
t.Fatalf("io.ReadAll() error = %v", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(got, originalContent) {
|
||||
t.Errorf(
|
||||
"Content mismatch: got %d bytes, want %d bytes",
|
||||
len(got), len(originalContent),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user