update
This commit is contained in:
@ -5,29 +5,78 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestVipStyle 验证:免费(free)用 -free 命名样式、VIP 用 -vip 命名样式,且均带私有空间签名。
|
||||
func TestVipStyle(t *testing.T) {
|
||||
c := New("http://toom-studio.23cm.cn", "free", "vip", "", "AK_TEST", "SK_TEST", 31536000, 3600)
|
||||
// TestDisplayStyle 验证新模型(2026-09-11):VIP 与免费用户同质量,统一走展示样式。
|
||||
// - 命名样式(默认 high):base_url/key!style:high
|
||||
// - 查询串模式(style 含 =):base_url/key?w=..&q=..
|
||||
// - /uploads/ 与 http(s) 原样返回
|
||||
func TestDisplayStyle(t *testing.T) {
|
||||
c := New("https://mybucket.s3.bitiful.net", "high", "thumb", "")
|
||||
got := c.Compose("runway/abc123.jpg")
|
||||
want := "https://mybucket.s3.bitiful.net/runway/abc123.jpg!style:high"
|
||||
if got != want {
|
||||
t.Fatalf("命名样式拼装错误: 期望 %s 实际 %s", want, got)
|
||||
}
|
||||
|
||||
free := c.Compose("runway/abc123.jpg", false)
|
||||
vip := c.Compose("runway/abc123.jpg", true)
|
||||
// 缺省回落 high
|
||||
c0 := New("https://mybucket.s3.bitiful.net", "", "", "")
|
||||
if c0.Compose("runway/abc123.jpg") != "https://mybucket.s3.bitiful.net/runway/abc123.jpg!style:high" {
|
||||
t.Fatal("style 缺省应回落 high")
|
||||
}
|
||||
|
||||
t.Logf("free URL = %s", free)
|
||||
t.Logf("vip URL = %s", vip)
|
||||
// 查询串模式
|
||||
cq := New("https://mybucket.s3.bitiful.net", "w=1080&q=80&fmt=webp", "", "")
|
||||
gq := cq.Compose("runway/abc123.jpg")
|
||||
wq := "https://mybucket.s3.bitiful.net/runway/abc123.jpg?w=1080&q=80&fmt=webp"
|
||||
if gq != wq {
|
||||
t.Fatalf("查询串模式拼装错误: 期望 %s 实际 %s", wq, gq)
|
||||
}
|
||||
|
||||
if free == "" || vip == "" {
|
||||
t.Fatal("URL 不应为空")
|
||||
// 本地兜底与外链原样返回
|
||||
if c.Compose("/uploads/runway/x.jpg") != "/uploads/runway/x.jpg" {
|
||||
t.Fatal("/uploads/ 应原样返回")
|
||||
}
|
||||
if !strings.Contains(free, "-free") {
|
||||
t.Fatalf("免费图应拼 -free 命名样式,实际: %s", free)
|
||||
if c.Compose("https://vogue.com/foo.jpg") != "https://vogue.com/foo.jpg" {
|
||||
t.Fatal("http(s) 应原样返回")
|
||||
}
|
||||
if !strings.Contains(vip, "-vip") {
|
||||
t.Fatalf("VIP 图应拼 -vip 命名样式,实际: %s", vip)
|
||||
}
|
||||
if !strings.Contains(free, "token=") || !strings.Contains(vip, "token=") {
|
||||
t.Fatal("两种图都必须带私有空间下载签名 token=")
|
||||
}
|
||||
if strings.Contains(vip, "-free") {
|
||||
t.Fatal("VIP 图不应混入 -free")
|
||||
if c.Compose("") != "" {
|
||||
t.Fatal("空串应返回空")
|
||||
}
|
||||
}
|
||||
|
||||
// TestComposeThumb 验证列表缩略图拼装:走 StyleThumb,兜底逻辑与 Compose 一致。
|
||||
func TestComposeThumb(t *testing.T) {
|
||||
c := New("https://mybucket.s3.bitiful.net", "high", "thumb", "")
|
||||
got := c.ComposeThumb("runway/abc123.jpg")
|
||||
want := "https://mybucket.s3.bitiful.net/runway/abc123.jpg!style:thumb"
|
||||
if got != want {
|
||||
t.Fatalf("缩略图样式拼装错误: 期望 %s 实际 %s", want, got)
|
||||
}
|
||||
|
||||
// 未配置 styleThumb 时应回落展示样式,不得拼出空样式(key!style:)
|
||||
c0 := New("https://mybucket.s3.bitiful.net", "high", "", "")
|
||||
if got := c0.ComposeThumb("runway/abc123.jpg"); got != "https://mybucket.s3.bitiful.net/runway/abc123.jpg!style:high" {
|
||||
t.Fatalf("styleThumb 缺省应回落 styleDisplay,实际 %s", got)
|
||||
}
|
||||
|
||||
// 兜底:本地路径 / 外链 / 空串
|
||||
if c.ComposeThumb("/uploads/runway/x.jpg") != "/uploads/runway/x.jpg" {
|
||||
t.Fatal("/uploads/ 应原样返回")
|
||||
}
|
||||
if c.ComposeThumb("https://vogue.com/foo.jpg") != "https://vogue.com/foo.jpg" {
|
||||
t.Fatal("http(s) 应原样返回")
|
||||
}
|
||||
if c.ComposeThumb("") != "" {
|
||||
t.Fatal("空串应返回空")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDisplayStyleURLSafe 确认展示 URL 不含任何签名/过期参数(匿名可访问)。
|
||||
func TestDisplayStyleURLSafe(t *testing.T) {
|
||||
c := New("https://mybucket.s3.bitiful.net", "high", "thumb", "")
|
||||
u := c.Compose("runway/abc123.jpg")
|
||||
for _, bad := range []string{"X-Amz", "sign", "e="} {
|
||||
if strings.Contains(u, bad) {
|
||||
t.Fatalf("展示 URL 不应含签名参数 %q,实际: %s", bad, u)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user