From cd134d1d05d5f1a79efca18b24346e97332803e1 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Mon, 10 Feb 2025 21:33:33 +0100 Subject: [PATCH] Add tests for handling nil messages in email client Introduce unit tests to verify the behavior of `DialAndSendWithContext` and `Send` methods when provided with `nil` messages. These tests ensure proper error handling and validation under such scenarios, improving robustness. This accompanies commit 4641da4 which was missing the tests. --- client_test.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/client_test.go b/client_test.go index 490e9c1..c0c6bfa 100644 --- a/client_test.go +++ b/client_test.go @@ -2217,6 +2217,39 @@ func TestClient_DialAndSendWithContext(t *testing.T) { t.Fatalf("failed to dial and send: %s", err) } }) + // https://github.com/wneessen/go-mail/commit/4641da450f5e3b3726e01b1cf03c88361cf49c8f + t.Run("DialAndSendWithContext with nil messages", func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + PortAdder.Add(1) + serverPort := int(TestServerPortBase + PortAdder.Load()) + featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8" + go func() { + if err := simpleSMTPServer(ctx, t, &serverProps{ + FeatureSet: featureSet, + ListenPort: serverPort, + }); err != nil { + t.Errorf("failed to start test server: %s", err) + return + } + }() + time.Sleep(time.Millisecond * 30) + + ctxDial, cancelDial := context.WithTimeout(ctx, time.Millisecond*500) + t.Cleanup(cancelDial) + + client, err := NewClient(DefaultHost, WithTLSPolicy(NoTLS), WithPort(serverPort)) + if err != nil { + t.Fatalf("failed to create new client: %s", err) + } + if err = client.DialAndSendWithContext(ctxDial, nil); err != nil { + var netErr net.Error + if errors.As(err, &netErr) && netErr.Timeout() { + t.Skip("failed to connect to the test server due to timeout") + } + t.Fatalf("failed to dial and send: %s", err) + } + }) t.Run("DialAndSendWithContext fail on dial", func(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -2677,6 +2710,47 @@ func TestClient_Send(t *testing.T) { t.Errorf("failed to send email: %s", err) } }) + // https://github.com/wneessen/go-mail/commit/4641da450f5e3b3726e01b1cf03c88361cf49c8f + t.Run("connect and try to send email but message is nil", func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + PortAdder.Add(1) + serverPort := int(TestServerPortBase + PortAdder.Load()) + featureSet := "250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8" + go func() { + if err := simpleSMTPServer(ctx, t, &serverProps{ + FeatureSet: featureSet, + ListenPort: serverPort, + }); err != nil { + t.Errorf("failed to start test server: %s", err) + return + } + }() + time.Sleep(time.Millisecond * 30) + + ctxDial, cancelDial := context.WithTimeout(ctx, time.Millisecond*500) + t.Cleanup(cancelDial) + + client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS)) + if err != nil { + t.Fatalf("failed to create new client: %s", err) + } + if err = client.DialWithContext(ctxDial); err != nil { + var netErr net.Error + if errors.As(err, &netErr) && netErr.Timeout() { + t.Skip("failed to connect to the test server due to timeout") + } + t.Fatalf("failed to connect to test server: %s", err) + } + t.Cleanup(func() { + if err := client.Close(); err != nil { + t.Errorf("failed to close client: %s", err) + } + }) + if err = client.Send(nil); err != nil { + t.Errorf("failed to send email: %s", err) + } + }) t.Run("send with no connection should fail", func(t *testing.T) { client, err := NewClient(DefaultHost) if err != nil {