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