-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathmethod_formatter_test.go
125 lines (111 loc) · 3.22 KB
/
method_formatter_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
package jsonrpc
import (
"context"
"fmt"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestDifferentMethodNamers(t *testing.T) {
tests := map[string]struct {
namer MethodNameFormatter
requestedMethod string
}{
"default namer": {
namer: DefaultMethodNameFormatter,
requestedMethod: "SimpleServerHandler.Inc",
},
"lower fist char": {
namer: NewMethodNameFormatter(true, LowerFirstCharCase),
requestedMethod: "SimpleServerHandler.inc",
},
"no namespace namer": {
namer: NewMethodNameFormatter(false, OriginalCase),
requestedMethod: "Inc",
},
"no namespace & lower fist char": {
namer: NewMethodNameFormatter(false, LowerFirstCharCase),
requestedMethod: "inc",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
rpcServer := NewServer(WithServerMethodNameFormatter(test.namer))
serverHandler := &SimpleServerHandler{}
rpcServer.Register("SimpleServerHandler", serverHandler)
testServ := httptest.NewServer(rpcServer)
defer testServ.Close()
req := fmt.Sprintf(`{"jsonrpc": "2.0", "method": "%s", "params": [], "id": 1}`, test.requestedMethod)
res, err := http.Post(testServ.URL, "application/json", strings.NewReader(req))
require.NoError(t, err)
require.Equal(t, http.StatusOK, res.StatusCode)
require.Equal(t, int32(1), serverHandler.n)
})
}
}
func TestDifferentMethodNamersWithClient(t *testing.T) {
tests := map[string]struct {
namer MethodNameFormatter
urlPrefix string
}{
"default namer & http": {
namer: DefaultMethodNameFormatter,
urlPrefix: "http://",
},
"default namer & ws": {
namer: DefaultMethodNameFormatter,
urlPrefix: "ws://",
},
"lower first char namer & http": {
namer: NewMethodNameFormatter(true, LowerFirstCharCase),
urlPrefix: "http://",
},
"lower first char namer & ws": {
namer: NewMethodNameFormatter(true, LowerFirstCharCase),
urlPrefix: "ws://",
},
"no namespace namer & http": {
namer: NewMethodNameFormatter(false, OriginalCase),
urlPrefix: "http://",
},
"no namespace namer & ws": {
namer: NewMethodNameFormatter(false, OriginalCase),
urlPrefix: "ws://",
},
"no namespace & lower first char & http": {
namer: NewMethodNameFormatter(false, LowerFirstCharCase),
urlPrefix: "http://",
},
"no namespace & lower first char & ws": {
namer: NewMethodNameFormatter(false, LowerFirstCharCase),
urlPrefix: "ws://",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
rpcServer := NewServer(WithServerMethodNameFormatter(test.namer))
serverHandler := &SimpleServerHandler{}
rpcServer.Register("SimpleServerHandler", serverHandler)
testServ := httptest.NewServer(rpcServer)
defer testServ.Close()
var client struct {
AddGet func(int) int
}
closer, err := NewMergeClient(
context.Background(),
test.urlPrefix+testServ.Listener.Addr().String(),
"SimpleServerHandler",
[]any{&client},
nil,
WithHTTPClient(testServ.Client()),
WithMethodNameFormatter(test.namer),
)
require.NoError(t, err)
defer closer()
n := client.AddGet(123)
require.Equal(t, 123, n)
})
}
}