github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/errdefs/http_helpers_test.go (about)

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