-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
jsonrpc_test.go
130 lines (104 loc) · 3.81 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
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
package jsonrpc_test
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/goccy/go-json"
"github.com/osamingo/jsonrpc/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseRequest(t *testing.T) {
t.Parallel()
r, rerr := http.NewRequestWithContext(context.Background(), "", "", bytes.NewReader(nil))
require.NoError(t, rerr)
_, _, err := jsonrpc.ParseRequest(r)
require.IsType(t, &jsonrpc.Error{}, err)
assert.Equal(t, jsonrpc.ErrorCodeInvalidRequest, err.Code)
r.Header.Set("Content-Type", "application/json")
_, _, err = jsonrpc.ParseRequest(r)
require.IsType(t, &jsonrpc.Error{}, err)
assert.Equal(t, jsonrpc.ErrorCodeInvalidRequest, err.Code)
r, rerr = http.NewRequestWithContext(context.Background(), "", "", bytes.NewReader([]byte("")))
require.NoError(t, rerr)
r.Header.Set("Content-Type", "application/json")
_, _, err = jsonrpc.ParseRequest(r)
require.IsType(t, &jsonrpc.Error{}, err)
assert.Equal(t, jsonrpc.ErrorCodeInvalidRequest, err.Code)
r, rerr = http.NewRequestWithContext(context.Background(), "", "", bytes.NewReader([]byte("test")))
require.NoError(t, rerr)
r.Header.Set("Content-Type", "application/json")
_, _, err = jsonrpc.ParseRequest(r)
require.IsType(t, &jsonrpc.Error{}, err)
assert.Equal(t, jsonrpc.ErrorCodeParse, err.Code)
r, rerr = http.NewRequestWithContext(context.Background(), "", "", bytes.NewReader([]byte("{}")))
require.NoError(t, rerr)
r.Header.Set("Content-Type", "application/json")
rs, batch, err := jsonrpc.ParseRequest(r)
require.Nil(t, err)
require.NotEmpty(t, rs)
assert.False(t, batch)
r, rerr = http.NewRequestWithContext(context.Background(), "", "", bytes.NewReader([]byte("[")))
require.NoError(t, rerr)
r.Header.Set("Content-Type", "application/json")
_, _, err = jsonrpc.ParseRequest(r)
require.IsType(t, &jsonrpc.Error{}, err)
assert.Equal(t, jsonrpc.ErrorCodeParse, err.Code)
r, rerr = http.NewRequestWithContext(context.Background(), "", "", bytes.NewReader([]byte("[test]")))
require.NoError(t, rerr)
r.Header.Set("Content-Type", "application/json")
_, _, err = jsonrpc.ParseRequest(r)
require.IsType(t, &jsonrpc.Error{}, err)
assert.Equal(t, jsonrpc.ErrorCodeParse, err.Code)
r, rerr = http.NewRequestWithContext(context.Background(), "", "", bytes.NewReader([]byte("[{}]")))
require.NoError(t, rerr)
r.Header.Set("Content-Type", "application/json")
rs, batch, err = jsonrpc.ParseRequest(r)
require.Nil(t, err)
require.NotEmpty(t, rs)
assert.True(t, batch)
}
func TestNewResponse(t *testing.T) {
t.Parallel()
id := json.RawMessage("test")
r := jsonrpc.NewResponse(&jsonrpc.Request{
Version: "2.0",
ID: &id,
})
assert.Equal(t, "2.0", r.Version)
assert.Equal(t, "test", string(*r.ID))
}
func TestSendResponse(t *testing.T) {
t.Parallel()
rec := httptest.NewRecorder()
err := jsonrpc.SendResponse(rec, []*jsonrpc.Response{}, false)
require.NoError(t, err)
assert.Empty(t, rec.Body.String())
id := json.RawMessage([]byte(`"test"`))
r := &jsonrpc.Response{
ID: &id,
Version: "2.0",
Result: struct {
Name string `json:"name"`
}{
Name: "john",
},
}
rec = httptest.NewRecorder()
err = jsonrpc.SendResponse(rec, []*jsonrpc.Response{r}, false)
require.NoError(t, err)
assert.Equal(t, `{"jsonrpc":"2.0","result":{"name":"john"},"id":"test"}
`, rec.Body.String())
rec = httptest.NewRecorder()
err = jsonrpc.SendResponse(rec, []*jsonrpc.Response{r}, true)
require.NoError(t, err)
assert.Equal(t, `[{"jsonrpc":"2.0","result":{"name":"john"},"id":"test"}]
`, rec.Body.String())
rec = httptest.NewRecorder()
err = jsonrpc.SendResponse(rec, []*jsonrpc.Response{r, r}, false)
require.NoError(t, err)
assert.Equal(t, `[{"jsonrpc":"2.0","result":{"name":"john"},"id":"test"},{"jsonrpc":"2.0","result":{"name":"john"},"id":"test"}]
`, rec.Body.String())
}