129 lines
2.9 KiB
Go
129 lines
2.9 KiB
Go
package smtp
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"code.yun.ink/pkg/mailx/interfaces"
|
|
)
|
|
|
|
func TestSmtpEnhanced(t *testing.T) {
|
|
smtp := NewSmtp()
|
|
|
|
// 测试配置验证
|
|
ctx := context.Background()
|
|
|
|
// 测试空配置
|
|
_, err := smtp.SetOption(ctx)
|
|
if err == nil {
|
|
t.Error("Expected error for empty config")
|
|
}
|
|
|
|
// 测试不完整配置
|
|
_, err = smtp.SetOption(ctx, func(opt *interfaces.Options) {
|
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
|
Host: "smtp.example.com",
|
|
// 缺少用户名和密码
|
|
}
|
|
})
|
|
if err == nil {
|
|
t.Error("Expected error for incomplete config")
|
|
}
|
|
|
|
// 测试完整配置
|
|
_, err = smtp.SetOption(ctx, func(opt *interfaces.Options) {
|
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
|
Host: "smtp.example.com",
|
|
Port: "587",
|
|
Username: "test@example.com",
|
|
Password: "password",
|
|
IsSSL: false,
|
|
}
|
|
})
|
|
if err != nil {
|
|
t.Errorf("Unexpected error for valid config: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestMessageValidation(t *testing.T) {
|
|
smtp := NewSmtp()
|
|
ctx := context.Background()
|
|
|
|
// 设置有效配置
|
|
smtp.SetOption(ctx, func(opt *interfaces.Options) {
|
|
opt.Smtp = &interfaces.EmailConfigDataSmtp{
|
|
Host: "smtp.example.com",
|
|
Port: "587",
|
|
Username: "test@example.com",
|
|
Password: "password",
|
|
}
|
|
})
|
|
|
|
// 测试空收件人
|
|
err := smtp.validateMessage(interfaces.Message{
|
|
Subject: "Test",
|
|
Body: "Test body",
|
|
})
|
|
if err == nil {
|
|
t.Error("Expected error for empty recipients")
|
|
}
|
|
|
|
// 测试空主题
|
|
err = smtp.validateMessage(interfaces.Message{
|
|
To: []string{"recipient@example.com"},
|
|
Body: "Test body",
|
|
})
|
|
if err == nil {
|
|
t.Error("Expected error for empty subject")
|
|
}
|
|
|
|
// 测试有效消息
|
|
err = smtp.validateMessage(interfaces.Message{
|
|
To: []string{"recipient@example.com"},
|
|
Subject: "Test",
|
|
Body: "Test body",
|
|
})
|
|
if err != nil {
|
|
t.Errorf("Unexpected error for valid message: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestEmailAddressFormatting(t *testing.T) {
|
|
smtp := NewSmtp()
|
|
|
|
addresses := []string{"user1@example.com", "user2@test.com"}
|
|
formatted := smtp.formatEmailAddresses(addresses)
|
|
|
|
expected := "user1<user1@example.com>,user2<user2@test.com>"
|
|
if formatted != expected {
|
|
t.Errorf("Expected %s, got %s", expected, formatted)
|
|
}
|
|
}
|
|
|
|
func TestTimeout(t *testing.T) {
|
|
smtp := NewSmtp()
|
|
|
|
// 测试默认超时
|
|
if smtp.timeout != DefaultTimeout {
|
|
t.Errorf("Expected default timeout %v, got %v", DefaultTimeout, smtp.timeout)
|
|
}
|
|
|
|
// 测试自定义超时
|
|
customTimeout := 60 * time.Second
|
|
smtp.timeout = customTimeout
|
|
if smtp.timeout != customTimeout {
|
|
t.Errorf("Expected custom timeout %v, got %v", customTimeout, smtp.timeout)
|
|
}
|
|
}
|
|
|
|
// 基准测试
|
|
func BenchmarkEmailAddressFormatting(b *testing.B) {
|
|
smtp := NewSmtp()
|
|
addresses := []string{"user1@example.com", "user2@test.com", "user3@demo.com"}
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
smtp.formatEmailAddresses(addresses)
|
|
}
|
|
} |