github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/btcjson/jsonrpc_test.go (about)

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