mirror of
https://github.com/wneessen/go-mail.git
synced 2024-12-23 02:50:39 +01:00
Add tests for getErrorCode function
Introduce a suite of unit tests for the getErrorCode function to validate its behavior with various error types, including go-mail errors, permanent and temporary errors, wrapper errors, non-4xx/5xx errors, and non-3-digit codes.
This commit is contained in:
parent
615155bfc2
commit
e8fb977afe
1 changed files with 40 additions and 0 deletions
|
@ -6,6 +6,7 @@ package mail
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -290,6 +291,45 @@ func TestSendError_ErrorCode(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestSendError_getErrorCode(t *testing.T) {
|
||||
t.Run("getErrorCode with a go-mail error should return 0", func(t *testing.T) {
|
||||
code := getErrorCode(ErrNoRcptAddresses)
|
||||
if code != 0 {
|
||||
t.Errorf("expected error code: %d, got: %d", 0, code)
|
||||
}
|
||||
})
|
||||
t.Run("getErrorCode with permanent error", func(t *testing.T) {
|
||||
code := getErrorCode(errors.New("535 5.7.8 Error: authentication failed"))
|
||||
if code != 535 {
|
||||
t.Errorf("expected error code: %d, got: %d", 535, code)
|
||||
}
|
||||
})
|
||||
t.Run("getErrorCode with temporary error", func(t *testing.T) {
|
||||
code := getErrorCode(errors.New("443 4.1.0 Server currently unavailable"))
|
||||
if code != 443 {
|
||||
t.Errorf("expected error code: %d, got: %d", 443, code)
|
||||
}
|
||||
})
|
||||
t.Run("getErrorCode with wrapper error", func(t *testing.T) {
|
||||
code := getErrorCode(fmt.Errorf("an error occured: %w", errors.New("443 4.1.0 Server currently unavailable")))
|
||||
if code != 443 {
|
||||
t.Errorf("expected error code: %d, got: %d", 443, code)
|
||||
}
|
||||
})
|
||||
t.Run("getErrorCode with non-4xx and non-5xx error", func(t *testing.T) {
|
||||
code := getErrorCode(errors.New("220 2.1.0 This is not an error"))
|
||||
if code != 0 {
|
||||
t.Errorf("expected error code: %d, got: %d", 0, code)
|
||||
}
|
||||
})
|
||||
t.Run("getErrorCode with non 3-digit code", func(t *testing.T) {
|
||||
code := getErrorCode(errors.New("4xx 4.1.0 The status code is invalid"))
|
||||
if code != 0 {
|
||||
t.Errorf("expected error code: %d, got: %d", 0, code)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// returnSendError is a helper method to retunr a SendError with a specific reason
|
||||
func returnSendError(r SendErrReason, t bool) error {
|
||||
message := NewMsg()
|
||||
|
|
Loading…
Reference in a new issue