errdefs/http_helpers_test.go
2a9c987e
 package errdefs
1af30c50
 
 import (
 	"fmt"
 	"net/http"
 	"testing"
 
 	"gotest.tools/assert"
 )
 
 func TestFromStatusCode(t *testing.T) {
 	testErr := fmt.Errorf("some error occurred")
 
 	testCases := []struct {
 		err    error
 		status int
 		check  func(error) bool
 	}{
 		{
 			err:    testErr,
 			status: http.StatusNotFound,
2a9c987e
 			check:  IsNotFound,
1af30c50
 		},
 		{
 			err:    testErr,
 			status: http.StatusBadRequest,
2a9c987e
 			check:  IsInvalidParameter,
1af30c50
 		},
 		{
 			err:    testErr,
 			status: http.StatusConflict,
2a9c987e
 			check:  IsConflict,
1af30c50
 		},
 		{
 			err:    testErr,
 			status: http.StatusUnauthorized,
2a9c987e
 			check:  IsUnauthorized,
1af30c50
 		},
 		{
 			err:    testErr,
 			status: http.StatusServiceUnavailable,
2a9c987e
 			check:  IsUnavailable,
1af30c50
 		},
 		{
 			err:    testErr,
 			status: http.StatusForbidden,
2a9c987e
 			check:  IsForbidden,
1af30c50
 		},
 		{
 			err:    testErr,
 			status: http.StatusNotModified,
2a9c987e
 			check:  IsNotModified,
1af30c50
 		},
 		{
 			err:    testErr,
 			status: http.StatusNotImplemented,
2a9c987e
 			check:  IsNotImplemented,
1af30c50
 		},
 		{
 			err:    testErr,
 			status: http.StatusInternalServerError,
2a9c987e
 			check:  IsSystem,
1af30c50
 		},
 		{
2a9c987e
 			err:    Unknown(testErr),
1af30c50
 			status: http.StatusInternalServerError,
2a9c987e
 			check:  IsUnknown,
1af30c50
 		},
 		{
2a9c987e
 			err:    DataLoss(testErr),
1af30c50
 			status: http.StatusInternalServerError,
2a9c987e
 			check:  IsDataLoss,
1af30c50
 		},
 		{
2a9c987e
 			err:    Deadline(testErr),
1af30c50
 			status: http.StatusInternalServerError,
2a9c987e
 			check:  IsDeadline,
1af30c50
 		},
 		{
2a9c987e
 			err:    Cancelled(testErr),
1af30c50
 			status: http.StatusInternalServerError,
2a9c987e
 			check:  IsCancelled,
1af30c50
 		},
 	}
 
 	for _, tc := range testCases {
 		t.Run(http.StatusText(tc.status), func(t *testing.T) {
 			err := FromStatusCode(tc.err, tc.status)
 			assert.Check(t, tc.check(err), "unexpected error-type %T", err)
 		})
 	}
 }