Add debug logging to client creation in tests

Introduce `WithDebugLog()` in client creation for enhanced logging during tests. Correct handling of certain SMTP command errors by replacing `return` with `break` in switch cases for proper loop continuation.
This commit is contained in:
Winni Neessen 2024-10-23 22:20:32 +02:00
parent 74fa3f6f62
commit ea57644a8e
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -1668,7 +1668,7 @@ func TestClient_DialWithContext(t *testing.T) {
ctxDial, cancelDial := context.WithTimeout(ctx, time.Millisecond*500) ctxDial, cancelDial := context.WithTimeout(ctx, time.Millisecond*500)
t.Cleanup(cancelDial) t.Cleanup(cancelDial)
client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS)) client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS), WithDebugLog())
if err != nil { if err != nil {
t.Fatalf("failed to create new client: %s", err) t.Fatalf("failed to create new client: %s", err)
} }
@ -3725,14 +3725,14 @@ func handleTestServerConnection(connection net.Conn, props *serverProps) {
case strings.HasPrefix(data, "EHLO"), strings.HasPrefix(data, "HELO"): case strings.HasPrefix(data, "EHLO"), strings.HasPrefix(data, "HELO"):
if len(strings.Split(data, " ")) != 2 { if len(strings.Split(data, " ")) != 2 {
_ = writeLine("501 Syntax: EHLO hostname") _ = writeLine("501 Syntax: EHLO hostname")
return break
} }
if props.FailOnHelo { if props.FailOnHelo {
_ = writeLine("500 5.5.2 Error: fail on HELO") _ = writeLine("500 5.5.2 Error: fail on HELO")
return break
} }
if err = writeLine("250-localhost.localdomain\r\n" + props.FeatureSet); err != nil { if err = writeLine("250-localhost.localdomain\r\n" + props.FeatureSet); err != nil {
return break
} }
case strings.HasPrefix(data, "MAIL FROM:"): case strings.HasPrefix(data, "MAIL FROM:"):
from := strings.TrimPrefix(data, "MAIL FROM:") from := strings.TrimPrefix(data, "MAIL FROM:")
@ -3849,6 +3849,7 @@ func handleTestServerConnection(connection net.Conn, props *serverProps) {
break break
} }
_ = writeLine("221 2.0.0 Bye") _ = writeLine("221 2.0.0 Bye")
return
default: default:
_ = writeLine("500 5.5.2 Error: bad syntax") _ = writeLine("500 5.5.2 Error: bad syntax")
} }