Files
go-core/smtp/smtp_mailer_test.go
2026-03-01 03:04:10 +01:00

48 lines
1.2 KiB
Go

package smtp
import (
"context"
"errors"
"testing"
"time"
)
func TestSendHonorsCanceledContext(t *testing.T) {
mailer := NewSMTPMailer(SMTPConfig{
Host: "smtp.example.com",
Port: 587,
From: "noreply@example.com",
Mode: SMTPModeTLS,
})
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := mailer.Send(ctx, "to@example.com", "subject", "<p>hello</p>", "hello")
if !errors.Is(err, context.Canceled) {
t.Fatalf("expected %v, got %v", context.Canceled, err)
}
}
func TestSMTPSendDeadlineUsesContextDeadline(t *testing.T) {
now := time.Date(2026, time.January, 1, 12, 0, 0, 0, time.UTC)
want := now.Add(2 * time.Minute)
ctx, cancel := context.WithDeadline(context.Background(), want)
defer cancel()
got := smtpSendDeadline(ctx, now)
if !got.Equal(want) {
t.Fatalf("expected context deadline %v, got %v", want, got)
}
}
func TestSMTPSendDeadlineUsesDefaultWhenContextHasNoDeadline(t *testing.T) {
now := time.Date(2026, time.January, 1, 12, 0, 0, 0, time.UTC)
want := now.Add(defaultSMTPOperationTimeout)
got := smtpSendDeadline(context.Background(), now)
if !got.Equal(want) {
t.Fatalf("expected default deadline %v, got %v", want, got)
}
}