83 lines
3.0 KiB
Go
83 lines
3.0 KiB
Go
package imgurl
|
||
|
||
import (
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// 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)
|
||
}
|
||
|
||
// 缺省回落 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")
|
||
}
|
||
|
||
// 查询串模式
|
||
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 c.Compose("/uploads/runway/x.jpg") != "/uploads/runway/x.jpg" {
|
||
t.Fatal("/uploads/ 应原样返回")
|
||
}
|
||
if c.Compose("https://vogue.com/foo.jpg") != "https://vogue.com/foo.jpg" {
|
||
t.Fatal("http(s) 应原样返回")
|
||
}
|
||
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)
|
||
}
|
||
}
|
||
}
|