forked from sourcegraph/jsonrpc2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_test.go
132 lines (128 loc) · 3.39 KB
/
object_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
package jsonrpc2
import (
"bytes"
"encoding/json"
"reflect"
"testing"
)
func TestAnyMessage(t *testing.T) {
tests := map[string]struct {
request, response, invalid bool
}{
// Single messages
`{}`: {invalid: true},
`{"foo":"bar"}`: {invalid: true},
`{"method":"m"}`: {request: true},
`{"result":123}`: {response: true},
`{"result":null}`: {response: true},
`{"error":{"code":456,"message":"m"}}`: {response: true},
}
for s, want := range tests {
var m anyMessage
if err := json.Unmarshal([]byte(s), &m); err != nil {
if !want.invalid {
t.Errorf("%s: error: %v", s, err)
}
continue
}
if (m.request != nil) != want.request {
t.Errorf("%s: got request %v, want %v", s, m.request != nil, want.request)
}
if (m.response != nil) != want.response {
t.Errorf("%s: got response %v, want %v", s, m.response != nil, want.response)
}
}
}
func TestRequest_MarshalUnmarshalJSON(t *testing.T) {
null := json.RawMessage("null")
obj := json.RawMessage(`{"foo":"bar"}`)
tests := []struct {
data []byte
want Request
}{
{
data: []byte(`{"id":123,"jsonrpc":"2.0","method":"m","params":{"foo":"bar"}}`),
want: Request{ID: ID{Num: 123}, Method: "m", Params: &obj},
},
{
data: []byte(`{"id":123,"jsonrpc":"2.0","method":"m","params":null}`),
want: Request{ID: ID{Num: 123}, Method: "m", Params: &null},
},
{
data: []byte(`{"id":123,"jsonrpc":"2.0","method":"m"}`),
want: Request{ID: ID{Num: 123}, Method: "m", Params: nil},
},
{
data: []byte(`{"id":123,"jsonrpc":"2.0","method":"m","sessionId":"session"}`),
want: Request{ID: ID{Num: 123}, Method: "m", Params: nil, ExtraFields: []RequestField{{Name: "sessionId", Value: "session"}}},
},
}
for _, test := range tests {
var got Request
if err := json.Unmarshal(test.data, &got); err != nil {
t.Error(err)
continue
}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("%q: got %+v, want %+v", test.data, got, test.want)
continue
}
data, err := json.Marshal(got)
if err != nil {
t.Error(err)
continue
}
if !bytes.Equal(data, test.data) {
t.Errorf("got JSON %q, want %q", data, test.data)
}
}
}
func TestResponse_MarshalUnmarshalJSON(t *testing.T) {
null := json.RawMessage("null")
obj := json.RawMessage(`{"foo":"bar"}`)
tests := []struct {
data []byte
want Response
error bool
}{
{
data: []byte(`{"id":123,"result":{"foo":"bar"},"jsonrpc":"2.0"}`),
want: Response{ID: ID{Num: 123}, Result: &obj},
},
{
data: []byte(`{"id":123,"result":null,"jsonrpc":"2.0"}`),
want: Response{ID: ID{Num: 123}, Result: &null},
},
{
data: []byte(`{"id":123,"jsonrpc":"2.0"}`),
want: Response{ID: ID{Num: 123}, Result: nil},
error: true, // either result or error field must be set
},
}
for _, test := range tests {
var got Response
if err := json.Unmarshal(test.data, &got); err != nil {
t.Error(err)
continue
}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("%q: got %+v, want %+v", test.data, got, test.want)
continue
}
data, err := json.Marshal(got)
if err != nil {
if test.error {
continue
}
t.Error(err)
continue
}
if test.error {
t.Errorf("%q: expected error", test.data)
continue
}
if !bytes.Equal(data, test.data) {
t.Errorf("got JSON %q, want %q", data, test.data)
}
}
}