Skip to content

Commit

Permalink
Add tests for Client debug logging behavior
Browse files Browse the repository at this point in the history
Introduce unit tests to verify the behavior of the debug logging in the Client. Confirm that logs are correctly produced when debug mode is enabled and appropriately suppressed when it is disabled.
  • Loading branch information
wneessen committed Nov 11, 2024
1 parent 3b9085e commit c58aa35
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions smtp/smtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3512,6 +3512,34 @@ func TestClient_GetTLSConnectionState(t *testing.T) {
})
}

func TestClient_debugLog(t *testing.T) {
t.Run("debug log is enabled", func(t *testing.T) {
buffer := bytes.NewBuffer(nil)
logger := log.New(buffer, log.LevelDebug)
client := &Client{logger: logger, debug: true}
client.debugLog(log.DirClientToServer, "%s", "simple string")
client.debugLog(log.DirServerToClient, "%d", 1234)
want := "DEBUG: C --> S: simple string"
if !strings.Contains(buffer.String(), want) {
t.Errorf("expected debug log to contain %q, got: %q", want, buffer.String())
}
want = "DEBUG: C <-- S: 1234"
if !strings.Contains(buffer.String(), want) {
t.Errorf("expected debug log to contain %q, got: %q", want, buffer.String())
}
})
t.Run("debug log is disable", func(t *testing.T) {
buffer := bytes.NewBuffer(nil)
logger := log.New(buffer, log.LevelDebug)
client := &Client{logger: logger, debug: false}
client.debugLog(log.DirClientToServer, "%s", "simple string")
client.debugLog(log.DirServerToClient, "%d", 1234)
if buffer.Len() > 0 {
t.Errorf("expected debug log to be empty, got: %q", buffer.String())
}
})
}

// faker is a struct embedding io.ReadWriter to simulate network connections for testing purposes.
type faker struct {
io.ReadWriter
Expand Down

0 comments on commit c58aa35

Please sign in to comment.