github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/utils/jsonutils/json_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package jsonutils_test
     5  
     6  import (
     7  	"encoding/json"
     8  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/pkg/errors"
    12  	"github.com/stretchr/testify/assert"
    13  
    14  	"github.com/mattermost/mattermost-server/utils/jsonutils"
    15  )
    16  
    17  func TestHumanizeJsonError(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	type testType struct{}
    21  
    22  	testCases := []struct {
    23  		Description string
    24  		Data        []byte
    25  		Err         error
    26  		ExpectedErr string
    27  	}{
    28  		{
    29  			"nil error",
    30  			[]byte{},
    31  			nil,
    32  			"",
    33  		},
    34  		{
    35  			"non-special error",
    36  			[]byte{},
    37  			errors.New("test"),
    38  			"test",
    39  		},
    40  		{
    41  			"syntax error, offset 17, middle of line 3",
    42  			[]byte("line 1\nline 2\nline 3"),
    43  			&json.SyntaxError{
    44  				// msg can't be set
    45  				Offset: 17,
    46  			},
    47  			"parsing error at line 3, character 4: ",
    48  		},
    49  		{
    50  			"unmarshal type error, offset 17, middle of line 3",
    51  			[]byte("line 1\nline 2\nline 3"),
    52  			&json.UnmarshalTypeError{
    53  				Value:  "bool",
    54  				Type:   reflect.TypeOf(testType{}),
    55  				Offset: 17,
    56  				Struct: "struct",
    57  				Field:  "field",
    58  			},
    59  			"parsing error at line 3, character 4: json: cannot unmarshal bool into Go struct field struct.field of type jsonutils_test.testType",
    60  		},
    61  	}
    62  
    63  	for _, testCase := range testCases {
    64  		testCase := testCase
    65  		t.Run(testCase.Description, func(t *testing.T) {
    66  			actual := jsonutils.HumanizeJsonError(testCase.Err, testCase.Data)
    67  			if testCase.ExpectedErr == "" {
    68  				assert.NoError(t, actual)
    69  			} else {
    70  				assert.EqualError(t, actual, testCase.ExpectedErr)
    71  			}
    72  		})
    73  	}
    74  }
    75  
    76  func TestNewHumanizedJsonError(t *testing.T) {
    77  	t.Parallel()
    78  
    79  	type testType struct{}
    80  
    81  	testCases := []struct {
    82  		Description string
    83  		Data        []byte
    84  		Offset      int64
    85  		Err         error
    86  		Expected    *jsonutils.HumanizedJsonError
    87  	}{
    88  		{
    89  			"nil error",
    90  			[]byte{},
    91  			0,
    92  			nil,
    93  			nil,
    94  		},
    95  		{
    96  			"offset -1, before start of string",
    97  			[]byte("line 1\nline 2\nline 3"),
    98  			-1,
    99  			errors.New("message"),
   100  			&jsonutils.HumanizedJsonError{
   101  				Err: errors.Wrap(errors.New("message"), "invalid offset -1"),
   102  			},
   103  		},
   104  		{
   105  			"offset 0, start of string",
   106  			[]byte("line 1\nline 2\nline 3"),
   107  			0,
   108  			errors.New("message"),
   109  			&jsonutils.HumanizedJsonError{
   110  				Err:       errors.Wrap(errors.New("message"), "parsing error at line 1, character 1"),
   111  				Line:      1,
   112  				Character: 1,
   113  			},
   114  		},
   115  		{
   116  			"offset 5, end of line 1",
   117  			[]byte("line 1\nline 2\nline 3"),
   118  			5,
   119  			errors.New("message"),
   120  			&jsonutils.HumanizedJsonError{
   121  				Err:       errors.Wrap(errors.New("message"), "parsing error at line 1, character 6"),
   122  				Line:      1,
   123  				Character: 6,
   124  			},
   125  		},
   126  		{
   127  			"offset 6, new line at end end of line 1",
   128  			[]byte("line 1\nline 2\nline 3"),
   129  			6,
   130  			errors.New("message"),
   131  			&jsonutils.HumanizedJsonError{
   132  				Err:       errors.Wrap(errors.New("message"), "parsing error at line 1, character 7"),
   133  				Line:      1,
   134  				Character: 7,
   135  			},
   136  		},
   137  		{
   138  			"offset 7, start of line 2",
   139  			[]byte("line 1\nline 2\nline 3"),
   140  			7,
   141  			errors.New("message"),
   142  			&jsonutils.HumanizedJsonError{
   143  				Err:       errors.Wrap(errors.New("message"), "parsing error at line 2, character 1"),
   144  				Line:      2,
   145  				Character: 1,
   146  			},
   147  		},
   148  		{
   149  			"offset 12, end of line 2",
   150  			[]byte("line 1\nline 2\nline 3"),
   151  			12,
   152  			errors.New("message"),
   153  			&jsonutils.HumanizedJsonError{
   154  				Err:       errors.Wrap(errors.New("message"), "parsing error at line 2, character 6"),
   155  				Line:      2,
   156  				Character: 6,
   157  			},
   158  		},
   159  		{
   160  			"offset 13, newline at end of line 2",
   161  			[]byte("line 1\nline 2\nline 3"),
   162  			13,
   163  			errors.New("message"),
   164  			&jsonutils.HumanizedJsonError{
   165  				Err:       errors.Wrap(errors.New("message"), "parsing error at line 2, character 7"),
   166  				Line:      2,
   167  				Character: 7,
   168  			},
   169  		},
   170  		{
   171  			"offset 17, middle of line 3",
   172  			[]byte("line 1\nline 2\nline 3"),
   173  			17,
   174  			errors.New("message"),
   175  			&jsonutils.HumanizedJsonError{
   176  				Err:       errors.Wrap(errors.New("message"), "parsing error at line 3, character 4"),
   177  				Line:      3,
   178  				Character: 4,
   179  			},
   180  		},
   181  		{
   182  			"offset 19, end of string",
   183  			[]byte("line 1\nline 2\nline 3"),
   184  			19,
   185  			errors.New("message"),
   186  			&jsonutils.HumanizedJsonError{
   187  				Err:       errors.Wrap(errors.New("message"), "parsing error at line 3, character 6"),
   188  				Line:      3,
   189  				Character: 6,
   190  			},
   191  		},
   192  		{
   193  			"offset 20, offset = length of string",
   194  			[]byte("line 1\nline 2\nline 3"),
   195  			20,
   196  			errors.New("message"),
   197  			&jsonutils.HumanizedJsonError{
   198  				Err:       errors.Wrap(errors.New("message"), "parsing error at line 3, character 7"),
   199  				Line:      3,
   200  				Character: 7,
   201  			},
   202  		},
   203  		{
   204  			"offset 21, offset = length of string, after newline",
   205  			[]byte("line 1\nline 2\nline 3\n"),
   206  			21,
   207  			errors.New("message"),
   208  			&jsonutils.HumanizedJsonError{
   209  				Err:       errors.Wrap(errors.New("message"), "parsing error at line 4, character 1"),
   210  				Line:      4,
   211  				Character: 1,
   212  			},
   213  		},
   214  		{
   215  			"offset 21, offset > length of string",
   216  			[]byte("line 1\nline 2\nline 3"),
   217  			21,
   218  			errors.New("message"),
   219  			&jsonutils.HumanizedJsonError{
   220  				Err: errors.Wrap(errors.New("message"), "invalid offset 21"),
   221  			},
   222  		},
   223  	}
   224  
   225  	for _, testCase := range testCases {
   226  		testCase := testCase
   227  		t.Run(testCase.Description, func(t *testing.T) {
   228  			actual := jsonutils.NewHumanizedJsonError(testCase.Err, testCase.Data, testCase.Offset)
   229  			if testCase.Expected != nil && actual.Err != nil {
   230  				if assert.EqualValues(t, testCase.Expected.Err.Error(), actual.Err.Error()) {
   231  					actual.Err = testCase.Expected.Err
   232  				}
   233  			}
   234  			assert.Equal(t, testCase.Expected, actual)
   235  		})
   236  	}
   237  }