45 lines
1.4 KiB
Go
45 lines
1.4 KiB
Go
package app
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseStorageHTTPSURL(t *testing.T) {
|
|
tests := []struct {
|
|
url string
|
|
allowPath bool
|
|
valid bool
|
|
}{
|
|
{url: "https://oss-cn-hangzhou.aliyuncs.com", valid: true},
|
|
{url: "https://cdn.example.com/media", allowPath: true, valid: true},
|
|
{url: "http://oss.example.com", valid: false},
|
|
{url: "https://user:pass@oss.example.com", valid: false},
|
|
{url: "https://oss.example.com/path", valid: false},
|
|
{url: "https://oss.example.com?token=secret", valid: false},
|
|
}
|
|
for _, test := range tests {
|
|
_, err := parseStorageHTTPSURL(test.url, test.allowPath)
|
|
if (err == nil) != test.valid {
|
|
t.Errorf("parseStorageHTTPSURL(%q, %v) error = %v, valid = %v", test.url, test.allowPath, err, test.valid)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateLocalStorageDirectoryRejectsRoot(t *testing.T) {
|
|
root := filepath.VolumeName(t.TempDir()) + string(filepath.Separator)
|
|
if err := validateLocalStorageDirectory(root); err == nil {
|
|
t.Fatalf("expected volume root %q to be rejected", root)
|
|
}
|
|
if err := validateLocalStorageDirectory(filepath.Join(t.TempDir(), "uploads")); err != nil {
|
|
t.Fatalf("expected nested upload directory to be accepted: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestStoragePublicURL(t *testing.T) {
|
|
got := storagePublicURL("https://cdn.example.com/media/", "/2026/08/test.png")
|
|
if got != "https://cdn.example.com/media/2026/08/test.png" {
|
|
t.Fatalf("unexpected public URL: %s", got)
|
|
}
|
|
}
|