-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjsonrpc2_test.go
174 lines (148 loc) · 3.72 KB
/
jsonrpc2_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// SPDX-FileCopyrightText: 2021 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package jsonrpc2_test
import (
"bytes"
"context"
"fmt"
"io"
"net"
"path"
"reflect"
"testing"
"github.com/segmentio/encoding/json"
"go.lsp.dev/jsonrpc2"
)
const (
methodNoArgs = "no_args"
methodOneString = "one_string"
methodOneNumber = "one_number"
methodJoin = "join"
)
type callTest struct {
method string
params interface{}
expect interface{}
}
var callTests = []callTest{
{
method: methodNoArgs,
params: nil,
expect: true,
},
{
method: methodOneString,
params: "fish",
expect: "got:fish",
},
{
method: methodOneNumber,
params: 10,
expect: "got:10",
},
{
method: methodJoin,
params: []string{"a", "b", "c"},
expect: "a/b/c",
},
// TODO: expand the test cases
}
func (test *callTest) newResults() interface{} {
switch e := test.expect.(type) {
case []interface{}:
var r []interface{}
for _, v := range e {
r = append(r, reflect.New(reflect.TypeOf(v)).Interface())
}
return r
case nil:
return nil
default:
return reflect.New(reflect.TypeOf(test.expect)).Interface()
}
}
func (test *callTest) verifyResults(t *testing.T, results interface{}) {
t.Helper()
if results == nil {
return
}
val := reflect.Indirect(reflect.ValueOf(results)).Interface()
if !reflect.DeepEqual(val, test.expect) {
t.Errorf("%v:Results are incorrect, got %+v expect %+v", test.method, val, test.expect)
}
}
func TestRequest(t *testing.T) {
ctx := context.Background()
a, b, done := prepare(ctx, t)
defer done()
for _, test := range callTests {
t.Run(test.method, func(t *testing.T) {
results := test.newResults()
if _, err := a.Call(ctx, test.method, test.params, results); err != nil {
t.Fatalf("%v:Call failed: %v", test.method, err)
}
test.verifyResults(t, results)
if _, err := b.Call(ctx, test.method, test.params, results); err != nil {
t.Fatalf("%v:Call failed: %v", test.method, err)
}
test.verifyResults(t, results)
})
}
}
func prepare(ctx context.Context, t *testing.T) (a, b jsonrpc2.Conn, done func()) {
t.Helper()
// make a wait group that can be used to wait for the system to shut down
aPipe, bPipe := net.Pipe()
a = run(ctx, aPipe)
b = run(ctx, bPipe)
done = func() {
a.Close()
b.Close()
<-a.Done()
<-b.Done()
}
return a, b, done
}
func run(ctx context.Context, nc io.ReadWriteCloser) jsonrpc2.Conn {
stream := jsonrpc2.NewStream(nc)
conn := jsonrpc2.NewConn(stream)
conn.Go(ctx, testHandler())
return conn
}
func testHandler() jsonrpc2.Handler {
return func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
switch req.Method() {
case methodNoArgs:
if len(req.Params()) > 0 {
return reply(ctx, nil, fmt.Errorf("expected no params: %w", jsonrpc2.ErrInvalidParams))
}
return reply(ctx, true, nil)
case methodOneString:
var v string
dec := json.NewDecoder(bytes.NewReader(req.Params()))
dec.ZeroCopy()
if err := dec.Decode(&v); err != nil {
return reply(ctx, nil, fmt.Errorf("%s: %w", jsonrpc2.ErrParse, err))
}
return reply(ctx, "got:"+v, nil)
case methodOneNumber:
var v int
dec := json.NewDecoder(bytes.NewReader(req.Params()))
dec.ZeroCopy()
if err := dec.Decode(&v); err != nil {
return reply(ctx, nil, fmt.Errorf("%s: %w", jsonrpc2.ErrParse, err))
}
return reply(ctx, fmt.Sprintf("got:%d", v), nil)
case methodJoin:
var v []string
dec := json.NewDecoder(bytes.NewReader(req.Params()))
dec.ZeroCopy()
if err := dec.Decode(&v); err != nil {
return reply(ctx, nil, fmt.Errorf("%s: %w", jsonrpc2.ErrParse, err))
}
return reply(ctx, path.Join(v...), nil)
default:
return jsonrpc2.MethodNotFoundHandler(ctx, reply, req)
}
}
}