go.uber.org/yarpc@v1.72.1/pkg/errors/errors_test.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package errors
    22  
    23  import (
    24  	"errors"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"go.uber.org/yarpc/api/transport"
    29  	"go.uber.org/yarpc/yarpcerrors"
    30  )
    31  
    32  func TestWrapHandlerError(t *testing.T) {
    33  	assert.Nil(t, WrapHandlerError(nil, "foo", "bar"))
    34  	assert.Equal(t, yarpcerrors.CodeInvalidArgument, yarpcerrors.FromError(WrapHandlerError(yarpcerrors.Newf(yarpcerrors.CodeInvalidArgument, ""), "foo", "bar")).Code())
    35  	assert.Equal(t, yarpcerrors.CodeUnknown, yarpcerrors.FromError(WrapHandlerError(errors.New(""), "foo", "bar")).Code())
    36  }
    37  
    38  func TestExpectEncodings(t *testing.T) {
    39  	assert.Error(t, ExpectEncodings(&transport.Request{}, "foo"))
    40  	assert.NoError(t, ExpectEncodings(&transport.Request{Encoding: "foo"}, "foo"))
    41  	assert.NoError(t, ExpectEncodings(&transport.Request{Encoding: "foo"}, "foo", "bar"))
    42  	assert.Error(t, ExpectEncodings(&transport.Request{Encoding: "foo"}, "bar"))
    43  	assert.Error(t, ExpectEncodings(&transport.Request{Encoding: "foo"}, "bar", "baz"))
    44  }
    45  
    46  func TestEncodeErrors(t *testing.T) {
    47  	tests := []struct {
    48  		errorFunc     func(*transport.Request, error) error
    49  		expectedCode  yarpcerrors.Code
    50  		expectedWords []string
    51  	}{
    52  		{
    53  			errorFunc:     RequestBodyEncodeError,
    54  			expectedCode:  yarpcerrors.CodeInvalidArgument,
    55  			expectedWords: []string{"request", "body", "encode"},
    56  		},
    57  		{
    58  			errorFunc:     RequestHeadersEncodeError,
    59  			expectedCode:  yarpcerrors.CodeInvalidArgument,
    60  			expectedWords: []string{"request", "headers", "encode"},
    61  		},
    62  		{
    63  			errorFunc:     RequestBodyDecodeError,
    64  			expectedCode:  yarpcerrors.CodeInvalidArgument,
    65  			expectedWords: []string{"request", "body", "decode"},
    66  		},
    67  		{
    68  			errorFunc:     RequestHeadersDecodeError,
    69  			expectedCode:  yarpcerrors.CodeInvalidArgument,
    70  			expectedWords: []string{"request", "headers", "decode"},
    71  		},
    72  		{
    73  			errorFunc:     ResponseBodyEncodeError,
    74  			expectedCode:  yarpcerrors.CodeInvalidArgument,
    75  			expectedWords: []string{"response", "body", "encode"},
    76  		},
    77  		{
    78  			errorFunc:     ResponseHeadersEncodeError,
    79  			expectedCode:  yarpcerrors.CodeInvalidArgument,
    80  			expectedWords: []string{"response", "headers", "encode"},
    81  		},
    82  		{
    83  			errorFunc:     ResponseBodyDecodeError,
    84  			expectedCode:  yarpcerrors.CodeInvalidArgument,
    85  			expectedWords: []string{"response", "body", "decode"},
    86  		},
    87  		{
    88  			errorFunc:     ResponseHeadersDecodeError,
    89  			expectedCode:  yarpcerrors.CodeInvalidArgument,
    90  			expectedWords: []string{"response", "headers", "decode"},
    91  		},
    92  	}
    93  	request := &transport.Request{}
    94  	for _, tt := range tests {
    95  		assertError(t, tt.errorFunc(request, errors.New("")), tt.expectedCode, tt.expectedWords...)
    96  	}
    97  }
    98  
    99  func assertError(t *testing.T, err error, expectedCode yarpcerrors.Code, expectedWords ...string) {
   100  	assert.Error(t, err)
   101  	assert.Equal(t, expectedCode, yarpcerrors.FromError(err).Code())
   102  	for _, expectedWord := range expectedWords {
   103  		assert.Contains(t, err.Error(), expectedWord)
   104  	}
   105  }