github.com/moby/docker@v26.1.3+incompatible/errdefs/http_helpers_test.go (about) 1 package errdefs 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 ) 8 9 func TestFromStatusCode(t *testing.T) { 10 testErr := fmt.Errorf("some error occurred") 11 12 testCases := []struct { 13 err error 14 status int 15 check func(error) bool 16 }{ 17 { 18 err: testErr, 19 status: http.StatusNotFound, 20 check: IsNotFound, 21 }, 22 { 23 err: testErr, 24 status: http.StatusBadRequest, 25 check: IsInvalidParameter, 26 }, 27 { 28 err: testErr, 29 status: http.StatusConflict, 30 check: IsConflict, 31 }, 32 { 33 err: testErr, 34 status: http.StatusUnauthorized, 35 check: IsUnauthorized, 36 }, 37 { 38 err: testErr, 39 status: http.StatusServiceUnavailable, 40 check: IsUnavailable, 41 }, 42 { 43 err: testErr, 44 status: http.StatusForbidden, 45 check: IsForbidden, 46 }, 47 { 48 err: testErr, 49 status: http.StatusNotModified, 50 check: IsNotModified, 51 }, 52 { 53 err: testErr, 54 status: http.StatusNotImplemented, 55 check: IsNotImplemented, 56 }, 57 { 58 err: testErr, 59 status: http.StatusInternalServerError, 60 check: IsSystem, 61 }, 62 { 63 err: Unknown(testErr), 64 status: http.StatusInternalServerError, 65 check: IsUnknown, 66 }, 67 { 68 err: DataLoss(testErr), 69 status: http.StatusInternalServerError, 70 check: IsDataLoss, 71 }, 72 { 73 err: Deadline(testErr), 74 status: http.StatusInternalServerError, 75 check: IsDeadline, 76 }, 77 { 78 err: Cancelled(testErr), 79 status: http.StatusInternalServerError, 80 check: IsCancelled, 81 }, 82 } 83 84 for _, tc := range testCases { 85 t.Run(http.StatusText(tc.status), func(t *testing.T) { 86 err := FromStatusCode(tc.err, tc.status) 87 if !tc.check(err) { 88 t.Errorf("unexpected error-type %T", err) 89 } 90 }) 91 } 92 }