Files
multica/server/internal/storage/s3_test.go
Bohan Jiang cc1ccedaf3 test(storage): lock S3 upload URL behavior across all env combos (#1421)
Extract the URL assembly at the end of S3Storage.Upload into a helper
(uploadedURL) so the four env-var combinations can be covered by a
table-driven test without mocking s3.PutObject. This locks in the fix
from #1300 — cdn > endpoint > bucket — so future refactors can't
silently regress the CDN-wins-over-custom-endpoint case.

No behavior change.
2026-04-21 12:57:36 +08:00

86 lines
2.2 KiB
Go

package storage
import "testing"
func TestS3StorageKeyFromURL_CustomEndpointPreservesNestedKey(t *testing.T) {
s := &S3Storage{
bucket: "test-bucket",
endpointURL: "http://localhost:9000",
}
rawURL := "http://localhost:9000/test-bucket/uploads/abc/file.png"
if got := s.KeyFromURL(rawURL); got != "uploads/abc/file.png" {
t.Fatalf("KeyFromURL(%q) = %q, want %q", rawURL, got, "uploads/abc/file.png")
}
}
func TestS3StorageKeyFromURL_CustomEndpointWithTrailingSlash(t *testing.T) {
s := &S3Storage{
bucket: "test-bucket",
endpointURL: "http://localhost:9000/",
}
rawURL := "http://localhost:9000/test-bucket/uploads/abc/file.png"
if got := s.KeyFromURL(rawURL); got != "uploads/abc/file.png" {
t.Fatalf("KeyFromURL(%q) = %q, want %q", rawURL, got, "uploads/abc/file.png")
}
}
func TestS3StorageUploadedURL(t *testing.T) {
const key = "uploads/abc/file.png"
cases := []struct {
name string
bucket string
cdnDomain string
endpointURL string
want string
}{
{
name: "bucket only",
bucket: "test-bucket",
want: "https://test-bucket/uploads/abc/file.png",
},
{
name: "cdn only",
bucket: "test-bucket",
cdnDomain: "cdn.example.com",
want: "https://cdn.example.com/uploads/abc/file.png",
},
{
name: "endpoint only",
bucket: "test-bucket",
endpointURL: "http://localhost:9000",
want: "http://localhost:9000/test-bucket/uploads/abc/file.png",
},
{
name: "endpoint with trailing slash",
bucket: "test-bucket",
endpointURL: "http://localhost:9000/",
want: "http://localhost:9000/test-bucket/uploads/abc/file.png",
},
{
name: "endpoint and cdn both set prefers cdn",
bucket: "test-bucket",
cdnDomain: "cdn.example.com",
endpointURL: "http://localhost:9000",
want: "https://cdn.example.com/uploads/abc/file.png",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
s := &S3Storage{
bucket: tc.bucket,
cdnDomain: tc.cdnDomain,
endpointURL: tc.endpointURL,
}
if got := s.uploadedURL(key); got != tc.want {
t.Fatalf("uploadedURL() = %q, want %q", got, tc.want)
}
})
}
}