-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TestClient_SendErrorReset to validate SMTP error handling
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
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|