-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods_test.go
More file actions
122 lines (104 loc) · 2.52 KB
/
Copy pathmethods_test.go
File metadata and controls
122 lines (104 loc) · 2.52 KB
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
package jsonrpc
import (
"context"
"encoding/json"
"fmt"
"testing"
)
func TestJSONRPC_RegisterMethod(t *testing.T) {
j := New()
m := func(ctx context.Context, data json.RawMessage) (json.RawMessage, int, error) {
return nil, 0, nil
}
mm := func(ctx context.Context, data json.RawMessage) (json.RawMessage, int, error) {
return nil, 0, nil
}
t.Run("register new method", func(t *testing.T) {
err := j.RegisterMethod("simple", m)
if err != nil {
t.Errorf("should register, but got %v", err)
}
})
t.Run("duplicate method", func(t *testing.T) {
err := j.RegisterMethod("simple", mm)
if err == nil {
t.Errorf("should not register duplicate method")
}
})
}
func TestJSONRPC_CallEmpty(t *testing.T) {
j := New()
m := func(ctx context.Context, data json.RawMessage) (json.RawMessage, int, error) {
return nil, 0, nil
}
methodName := "simple"
callID := 1
err := j.RegisterMethod(methodName, m)
if err != nil {
t.Fatalf("should register, but got %v", err)
}
ctx := context.Background()
resp := j.call(ctx, methodName, nil, callID)
if resp.ID != callID {
t.Errorf("should get ID=%v, but got %v", callID, resp.ID)
}
}
func TestJSONRPC_CallSum(t *testing.T) {
j := New()
type income struct {
A int `json:"a"`
B int `json:"bb"`
}
type outcome struct {
C int `json:"c"`
}
m := func(ctx context.Context, data json.RawMessage) (json.RawMessage, int, error) {
if data == nil {
return nil, InvalidRequestErrorCode, fmt.Errorf("empty request")
}
inc := &income{}
err := json.Unmarshal(data, inc)
if err != nil {
return nil, InvalidRequestErrorCode, err
}
C := outcome{
C: inc.A + inc.B,
}
mdata, err := json.Marshal(C)
if err != nil {
return nil, InternalErrorCode, err
}
return mdata, 0, nil
}
methodName := "sum"
callID := 1
err := j.RegisterMethod(methodName, m)
if err != nil {
t.Fatalf("should register, but got %v", err)
}
ctx := context.Background()
sendData := `{"a":3, "bb":5}`
resp := j.call(ctx, methodName, []byte(sendData), callID)
if resp.ID != callID {
t.Errorf("should get ID=%v, but got %v", callID, resp.ID)
return
}
if resp.Error != nil {
t.Errorf("should get no error, but got %v", resp.Error)
return
}
if resp.Result == nil {
t.Errorf("should get result, but got nil")
return
}
recData := resp.Result
checkdata := outcome{}
err = json.Unmarshal(*recData, &checkdata)
if err != nil {
t.Errorf("error on unmarshal response: %v", err)
return
}
if checkdata.C != 8 {
t.Errorf("should be %v, but got %v", 8, checkdata.C)
}
}