github.com/lbryio/lbcd@v0.22.119/btcjson/jsonrpc_test.go (about)

     1  // Copyright (c) 2014 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package btcjson_test
     6  
     7  import (
     8  	"encoding/json"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"github.com/lbryio/lbcd/btcjson"
    13  )
    14  
    15  // TestIsValidIDType ensures the IsValidIDType function behaves as expected.
    16  func TestIsValidIDType(t *testing.T) {
    17  	t.Parallel()
    18  
    19  	tests := []struct {
    20  		name    string
    21  		id      interface{}
    22  		isValid bool
    23  	}{
    24  		{"int", int(1), true},
    25  		{"int8", int8(1), true},
    26  		{"int16", int16(1), true},
    27  		{"int32", int32(1), true},
    28  		{"int64", int64(1), true},
    29  		{"uint", uint(1), true},
    30  		{"uint8", uint8(1), true},
    31  		{"uint16", uint16(1), true},
    32  		{"uint32", uint32(1), true},
    33  		{"uint64", uint64(1), true},
    34  		{"string", "1", true},
    35  		{"nil", nil, true},
    36  		{"float32", float32(1), true},
    37  		{"float64", float64(1), true},
    38  		{"bool", true, false},
    39  		{"chan int", make(chan int), false},
    40  		{"complex64", complex64(1), false},
    41  		{"complex128", complex128(1), false},
    42  		{"func", func() {}, false},
    43  	}
    44  
    45  	t.Logf("Running %d tests", len(tests))
    46  	for i, test := range tests {
    47  		if btcjson.IsValidIDType(test.id) != test.isValid {
    48  			t.Errorf("Test #%d (%s) valid mismatch - got %v, "+
    49  				"want %v", i, test.name, !test.isValid,
    50  				test.isValid)
    51  			continue
    52  		}
    53  	}
    54  }
    55  
    56  // TestMarshalResponse ensures the MarshalResponse function works as expected.
    57  func TestMarshalResponse(t *testing.T) {
    58  	t.Parallel()
    59  
    60  	testID := 1
    61  	tests := []struct {
    62  		name     string
    63  		result   interface{}
    64  		jsonErr  *btcjson.RPCError
    65  		expected []byte
    66  	}{
    67  		{
    68  			name:     "ordinary bool result with no error",
    69  			result:   true,
    70  			jsonErr:  nil,
    71  			expected: []byte(`{"jsonrpc":"1.0","result":true,"error":null,"id":1}`),
    72  		},
    73  		{
    74  			name:   "result with error",
    75  			result: nil,
    76  			jsonErr: func() *btcjson.RPCError {
    77  				return btcjson.NewRPCError(btcjson.ErrRPCBlockNotFound, "123 not found")
    78  			}(),
    79  			expected: []byte(`{"jsonrpc":"1.0","result":null,"error":{"code":-5,"message":"123 not found"},"id":1}`),
    80  		},
    81  	}
    82  
    83  	t.Logf("Running %d tests", len(tests))
    84  	for i, test := range tests {
    85  		_, _ = i, test
    86  		marshalled, err := btcjson.MarshalResponse(btcjson.RpcVersion1, testID, test.result, test.jsonErr)
    87  		if err != nil {
    88  			t.Errorf("Test #%d (%s) unexpected error: %v", i,
    89  				test.name, err)
    90  			continue
    91  		}
    92  
    93  		if !reflect.DeepEqual(marshalled, test.expected) {
    94  			t.Errorf("Test #%d (%s) mismatched result - got %s, "+
    95  				"want %s", i, test.name, marshalled,
    96  				test.expected)
    97  		}
    98  	}
    99  }
   100  
   101  // TestMiscErrors tests a few error conditions not covered elsewhere.
   102  func TestMiscErrors(t *testing.T) {
   103  	t.Parallel()
   104  
   105  	// Force an error in NewRequest by giving it a parameter type that is
   106  	// not supported.
   107  	_, err := btcjson.NewRequest(btcjson.RpcVersion1, nil, "test", []interface{}{make(chan int)})
   108  	if err == nil {
   109  		t.Error("NewRequest: did not receive error")
   110  		return
   111  	}
   112  
   113  	// Force an error in MarshalResponse by giving it an id type that is not
   114  	// supported.
   115  	wantErr := btcjson.Error{ErrorCode: btcjson.ErrInvalidType}
   116  	_, err = btcjson.MarshalResponse(btcjson.RpcVersion1, make(chan int), nil, nil)
   117  	if jerr, ok := err.(btcjson.Error); !ok || jerr.ErrorCode != wantErr.ErrorCode {
   118  		t.Errorf("MarshalResult: did not receive expected error - got "+
   119  			"%v (%[1]T), want %v (%[2]T)", err, wantErr)
   120  		return
   121  	}
   122  
   123  	// Force an error in MarshalResponse by giving it a result type that
   124  	// can't be marshalled.
   125  	_, err = btcjson.MarshalResponse(btcjson.RpcVersion1, 1, make(chan int), nil)
   126  	if _, ok := err.(*json.UnsupportedTypeError); !ok {
   127  		wantErr := &json.UnsupportedTypeError{}
   128  		t.Errorf("MarshalResult: did not receive expected error - got "+
   129  			"%v (%[1]T), want %T", err, wantErr)
   130  		return
   131  	}
   132  }
   133  
   134  // TestRPCError tests the error output for the RPCError type.
   135  func TestRPCError(t *testing.T) {
   136  	t.Parallel()
   137  
   138  	tests := []struct {
   139  		in   *btcjson.RPCError
   140  		want string
   141  	}{
   142  		{
   143  			btcjson.ErrRPCInvalidRequest,
   144  			"-32600: Invalid request",
   145  		},
   146  		{
   147  			btcjson.ErrRPCMethodNotFound,
   148  			"-32601: Method not found",
   149  		},
   150  	}
   151  
   152  	t.Logf("Running %d tests", len(tests))
   153  	for i, test := range tests {
   154  		result := test.in.Error()
   155  		if result != test.want {
   156  			t.Errorf("Error #%d\n got: %s want: %s", i, result,
   157  				test.want)
   158  			continue
   159  		}
   160  	}
   161  }