-
Notifications
You must be signed in to change notification settings - Fork 19
/
jsonrpc_test.go
87 lines (74 loc) · 2.41 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
// Copyright 2016 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package factom_test
import (
"bytes"
"encoding/json"
"fmt"
. "github.com/FactomProject/factom"
"testing"
)
var _ = fmt.Sprint("testing")
func TestNewJSON2Request(t *testing.T) {
type t1 struct {
A int
B string
}
x1 := &t1{A: 1, B: "hello"}
j1 := NewJSON2Request("testing", 1111, x1)
r1 := `{"jsonrpc":"2.0","id":1111,"params":{"A":1,"B":"hello"},"method":"testing"}`
if p, err := json.Marshal(j1); err != nil {
t.Error(err)
} else if string(p) != r1 {
t.Errorf(string(p))
}
x2 := "hello"
j2 := NewJSON2Request("testing", 2222, x2)
r2 := `{"jsonrpc":"2.0","id":2222,"params":"hello","method":"testing"}`
if p, err := json.Marshal(j2); err != nil {
t.Error(err)
} else if string(p) != r2 {
t.Errorf(string(p))
}
x3 := new(Entry)
x3.ChainID = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
x3.ExtIDs = append(x3.ExtIDs, []byte("test01"))
x3.Content = []byte("hello factom")
j3 := NewJSON2Request("testing", 3333, x3)
r3 := `{"jsonrpc":"2.0","id":3333,"params":{"chainid":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","extids":["746573743031"],"content":"68656c6c6f20666163746f6d"},"method":"testing"}`
if p, err := json.Marshal(j3); err != nil {
t.Error(err)
} else if string(p) != r3 {
t.Errorf(string(p))
}
}
func TestJSON2Response(t *testing.T) {
j1 := []byte(`{"jsonrpc":"2.0","id":2,"result":"hello"}`)
j2 := []byte(`{"jsonrpc":"2.0","id":3,"result":{"ChainID":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","ExtIDs":["746573743031"],"Content":"68656c6c6f20666163746f6d"},"method":"testing"}`)
r1 := `"hello"`
r2 := Entry{ChainID: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ExtIDs: [][]byte{[]byte("test01")}, Content: []byte("hello factom")}
resp := NewJSON2Response()
if err := json.Unmarshal(j1, resp); err != nil {
t.Error(err)
} else if string(resp.JSONResult()) != r1 {
t.Errorf("%s is not equal to %s", resp.JSONResult(), r1)
}
resp = NewJSON2Response()
if err := json.Unmarshal(j2, resp); err != nil {
t.Error(err)
}
e := new(Entry)
e.UnmarshalJSON(resp.JSONResult())
if e.ChainID != r2.ChainID {
t.Error(e)
}
for i, v := range e.ExtIDs {
if !bytes.Equal(v, r2.ExtIDs[i]) {
t.Error(e)
}
}
if !bytes.Equal(e.Content, r2.Content) {
t.Error(e)
}
}