Remove connection field from Client struct

This commit removes the 'connection' field from the 'Client' struct and updates the related test logic accordingly. By using 'smtpClient.HasConnection()' to check for connections, code readability and maintainability are improved. All necessary test cases have been adjusted to reflect this change.
This commit is contained in:
Winni Neessen 2024-09-27 11:43:22 +02:00
parent fdb80ad9dd
commit 2234f0c5bc
Signed by: wneessen
GPG key ID: 385AC9889632126E
2 changed files with 38 additions and 42 deletions

View file

@ -88,9 +88,6 @@ type DialContextFunc func(ctx context.Context, network, address string) (net.Con
// Client is the SMTP client struct // Client is the SMTP client struct
type Client struct { type Client struct {
// connection is the net.Conn that the smtp.Client is based on
connection net.Conn
// Timeout for the SMTP server connection // Timeout for the SMTP server connection
connTimeout time.Duration connTimeout time.Duration

View file

@ -623,11 +623,12 @@ func TestClient_DialWithContext(t *testing.T) {
t.Errorf("failed to dial with context: %s", err) t.Errorf("failed to dial with context: %s", err)
return return
} }
if c.connection == nil {
t.Errorf("DialWithContext didn't fail but no connection found.")
}
if c.smtpClient == nil { if c.smtpClient == nil {
t.Errorf("DialWithContext didn't fail but no SMTP client found.") t.Errorf("DialWithContext didn't fail but no SMTP client found.")
return
}
if !c.smtpClient.HasConnection() {
t.Errorf("DialWithContext didn't fail but no connection found.")
} }
if err := c.Close(); err != nil { if err := c.Close(); err != nil {
t.Errorf("failed to close connection: %s", err) t.Errorf("failed to close connection: %s", err)
@ -644,17 +645,18 @@ func TestClient_DialWithContext_Fallback(t *testing.T) {
c.SetTLSPortPolicy(TLSOpportunistic) c.SetTLSPortPolicy(TLSOpportunistic)
c.port = 999 c.port = 999
ctx := context.Background() ctx := context.Background()
if err := c.DialWithContext(ctx); err != nil { if err = c.DialWithContext(ctx); err != nil {
t.Errorf("failed to dial with context: %s", err) t.Errorf("failed to dial with context: %s", err)
return return
} }
if c.connection == nil {
t.Errorf("DialWithContext didn't fail but no connection found.")
}
if c.smtpClient == nil { if c.smtpClient == nil {
t.Errorf("DialWithContext didn't fail but no SMTP client found.") t.Errorf("DialWithContext didn't fail but no SMTP client found.")
return
} }
if err := c.Close(); err != nil { if !c.smtpClient.HasConnection() {
t.Errorf("DialWithContext didn't fail but no connection found.")
}
if err = c.Close(); err != nil {
t.Errorf("failed to close connection: %s", err) t.Errorf("failed to close connection: %s", err)
} }
@ -674,18 +676,19 @@ func TestClient_DialWithContext_Debug(t *testing.T) {
t.Skipf("failed to create test client: %s. Skipping tests", err) t.Skipf("failed to create test client: %s. Skipping tests", err)
} }
ctx := context.Background() ctx := context.Background()
if err := c.DialWithContext(ctx); err != nil { if err = c.DialWithContext(ctx); err != nil {
t.Errorf("failed to dial with context: %s", err) t.Errorf("failed to dial with context: %s", err)
return return
} }
if c.connection == nil {
t.Errorf("DialWithContext didn't fail but no connection found.")
}
if c.smtpClient == nil { if c.smtpClient == nil {
t.Errorf("DialWithContext didn't fail but no SMTP client found.") t.Errorf("DialWithContext didn't fail but no SMTP client found.")
return
}
if !c.smtpClient.HasConnection() {
t.Errorf("DialWithContext didn't fail but no connection found.")
} }
c.SetDebugLog(true) c.SetDebugLog(true)
if err := c.Close(); err != nil { if err = c.Close(); err != nil {
t.Errorf("failed to close connection: %s", err) t.Errorf("failed to close connection: %s", err)
} }
} }
@ -698,19 +701,20 @@ func TestClient_DialWithContext_Debug_custom(t *testing.T) {
t.Skipf("failed to create test client: %s. Skipping tests", err) t.Skipf("failed to create test client: %s. Skipping tests", err)
} }
ctx := context.Background() ctx := context.Background()
if err := c.DialWithContext(ctx); err != nil { if err = c.DialWithContext(ctx); err != nil {
t.Errorf("failed to dial with context: %s", err) t.Errorf("failed to dial with context: %s", err)
return return
} }
if c.connection == nil {
t.Errorf("DialWithContext didn't fail but no connection found.")
}
if c.smtpClient == nil { if c.smtpClient == nil {
t.Errorf("DialWithContext didn't fail but no SMTP client found.") t.Errorf("DialWithContext didn't fail but no SMTP client found.")
return
}
if !c.smtpClient.HasConnection() {
t.Errorf("DialWithContext didn't fail but no connection found.")
} }
c.SetDebugLog(true) c.SetDebugLog(true)
c.SetLogger(log.New(os.Stderr, log.LevelDebug)) c.SetLogger(log.New(os.Stderr, log.LevelDebug))
if err := c.Close(); err != nil { if err = c.Close(); err != nil {
t.Errorf("failed to close connection: %s", err) t.Errorf("failed to close connection: %s", err)
} }
} }
@ -722,10 +726,9 @@ func TestClient_DialWithContextInvalidHost(t *testing.T) {
if err != nil { if err != nil {
t.Skipf("failed to create test client: %s. Skipping tests", err) t.Skipf("failed to create test client: %s. Skipping tests", err)
} }
c.connection = nil
c.host = "invalid.addr" c.host = "invalid.addr"
ctx := context.Background() ctx := context.Background()
if err := c.DialWithContext(ctx); err == nil { if err = c.DialWithContext(ctx); err == nil {
t.Errorf("dial succeeded but was supposed to fail") t.Errorf("dial succeeded but was supposed to fail")
return return
} }
@ -738,10 +741,9 @@ func TestClient_DialWithContextInvalidHELO(t *testing.T) {
if err != nil { if err != nil {
t.Skipf("failed to create test client: %s. Skipping tests", err) t.Skipf("failed to create test client: %s. Skipping tests", err)
} }
c.connection = nil
c.helo = "" c.helo = ""
ctx := context.Background() ctx := context.Background()
if err := c.DialWithContext(ctx); err == nil { if err = c.DialWithContext(ctx); err == nil {
t.Errorf("dial succeeded but was supposed to fail") t.Errorf("dial succeeded but was supposed to fail")
return return
} }
@ -758,7 +760,7 @@ func TestClient_DialWithContextInvalidAuth(t *testing.T) {
c.pass = "invalid" c.pass = "invalid"
c.SetSMTPAuthCustom(smtp.LoginAuth("invalid", "invalid", "invalid")) c.SetSMTPAuthCustom(smtp.LoginAuth("invalid", "invalid", "invalid"))
ctx := context.Background() ctx := context.Background()
if err := c.DialWithContext(ctx); err == nil { if err = c.DialWithContext(ctx); err == nil {
t.Errorf("dial succeeded but was supposed to fail") t.Errorf("dial succeeded but was supposed to fail")
return return
} }
@ -770,8 +772,7 @@ func TestClient_checkConn(t *testing.T) {
if err != nil { if err != nil {
t.Skipf("failed to create test client: %s. Skipping tests", err) t.Skipf("failed to create test client: %s. Skipping tests", err)
} }
c.connection = nil if err = c.checkConn(); err == nil {
if err := c.checkConn(); err == nil {
t.Errorf("connCheck() should fail but succeeded") t.Errorf("connCheck() should fail but succeeded")
} }
} }
@ -802,21 +803,23 @@ func TestClient_DialWithContextOptions(t *testing.T) {
} }
ctx := context.Background() ctx := context.Background()
if err := c.DialWithContext(ctx); err != nil && !tt.sf { if err = c.DialWithContext(ctx); err != nil && !tt.sf {
t.Errorf("failed to dial with context: %s", err) t.Errorf("failed to dial with context: %s", err)
return return
} }
if !tt.sf { if !tt.sf {
if c.connection == nil && !tt.sf {
t.Errorf("DialWithContext didn't fail but no connection found.")
}
if c.smtpClient == nil && !tt.sf { if c.smtpClient == nil && !tt.sf {
t.Errorf("DialWithContext didn't fail but no SMTP client found.") t.Errorf("DialWithContext didn't fail but no SMTP client found.")
return
} }
if err := c.Reset(); err != nil { if !c.smtpClient.HasConnection() && !tt.sf {
t.Errorf("DialWithContext didn't fail but no connection found.")
return
}
if err = c.Reset(); err != nil {
t.Errorf("failed to reset connection: %s", err) t.Errorf("failed to reset connection: %s", err)
} }
if err := c.Close(); err != nil { if err = c.Close(); err != nil {
t.Errorf("failed to close connection: %s", err) t.Errorf("failed to close connection: %s", err)
} }
} }
@ -1011,17 +1014,15 @@ func TestClient_DialSendCloseBroken(t *testing.T) {
} }
if tt.closestart { if tt.closestart {
_ = c.smtpClient.Close() _ = c.smtpClient.Close()
_ = c.connection.Close()
} }
if err := c.Send(m); err != nil && !tt.sf { if err = c.Send(m); err != nil && !tt.sf {
t.Errorf("Send() failed: %s", err) t.Errorf("Send() failed: %s", err)
return return
} }
if tt.closeearly { if tt.closeearly {
_ = c.smtpClient.Close() _ = c.smtpClient.Close()
_ = c.connection.Close()
} }
if err := c.Close(); err != nil && !tt.sf { if err = c.Close(); err != nil && !tt.sf {
t.Errorf("Close() failed: %s", err) t.Errorf("Close() failed: %s", err)
return return
} }
@ -1071,17 +1072,15 @@ func TestClient_DialSendCloseBrokenWithDSN(t *testing.T) {
} }
if tt.closestart { if tt.closestart {
_ = c.smtpClient.Close() _ = c.smtpClient.Close()
_ = c.connection.Close()
} }
if err := c.Send(m); err != nil && !tt.sf { if err = c.Send(m); err != nil && !tt.sf {
t.Errorf("Send() failed: %s", err) t.Errorf("Send() failed: %s", err)
return return
} }
if tt.closeearly { if tt.closeearly {
_ = c.smtpClient.Close() _ = c.smtpClient.Close()
_ = c.connection.Close()
} }
if err := c.Close(); err != nil && !tt.sf { if err = c.Close(); err != nil && !tt.sf {
t.Errorf("Close() failed: %s", err) t.Errorf("Close() failed: %s", err)
return return
} }