Add tests for Client's SetDebugLog method

These tests verify the behavior of the SetDebugLog method in various scenarios such as enabling and disabling debug logging and ensuring the logger type is as expected. This improves the robustness and reliability of the debug logging functionality in the Client class.
This commit is contained in:
Winni Neessen 2024-11-11 17:44:31 +01:00
parent 2cc670659e
commit 59f2778a38
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -29,12 +29,15 @@ import (
"hash" "hash"
"io" "io"
"net" "net"
"os"
"strings" "strings"
"sync/atomic" "sync/atomic"
"testing" "testing"
"time" "time"
"golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/pbkdf2"
"github.com/wneessen/go-mail/log"
) )
const ( const (
@ -3117,6 +3120,55 @@ func TestClient_Noop(t *testing.T) {
}) })
} }
func TestClient_SetDebugLog(t *testing.T) {
t.Run("set debug loggging to on with no logger defined", func(t *testing.T) {
client := &Client{}
client.SetDebugLog(true)
if !client.debug {
t.Fatalf("expected debug log to be true")
}
if client.logger == nil {
t.Fatalf("expected logger to be defined")
}
if !strings.EqualFold(fmt.Sprintf("%T", client.logger), "*log.Stdlog") {
t.Errorf("expected logger to be of type *log.Stdlog, got: %T", client.logger)
}
})
t.Run("set debug loggging to on should not override logger", func(t *testing.T) {
client := &Client{logger: log.NewJSON(os.Stderr, log.LevelDebug)}
client.SetDebugLog(true)
if !client.debug {
t.Fatalf("expected debug log to be true")
}
if client.logger == nil {
t.Fatalf("expected logger to be defined")
}
if !strings.EqualFold(fmt.Sprintf("%T", client.logger), "*log.JSONlog") {
t.Errorf("expected logger to be of type *log.JSONlog, got: %T", client.logger)
}
})
t.Run("set debug logggin to off with no logger defined", func(t *testing.T) {
client := &Client{}
client.SetDebugLog(false)
if client.debug {
t.Fatalf("expected debug log to be false")
}
if client.logger != nil {
t.Fatalf("expected logger to be nil")
}
})
t.Run("set active logging to off should cancel out logger", func(t *testing.T) {
client := &Client{debug: true, logger: log.New(os.Stderr, log.LevelDebug)}
client.SetDebugLog(false)
if client.debug {
t.Fatalf("expected debug log to be false")
}
if client.logger != nil {
t.Fatalf("expected logger to be nil")
}
})
}
// faker is a struct embedding io.ReadWriter to simulate network connections for testing purposes. // faker is a struct embedding io.ReadWriter to simulate network connections for testing purposes.
type faker struct { type faker struct {
io.ReadWriter io.ReadWriter