mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-23 14:10:50 +01:00
Refactor error code functions and add enhanced status code tests
Renamed `getErrorCode` function to `errorCode` for consistency. Added new tests for the `enhancedStatusCode` function to validate its behavior with various error scenarios.
This commit is contained in:
parent
6268acac44
commit
f367db0278
1 changed files with 49 additions and 16 deletions
|
@ -255,7 +255,7 @@ func TestSendError_ErrorCode(t *testing.T) {
|
||||||
errlist: []error{ErrNoRcptAddresses},
|
errlist: []error{ErrNoRcptAddresses},
|
||||||
rcpt: []string{"<toni.tester@domain.tld>", "<tina.tester@domain.tld>"},
|
rcpt: []string{"<toni.tester@domain.tld>", "<tina.tester@domain.tld>"},
|
||||||
Reason: ErrAmbiguous,
|
Reason: ErrAmbiguous,
|
||||||
errcode: getErrorCode(ErrNoRcptAddresses),
|
errcode: errorCode(ErrNoRcptAddresses),
|
||||||
}
|
}
|
||||||
if err.ErrorCode() != 0 {
|
if err.ErrorCode() != 0 {
|
||||||
t.Errorf("expected error code: %d, got: %d", 0, err.ErrorCode())
|
t.Errorf("expected error code: %d, got: %d", 0, err.ErrorCode())
|
||||||
|
@ -266,7 +266,7 @@ func TestSendError_ErrorCode(t *testing.T) {
|
||||||
errlist: []error{ErrNoRcptAddresses},
|
errlist: []error{ErrNoRcptAddresses},
|
||||||
rcpt: []string{"<toni.tester@domain.tld>", "<tina.tester@domain.tld>"},
|
rcpt: []string{"<toni.tester@domain.tld>", "<tina.tester@domain.tld>"},
|
||||||
Reason: ErrAmbiguous,
|
Reason: ErrAmbiguous,
|
||||||
errcode: getErrorCode(errors.New("535 5.7.8 Error: authentication failed")),
|
errcode: errorCode(errors.New("535 5.7.8 Error: authentication failed")),
|
||||||
}
|
}
|
||||||
if err.ErrorCode() != 535 {
|
if err.ErrorCode() != 535 {
|
||||||
t.Errorf("expected error code: %d, got: %d", 535, err.ErrorCode())
|
t.Errorf("expected error code: %d, got: %d", 535, err.ErrorCode())
|
||||||
|
@ -277,7 +277,7 @@ func TestSendError_ErrorCode(t *testing.T) {
|
||||||
errlist: []error{ErrNoRcptAddresses},
|
errlist: []error{ErrNoRcptAddresses},
|
||||||
rcpt: []string{"<toni.tester@domain.tld>", "<tina.tester@domain.tld>"},
|
rcpt: []string{"<toni.tester@domain.tld>", "<tina.tester@domain.tld>"},
|
||||||
Reason: ErrAmbiguous,
|
Reason: ErrAmbiguous,
|
||||||
errcode: getErrorCode(errors.New("441 4.1.0 Server currently unavailable")),
|
errcode: errorCode(errors.New("441 4.1.0 Server currently unavailable")),
|
||||||
}
|
}
|
||||||
if err.ErrorCode() != 441 {
|
if err.ErrorCode() != 441 {
|
||||||
t.Errorf("expected error code: %d, got: %d", 441, err.ErrorCode())
|
t.Errorf("expected error code: %d, got: %d", 441, err.ErrorCode())
|
||||||
|
@ -291,45 +291,78 @@ func TestSendError_ErrorCode(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSendError_getErrorCode(t *testing.T) {
|
func TestSendError_errorCode(t *testing.T) {
|
||||||
t.Run("getErrorCode with a go-mail error should return 0", func(t *testing.T) {
|
t.Run("errorCode with a go-mail error should return 0", func(t *testing.T) {
|
||||||
code := getErrorCode(ErrNoRcptAddresses)
|
code := errorCode(ErrNoRcptAddresses)
|
||||||
if code != 0 {
|
if code != 0 {
|
||||||
t.Errorf("expected error code: %d, got: %d", 0, code)
|
t.Errorf("expected error code: %d, got: %d", 0, code)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
t.Run("getErrorCode with permanent error", func(t *testing.T) {
|
t.Run("errorCode with permanent error", func(t *testing.T) {
|
||||||
code := getErrorCode(errors.New("535 5.7.8 Error: authentication failed"))
|
code := errorCode(errors.New("535 5.7.8 Error: authentication failed"))
|
||||||
if code != 535 {
|
if code != 535 {
|
||||||
t.Errorf("expected error code: %d, got: %d", 535, code)
|
t.Errorf("expected error code: %d, got: %d", 535, code)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
t.Run("getErrorCode with temporary error", func(t *testing.T) {
|
t.Run("errorCode with temporary error", func(t *testing.T) {
|
||||||
code := getErrorCode(errors.New("443 4.1.0 Server currently unavailable"))
|
code := errorCode(errors.New("443 4.1.0 Server currently unavailable"))
|
||||||
if code != 443 {
|
if code != 443 {
|
||||||
t.Errorf("expected error code: %d, got: %d", 443, code)
|
t.Errorf("expected error code: %d, got: %d", 443, code)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
t.Run("getErrorCode with wrapper error", func(t *testing.T) {
|
t.Run("errorCode with wrapper error", func(t *testing.T) {
|
||||||
code := getErrorCode(fmt.Errorf("an error occured: %w", errors.New("443 4.1.0 Server currently unavailable")))
|
code := errorCode(fmt.Errorf("an error occured: %w", errors.New("443 4.1.0 Server currently unavailable")))
|
||||||
if code != 443 {
|
if code != 443 {
|
||||||
t.Errorf("expected error code: %d, got: %d", 443, code)
|
t.Errorf("expected error code: %d, got: %d", 443, code)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
t.Run("getErrorCode with non-4xx and non-5xx error", func(t *testing.T) {
|
t.Run("errorCode with non-4xx and non-5xx error", func(t *testing.T) {
|
||||||
code := getErrorCode(errors.New("220 2.1.0 This is not an error"))
|
code := errorCode(errors.New("220 2.1.0 This is not an error"))
|
||||||
if code != 0 {
|
if code != 0 {
|
||||||
t.Errorf("expected error code: %d, got: %d", 0, code)
|
t.Errorf("expected error code: %d, got: %d", 0, code)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
t.Run("getErrorCode with non 3-digit code", func(t *testing.T) {
|
t.Run("errorCode with non 3-digit code", func(t *testing.T) {
|
||||||
code := getErrorCode(errors.New("4xx 4.1.0 The status code is invalid"))
|
code := errorCode(errors.New("4xx 4.1.0 The status code is invalid"))
|
||||||
if code != 0 {
|
if code != 0 {
|
||||||
t.Errorf("expected error code: %d, got: %d", 0, code)
|
t.Errorf("expected error code: %d, got: %d", 0, code)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSendError_enhancedStatusCode(t *testing.T) {
|
||||||
|
t.Run("enhancedStatusCode with nil error should return empty string", func(t *testing.T) {
|
||||||
|
code := enhancedStatusCode(nil, true)
|
||||||
|
if code != "" {
|
||||||
|
t.Errorf("expected empty enhanced status code, got: %s", code)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("enhancedStatusCode with error but no support should return empty string", func(t *testing.T) {
|
||||||
|
code := enhancedStatusCode(errors.New("553 5.5.3 something went wrong"), false)
|
||||||
|
if code != "" {
|
||||||
|
t.Errorf("expected empty enhanced status code, got: %s", code)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("enhancedStatusCode with error and support", func(t *testing.T) {
|
||||||
|
code := enhancedStatusCode(errors.New("553 5.5.3 something went wrong"), true)
|
||||||
|
if code != "5.5.3" {
|
||||||
|
t.Errorf("expected enhanced status code: %s, got: %s", "5.5.3", code)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("enhancedStatusCode with wrapped error and support", func(t *testing.T) {
|
||||||
|
code := enhancedStatusCode(fmt.Errorf("this error is wrapped: %w", errors.New("553 5.5.3 something went wrong")), true)
|
||||||
|
if code != "5.5.3" {
|
||||||
|
t.Errorf("expected enhanced status code: %s, got: %s", "5.5.3", code)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("enhancedStatusCode with 3xx error", func(t *testing.T) {
|
||||||
|
code := enhancedStatusCode(errors.New("300 3.0.0 i don't know what i'm doing"), true)
|
||||||
|
if code != "" {
|
||||||
|
t.Errorf("expected enhanced status code to be empty, got: %s", code)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// returnSendError is a helper method to retunr a SendError with a specific reason
|
// returnSendError is a helper method to retunr a SendError with a specific reason
|
||||||
func returnSendError(r SendErrReason, t bool) error {
|
func returnSendError(r SendErrReason, t bool) error {
|
||||||
message := NewMsg()
|
message := NewMsg()
|
||||||
|
|
Loading…
Reference in a new issue