Files
moby/errdefs/http_helpers_test.go
Matthieu MOREL 381d9d0723 fix use-errors-new from revive
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2025-06-26 12:07:38 +00:00

94 lines
1.7 KiB
Go

package errdefs
import (
"errors"
"net/http"
"testing"
)
func TestFromStatusCode(t *testing.T) {
testErr := errors.New("some error occurred")
testCases := []struct {
err error
status int
check func(error) bool
}{
{
err: testErr,
status: http.StatusNotFound,
check: IsNotFound,
},
{
err: testErr,
status: http.StatusBadRequest,
check: IsInvalidParameter,
},
{
err: testErr,
status: http.StatusConflict,
check: IsConflict,
},
{
err: testErr,
status: http.StatusUnauthorized,
check: IsUnauthorized,
},
{
err: testErr,
status: http.StatusServiceUnavailable,
check: IsUnavailable,
},
{
err: testErr,
status: http.StatusForbidden,
check: IsForbidden,
},
{
err: testErr,
status: http.StatusNotModified,
check: IsNotModified,
},
{
err: testErr,
status: http.StatusNotImplemented,
check: IsNotImplemented,
},
{
err: testErr,
status: http.StatusInternalServerError,
check: IsSystem,
},
{
err: Unknown(testErr),
status: http.StatusInternalServerError,
check: IsUnknown,
},
{
err: DataLoss(testErr),
status: http.StatusInternalServerError,
check: IsDataLoss,
},
{
err: Deadline(testErr),
status: http.StatusInternalServerError,
check: IsDeadline,
},
{
err: Cancelled(testErr),
status: http.StatusInternalServerError,
check: IsCancelled,
},
}
for _, tc := range testCases {
t.Run(http.StatusText(tc.status), func(t *testing.T) {
//nolint:staticcheck // ignore SA1019: FromStatusCode is deprecated
err := FromStatusCode(tc.err, tc.status)
if !tc.check(err) {
t.Errorf("unexpected error-type %T", err)
}
})
}
}