Files
hfb_sys/backend/internal/modules/file/storage_test.go
T
yml2213 8dc21d2757 refactor(sitecode): 统一各模块分销站点编码解析为公共包
- 新增 internal/sitecode 包:FromRequest 头优先解析、FromQueryThenHeader
  探测接口专用、ExtractHost/IsLocalHost 提取 Host 域名
- 替换 auth/order/listing/chat/notification/withdrawal/wallet/distribution
  八模块 17 处各自内联的解析逻辑,统一优先级与 TrimSpace 行为
- 修复 notification 漏 TrimSpace、探测接口会话头压过显式 query、
  GetPublicSiteInfo 把 localhost Host 当站点域名查库等不一致
- 顺手清理 auth 中无引用的 extractHost 死代码
- gofmt 修复 withdrawal/presenter、chat/file/payment 测试四个历史遗留格式问题
2026-09-17 14:54:38 +08:00

94 lines
3.0 KiB
Go

package file
import (
"strings"
"testing"
"github.com/minio/minio-go/v7"
)
func TestSanitizeObjectMetadataEscapesNonASCII(t *testing.T) {
got := sanitizeObjectMetadata(map[string]string{
"original-filename": "游戏ID截图 1.png",
})
if got["original-filename"] != "%E6%B8%B8%E6%88%8FID%E6%88%AA%E5%9B%BE+1.png" {
t.Fatalf("metadata filename = %q", got["original-filename"])
}
}
func TestParseBucketLookup(t *testing.T) {
tests := []struct {
name string
raw string
want minio.BucketLookupType
}{
{name: "auto", raw: "auto", want: minio.BucketLookupAuto},
{name: "dns", raw: "dns", want: minio.BucketLookupDNS},
{name: "path", raw: "path", want: minio.BucketLookupPath},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseBucketLookup(tt.raw)
if err != nil || got != tt.want {
t.Fatalf("parseBucketLookup(%q) = %v, %v", tt.raw, got, err)
}
})
}
if _, err := parseBucketLookup("invalid"); err == nil {
t.Fatal("invalid bucket lookup should fail")
}
}
func TestAnnouncementSceneUsesPublicFileURL(t *testing.T) {
key := "announcement/2026/06/15/example.webp"
got := fileURLForScene("announcement", key)
if !strings.HasPrefix(got, "/api/public/files/object?") {
t.Fatalf("announcement file url = %q, want public file endpoint", got)
}
}
func TestNormalizeSceneAllowsAnnouncement(t *testing.T) {
if got := normalizeScene("announcement"); got != "announcement" {
t.Fatalf("normalize announcement scene = %q", got)
}
}
func TestAWRecycleSceneUsesPublicFileURL(t *testing.T) {
if got := normalizeScene("aw-recycle"); got != "aw-recycle" {
t.Fatalf("normalize aw-recycle scene = %q", got)
}
key := "aw-recycle/2026/07/19/example.webp"
got := fileURLForScene("aw-recycle", key)
if !strings.HasPrefix(got, "/api/public/files/object?") {
t.Fatalf("aw-recycle file url = %q, want public file endpoint", got)
}
}
func TestCooperationFeedbackSceneUsesPublicFileURL(t *testing.T) {
if got := normalizeScene("cooperation-feedback"); got != "cooperation-feedback" {
t.Fatalf("normalize cooperation-feedback scene = %q", got)
}
key := "cooperation-feedback/2026/07/19/example.webp"
got := fileURLForScene("cooperation-feedback", key)
if !strings.HasPrefix(got, "/api/public/files/object?") {
t.Fatalf("cooperation-feedback file url = %q, want public file endpoint", got)
}
}
func TestNormalizeSceneListingScreenshotAndOperatingExpense(t *testing.T) {
if got := normalizeScene("listing_screenshot"); got != "listing" {
t.Fatalf("normalize listing_screenshot scene = %q, want listing", got)
}
if got := normalizeScene("listing-screenshot"); got != "listing" {
t.Fatalf("normalize listing-screenshot scene = %q, want listing", got)
}
if got := normalizeScene("operating-expense"); got != "operating-expense" {
t.Fatalf("normalize operating-expense scene = %q, want operating-expense", got)
}
if got := normalizeScene("operating_expense"); got != "operating-expense" {
t.Fatalf("normalize operating_expense scene = %q, want operating-expense", got)
}
}