-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
method_test.go
74 lines (54 loc) · 1.64 KB
/
method_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
package jsonrpc_test
import (
"context"
"testing"
"github.com/goccy/go-json"
"github.com/osamingo/jsonrpc/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTakeMethod(t *testing.T) {
t.Parallel()
mr := jsonrpc.NewMethodRepository()
r := &jsonrpc.Request{}
_, err := mr.TakeMethod(r)
require.IsType(t, &jsonrpc.Error{}, err)
assert.Equal(t, jsonrpc.ErrorCodeInvalidParams, err.Code)
r.Method = "test"
_, err = mr.TakeMethod(r)
require.IsType(t, &jsonrpc.Error{}, err)
assert.Equal(t, jsonrpc.ErrorCodeInvalidParams, err.Code)
r.Version = "2.0"
_, err = mr.TakeMethod(r)
require.IsType(t, &jsonrpc.Error{}, err)
assert.Equal(t, jsonrpc.ErrorCodeMethodNotFound, err.Code)
require.NoError(t, mr.RegisterMethod("test", SampleHandler(), nil, nil))
f, err := mr.TakeMethod(r)
require.Nil(t, err)
assert.NotEmpty(t, f)
}
func TestRegisterMethod(t *testing.T) {
t.Parallel()
mr := jsonrpc.NewMethodRepository()
err := mr.RegisterMethod("", nil, nil, nil)
require.Error(t, err)
err = mr.RegisterMethod("test", nil, nil, nil)
require.Error(t, err)
err = mr.RegisterMethod("test", SampleHandler(), nil, nil)
require.NoError(t, err)
}
func TestMethods(t *testing.T) {
t.Parallel()
mr := jsonrpc.NewMethodRepository()
err := mr.RegisterMethod("JsonRpc.Sample", SampleHandler(), nil, nil)
require.NoError(t, err)
ml := mr.Methods()
require.NotEmpty(t, ml)
assert.NotEmpty(t, ml["JsonRpc.Sample"].Handler)
}
func SampleHandler() *jsonrpc.HandlerFunc {
h := jsonrpc.HandlerFunc(func(c context.Context, params *json.RawMessage) (any, *jsonrpc.Error) {
return (any)(nil), nil
})
return &h
}