Skip to content

Commit

Permalink
Add TestClient_SendErrorReset to validate SMTP error handling
Browse files Browse the repository at this point in the history
This test checks the client's ability to handle SMTP reset errors when sending an email. It verifies the correct error type, ensures it is recognized as a permanent error, and confirms the correct message ID handling.
  • Loading branch information
wneessen committed Sep 20, 2024
1 parent b8f0462 commit 482194b
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,68 @@ func TestClient_SendErrorDataWrite(t *testing.T) {
}
}

func TestClient_SendErrorReset(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8"
go func() {
if err := simpleSMTPServer(ctx, featureSet, true); err != nil {
t.Errorf("failed to start test server: %s", err)
return
}
}()
time.Sleep(time.Millisecond * 300)

message := NewMsg()
if err := message.From("[email protected]"); err != nil {
t.Errorf("failed to set FROM address: %s", err)
return
}
if err := message.To("[email protected]"); err != nil {
t.Errorf("failed to set TO address: %s", err)
return
}
message.Subject("Test subject")
message.SetBodyString(TypeTextPlain, "Test body")
message.SetMessageIDWithValue("this.is.a.message.id")

client, err := NewClient(TestServerAddr, WithPort(TestServerPort),
WithTLSPortPolicy(NoTLS), WithSMTPAuth(SMTPAuthPlain),
WithUsername("[email protected]"),
WithPassword("V3ryS3cr3t+"))
if err != nil {
t.Errorf("unable to create new client: %s", err)
}
if err = client.DialWithContext(context.Background()); err != nil {
t.Errorf("failed to dial to test server: %s", err)
}
if err = client.Send(message); err == nil {
t.Error("expected Send() to fail but didn't")
}

var sendErr *SendError
if !errors.As(err, &sendErr) {
t.Errorf("expected *SendError type as returned error, but got %T", sendErr)
}
if errors.As(err, &sendErr) {
if sendErr.IsTemp() {
t.Errorf("expected permanent error but IsTemp() returned true")
}
if sendErr.Reason != ErrSMTPReset {
t.Errorf("expected ErrSMTPReset error, but got %s", sendErr.Reason)
}
if !strings.EqualFold(sendErr.MessageID(), "<this.is.a.message.id>") {
t.Errorf("expected message ID: %q, but got %q", "<this.is.a.message.id>",
sendErr.MessageID())
}
}

if err = client.Close(); err != nil {
t.Errorf("failed to close server connection: %s", err)
}
}

// getTestConnection takes environment variables to establish a connection to a real
// SMTP server to test all functionality that requires a connection
func getTestConnection(auth bool) (*Client, error) {
Expand Down

0 comments on commit 482194b

Please sign in to comment.