Add DSN and dial context func options to client tests

Expanded the client test cases to include DSN mail return types and multiple DSN recipient notify types. Also added tests for setting dial context functions using both net.Dialer and tls.Dialer, including error handling for invalid options.
This commit is contained in:
Winni Neessen 2024-10-23 13:51:48 +02:00
parent 35f92f2ddc
commit ab8fc3e4fc
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -8,6 +8,7 @@ import (
"crypto/tls" "crypto/tls"
"errors" "errors"
"fmt" "fmt"
"net"
"os" "os"
"reflect" "reflect"
"testing" "testing"
@ -85,6 +86,8 @@ func TestNewClient(t *testing.T) {
}) })
t.Run("NewClient with option", func(t *testing.T) { t.Run("NewClient with option", func(t *testing.T) {
hostname := "mail.example.com" hostname := "mail.example.com"
netDailer := net.Dialer{}
tlsDailer := tls.Dialer{NetDialer: &netDailer, Config: &tls.Config{}}
tests := []struct { tests := []struct {
name string name string
option Option option Option
@ -438,6 +441,174 @@ func TestNewClient(t *testing.T) {
}, },
false, nil, false, nil,
}, },
{
"WithDSNMailReturnType DSNMailReturnHeadersOnly",
WithDSNMailReturnType(DSNMailReturnHeadersOnly),
func(c *Client) error {
if c.requestDSN != true {
return fmt.Errorf("failed to enable DSN. Want requestDSN: %t, got: %t", true,
c.requestDSN)
}
if c.dsnReturnType != DSNMailReturnHeadersOnly {
return fmt.Errorf("failed to enable DSN. Want dsnReturnType: %s, got: %s",
DSNMailReturnHeadersOnly, c.dsnReturnType)
}
return nil
},
false, nil,
},
{
"WithDSNMailReturnType DSNMailReturnFull",
WithDSNMailReturnType(DSNMailReturnFull),
func(c *Client) error {
if c.requestDSN != true {
return fmt.Errorf("failed to enable DSN. Want requestDSN: %t, got: %t", true,
c.requestDSN)
}
if c.dsnReturnType != DSNMailReturnFull {
return fmt.Errorf("failed to enable DSN. Want dsnReturnType: %s, got: %s",
DSNMailReturnFull, c.dsnReturnType)
}
return nil
},
false, nil,
},
{
"WithDSNMailReturnType invalid", WithDSNMailReturnType("invalid"), nil,
true, &ErrInvalidDSNMailReturnOption,
},
{
"WithDSNRcptNotifyType DSNRcptNotifyNever",
WithDSNRcptNotifyType(DSNRcptNotifyNever),
func(c *Client) error {
if c.requestDSN != true {
return fmt.Errorf("failed to enable DSN. Want requestDSN: %t, got: %t", true,
c.requestDSN)
}
if len(c.dsnRcptNotifyType) != 1 {
return fmt.Errorf("failed to enable DSN. Want 1 DSN Rcpt Notify type, got: %d",
len(c.dsnRcptNotifyType))
}
if c.dsnRcptNotifyType[0] != string(DSNRcptNotifyNever) {
return fmt.Errorf("failed to enable DSN. Want DSN Rcpt Notify Never type: %s, got: %s",
string(DSNRcptNotifyNever), c.dsnRcptNotifyType[0])
}
return nil
},
false, nil,
},
{
"WithDSNRcptNotifyType DSNRcptNotifySuccess, DSNRcptNotifyFailure",
WithDSNRcptNotifyType(DSNRcptNotifySuccess, DSNRcptNotifyFailure),
func(c *Client) error {
if c.requestDSN != true {
return fmt.Errorf("failed to enable DSN. Want requestDSN: %t, got: %t", true,
c.requestDSN)
}
if len(c.dsnRcptNotifyType) != 2 {
return fmt.Errorf("failed to enable DSN. Want 2 DSN Rcpt Notify type, got: %d",
len(c.dsnRcptNotifyType))
}
if c.dsnRcptNotifyType[0] != string(DSNRcptNotifySuccess) {
return fmt.Errorf("failed to enable DSN. Want DSN Rcpt Notify Success type: %s, got: %s",
string(DSNRcptNotifySuccess), c.dsnRcptNotifyType[0])
}
if c.dsnRcptNotifyType[1] != string(DSNRcptNotifyFailure) {
return fmt.Errorf("failed to enable DSN. Want DSN Rcpt Notify Failure type: %s, got: %s",
string(DSNRcptNotifyFailure), c.dsnRcptNotifyType[1])
}
return nil
},
false, nil,
},
{
"WithDSNRcptNotifyType DSNRcptNotifyDelay",
WithDSNRcptNotifyType(DSNRcptNotifyDelay),
func(c *Client) error {
if c.requestDSN != true {
return fmt.Errorf("failed to enable DSN. Want requestDSN: %t, got: %t", true,
c.requestDSN)
}
if len(c.dsnRcptNotifyType) != 1 {
return fmt.Errorf("failed to enable DSN. Want 1 DSN Rcpt Notify type, got: %d",
len(c.dsnRcptNotifyType))
}
if c.dsnRcptNotifyType[0] != string(DSNRcptNotifyDelay) {
return fmt.Errorf("failed to enable DSN. Want DSN Rcpt Notify Delay type: %s, got: %s",
string(DSNRcptNotifyDelay), c.dsnRcptNotifyType[0])
}
return nil
},
false, nil,
},
{
"WithDSNRcptNotifyType invalid", WithDSNRcptNotifyType("invalid"), nil,
true, &ErrInvalidDSNRcptNotifyOption,
},
{
"WithDSNRcptNotifyType mix valid and invalid",
WithDSNRcptNotifyType(DSNRcptNotifyDelay, "invalid"), nil,
true, &ErrInvalidDSNRcptNotifyOption,
},
{
"WithDSNRcptNotifyType mix NEVER with SUCCESS",
WithDSNRcptNotifyType(DSNRcptNotifyNever, DSNRcptNotifySuccess), nil,
true, &ErrInvalidDSNRcptNotifyCombination,
},
{
"WithDSNRcptNotifyType mix NEVER with FAIL",
WithDSNRcptNotifyType(DSNRcptNotifyNever, DSNRcptNotifyFailure), nil,
true, &ErrInvalidDSNRcptNotifyCombination,
},
{
"WithDSNRcptNotifyType mix NEVER with DELAY",
WithDSNRcptNotifyType(DSNRcptNotifyNever, DSNRcptNotifyDelay), nil,
true, &ErrInvalidDSNRcptNotifyCombination,
},
{
"WithoutNoop", WithoutNoop(),
func(c *Client) error {
if !c.noNoop {
return fmt.Errorf("failed to disable Noop. Want noNoop: %t, got: %t", false, c.noNoop)
}
return nil
},
false, nil,
},
{
"WithDialContextFunc with net.Dailer", WithDialContextFunc(netDailer.DialContext),
func(c *Client) error {
if c.dialContextFunc == nil {
return errors.New("failed to set dial context func, got: nil")
}
ctxType := reflect.TypeOf(c.dialContextFunc).String()
if ctxType != "mail.DialContextFunc" {
return fmt.Errorf("failed to set dial context func, want: %s, got: %s",
"mail.DialContextFunc", ctxType)
}
return nil
},
false, nil,
},
{
"WithDialContextFunc with tls.Dailer", WithDialContextFunc(tlsDailer.DialContext),
func(c *Client) error {
if c.dialContextFunc == nil {
return errors.New("failed to set dial context func, got: nil")
}
ctxType := reflect.TypeOf(c.dialContextFunc).String()
if ctxType != "mail.DialContextFunc" {
return fmt.Errorf("failed to set dial context func, want: %s, got: %s",
"mail.DialContextFunc", ctxType)
}
return nil
},
false, nil,
},
{
"WithDialContextFunc with nil", WithDialContextFunc(nil), nil,
true, &ErrDialContextFuncIsNil,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -502,11 +673,6 @@ func TestNewClientWithOptions(t *testing.T) {
option Option option Option
shouldfail bool shouldfail bool
}{ }{
{"WithDSN()", WithDSN(), false},
{"WithDSNMailReturnType()", WithDSNMailReturnType(DSNMailReturnFull), false},
{"WithDSNMailReturnType() wrong option", WithDSNMailReturnType("FAIL"), true},
{"WithDSNRcptNotifyType()", WithDSNRcptNotifyType(DSNRcptNotifySuccess), false},
{"WithDSNRcptNotifyType() wrong option", WithDSNRcptNotifyType("FAIL"), true},
{"WithoutNoop()", WithoutNoop(), false}, {"WithoutNoop()", WithoutNoop(), false},
{"WithDebugLog()", WithDebugLog(), false}, {"WithDebugLog()", WithDebugLog(), false},
{"WithLogger()", WithLogger(log.New(os.Stderr, log.LevelDebug)), false}, {"WithLogger()", WithLogger(log.New(os.Stderr, log.LevelDebug)), false},