-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_test.go
58 lines (51 loc) · 1.48 KB
/
request_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
package jsonrpc_test
import (
"encoding/json"
"testing"
"github.com/41north/jsonrpc.go"
"github.com/stretchr/testify/assert"
)
var requestTestCases = []struct {
value *jsonrpc.Request
json string
}{
{
newRequest("ping", "foo"),
"{\"method\":\"ping\",\"params\":\"foo\",\"jsonrpc\":\"2.0\"}",
},
{
newRequest("pong", "bar", jsonrpc.RequestNumericId(1456)),
"{\"id\":1456,\"method\":\"pong\",\"params\":\"bar\",\"jsonrpc\":\"2.0\"}",
},
{
newRequest("ping", nil, jsonrpc.RequestStringId("req-1")),
"{\"id\":\"req-1\",\"method\":\"ping\",\"jsonrpc\":\"2.0\"}",
},
{
newRequest("pong", []string{"hello", "world"}, jsonrpc.RequestNumericId(554), jsonrpc.RequestVersion("1.0")),
"{\"id\":554,\"method\":\"pong\",\"params\":[\"hello\",\"world\"],\"jsonrpc\":\"1.0\"}",
},
}
func TestRequest_MarshalJSON(t *testing.T) {
for _, tc := range requestTestCases {
bytes, err := json.Marshal(tc.value)
assert.Nil(t, err, "failed to marshal to json")
assert.Equal(t, tc.json, string(bytes))
}
}
func TestRequest_UnmarshalJSON(t *testing.T) {
for _, tc := range requestTestCases {
var req jsonrpc.Request
err := json.Unmarshal([]byte(tc.json), &req)
assert.Nil(t, err, "failed to unmarshal from json")
assert.Equal(t, *tc.value, req)
}
}
func TestRequest_UnmarshalParams(t *testing.T) {
expected := []string{"hello", "world"}
req := newRequest("ping", expected)
var actual []string
err := req.UnmarshalParams(&actual)
assert.Nil(t, err)
assert.Equal(t, expected, actual)
}