接口参数调整

This commit is contained in:
ryan
2026-06-11 15:32:44 +08:00
parent ad97ca7df1
commit 6b93320404
10 changed files with 86 additions and 83 deletions
+33 -7
View File
@@ -279,8 +279,8 @@ func TestImageCompression(t *testing.T) {
}
})
t.Run("serve compressed WebP file with compress=true", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/f/3001?compress=true&level=medium", nil)
t.Run("serve compressed WebP file with medium quality", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/f/3001?quality=medium", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
@@ -320,17 +320,43 @@ func TestImageCompression(t *testing.T) {
}
})
t.Run("serve compressed with default quality high", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/f/3001?compress=true", nil)
t.Run("serve original file with origin quality", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/f/3001?quality=origin", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d", w.Code)
}
cacheKey := imageCompressionCacheKey(&uploadRecord, "high")
if _, err := cache.Get(cacheKey); err != nil {
t.Errorf("disk cache Get(%q) returned error: %v", cacheKey, err)
if w.Header().Get("Content-Type") != "image/png" {
t.Errorf("expected Content-Type image/png, got %s", w.Header().Get("Content-Type"))
}
if !bytes.Equal(w.Body.Bytes(), pngBuf.Bytes()) {
t.Errorf("origin-quality response differs from original image")
}
})
}
func TestNormalizeImageQuality(t *testing.T) {
tests := []struct {
name string
quality string
want string
}{
{name: "low", quality: "low", want: "low"},
{name: "medium", quality: "medium", want: "medium"},
{name: "high", quality: "high", want: "high"},
{name: "origin", quality: "origin", want: "origin"},
{name: "uppercase", quality: "LOW", want: "low"},
{name: "empty", quality: "", want: "origin"},
{name: "invalid", quality: "maximum", want: "origin"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := normalizeImageQuality(tt.quality); got != tt.want {
t.Errorf("normalizeImageQuality(%q) = %q, want %q", tt.quality, got, tt.want)
}
})
}
}