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.
12 "github.com/btcsuite/btcd/btcjson"
15 // TestIsValidIDType ensures the IsValidIDType function behaves as expected.
16 func TestIsValidIDType(t *testing.T) {
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},
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},
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,
56 // TestMarshalResponse ensures the MarshalResponse function works as expected.
57 func TestMarshalResponse(t *testing.T) {
64 jsonErr *btcjson.RPCError
68 name: "ordinary bool result with no error",
71 expected: []byte(`{"result":true,"error":null,"id":1}`),
74 name: "result with error",
76 jsonErr: func() *btcjson.RPCError {
77 return btcjson.NewRPCError(btcjson.ErrRPCBlockNotFound, "123 not found")
79 expected: []byte(`{"result":null,"error":{"code":-5,"message":"123 not found"},"id":1}`),
83 t.Logf("Running %d tests", len(tests))
84 for i, test := range tests {
86 marshalled, err := btcjson.MarshalResponse(testID, test.result, test.jsonErr)
88 t.Errorf("Test #%d (%s) unexpected error: %v", i,
93 if !reflect.DeepEqual(marshalled, test.expected) {
94 t.Errorf("Test #%d (%s) mismatched result - got %s, "+
95 "want %s", i, test.name, marshalled,
101 // TestMiscErrors tests a few error conditions not covered elsewhere.
102 func TestMiscErrors(t *testing.T) {
105 // Force an error in NewRequest by giving it a parameter type that is
107 _, err := btcjson.NewRequest(nil, "test", []interface{}{make(chan int)})
109 t.Error("NewRequest: did not receive error")
113 // Force an error in MarshalResponse by giving it an id type that is not
115 wantErr := btcjson.Error{ErrorCode: btcjson.ErrInvalidType}
116 _, err = btcjson.MarshalResponse(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)
123 // Force an error in MarshalResponse by giving it a result type that
124 // can't be marshalled.
125 _, err = btcjson.MarshalResponse(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)
134 // TestRPCError tests the error output for the RPCError type.
135 func TestRPCError(t *testing.T) {
143 btcjson.ErrRPCInvalidRequest,
144 "-32600: Invalid request",
147 btcjson.ErrRPCMethodNotFound,
148 "-32601: Method not found",
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,