forked from sergei-svistunov/gorpc
/
handlers_manager_test.go
254 lines (200 loc) · 6.2 KB
/
handlers_manager_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package gorpc
import (
"errors"
"strconv"
"strings"
"testing"
"time"
"github.com/stretchr/testify/suite"
"golang.org/x/net/context"
test_handler1 "github.com/sergei-svistunov/gorpc/test/handler1"
)
// Cache implementation
type TestCache struct{}
func (c *TestCache) Get(key string) (interface{}, bool) {
return nil, false
}
func (c *TestCache) Put(key string, data interface{}, ttl time.Duration) {}
// Parameters getter
type ParametersGetter struct {
values map[string][]string
}
func (pg *ParametersGetter) IsExists(name string) bool {
_, exists := pg.values[name]
return exists
}
func (pg *ParametersGetter) GetStringSlice(name string) []string {
name = strings.ToLower(name)
if slice, ok := pg.values[name]; ok {
return slice
}
return []string{}
}
func (pg *ParametersGetter) GetString(name string) (string, error) {
return pg.get(name), nil
}
func (pg *ParametersGetter) GetBool(name string) (bool, error) {
v, err := strconv.ParseBool(pg.get(name))
if err != nil {
err = errors.New(`Wrong value of param "` + name + `". It should be Bool`)
}
return v, err
}
func (pg *ParametersGetter) GetUint(name string) (uint, error) {
v, err := strconv.ParseUint(pg.get(name), 0, 0)
if err != nil {
err = errors.New(`Wrong value of param "` + name + `". It should be Uint`)
}
return uint(v), err
}
func (pg *ParametersGetter) GetByte(name string) (uint8, error) {
v, err := strconv.ParseUint(pg.get(name), 0, 8)
if err != nil {
err = errors.New(`Wrong value of param "` + name + `". It should be Uint`)
}
return uint8(v), err
}
func (pg *ParametersGetter) GetUint8(name string) (uint8, error) {
v, err := strconv.ParseUint(pg.get(name), 0, 8)
if err != nil {
err = errors.New(`Wrong value of param "` + name + `". It should be Uint`)
}
return uint8(v), err
}
func (pg *ParametersGetter) GetUint16(name string) (uint16, error) {
v, err := strconv.ParseUint(pg.get(name), 0, 16)
if err != nil {
err = errors.New(`Wrong value of param "` + name + `". It should be Uint`)
}
return uint16(v), err
}
func (pg *ParametersGetter) GetUint32(name string) (uint32, error) {
v, err := strconv.ParseUint(pg.get(name), 0, 32)
if err != nil {
err = errors.New(`Wrong value of param "` + name + `". It should be Uint`)
}
return uint32(v), err
}
func (pg *ParametersGetter) GetUint64(name string) (uint64, error) {
v, err := strconv.ParseUint(pg.get(name), 0, 64)
if err != nil {
err = errors.New(`Wrong value of param "` + name + `". It should be Uint`)
}
return v, err
}
func (pg *ParametersGetter) GetInt(name string) (int, error) {
v, err := strconv.ParseInt(pg.get(name), 0, 0)
if err != nil {
err = errors.New(`Wrong value of param "` + name + `". It should be Int`)
}
return int(v), err
}
func (pg *ParametersGetter) GetInt8(name string) (int8, error) {
v, err := strconv.ParseInt(pg.get(name), 0, 8)
if err != nil {
err = errors.New(`Wrong value of param "` + name + `". It should be Int`)
}
return int8(v), err
}
func (pg *ParametersGetter) GetInt16(name string) (int16, error) {
v, err := strconv.ParseInt(pg.get(name), 0, 16)
if err != nil {
err = errors.New(`Wrong value of param "` + name + `". It should be Int`)
}
return int16(v), err
}
func (pg *ParametersGetter) GetInt32(name string) (int32, error) {
v, err := strconv.ParseInt(pg.get(name), 0, 32)
if err != nil {
err = errors.New(`Wrong value of param "` + name + `". It should be Int`)
}
return int32(v), err
}
func (pg *ParametersGetter) GetInt64(name string) (int64, error) {
v, err := strconv.ParseInt(pg.get(name), 0, 64)
if err != nil {
err = errors.New(`Wrong value of param "` + name + `". It should be Int`)
}
return v, err
}
func (pg *ParametersGetter) GetFloat32(name string) (float32, error) {
v, err := strconv.ParseFloat(pg.get(name), 32)
if err != nil {
err = errors.New(`Wrong value of param "` + name + `". It should be Float`)
}
return float32(v), err
}
func (pg *ParametersGetter) GetFloat64(name string) (float64, error) {
v, err := strconv.ParseFloat(pg.get(name), 64)
if err != nil {
err = errors.New(`Wrong value of param "` + name + `". It should be Float`)
}
return v, err
}
func (pg *ParametersGetter) get(name string) string {
slice := pg.values[name]
return slice[0]
}
// Suite definition
type HandlersManagerSuite struct {
suite.Suite
hm *HandlersManager
}
func (s *HandlersManagerSuite) SetupTest() {
s.hm = NewHandlersManager("github.com/sergei-svistunov/gorpc", HandlersManagerCallbacks{}, &TestCache{}, 0)
s.NoError(s.hm.RegisterHandler(test_handler1.NewHandler()))
}
func TestRunSuite(t *testing.T) {
suite.Run(t, new(HandlersManagerSuite))
}
// Tests
func (s *HandlersManagerSuite) TestHandlerManager_CheckHandlersPaths() {
s.Equal([]string{"/test/handler1"}, s.hm.GetHandlersPaths())
}
func (s *HandlersManagerSuite) TestHandlerManager_FindExistsHandler() {
s.NotNil(s.hm.FindHandler("/test/handler1", 1))
}
func (s *HandlersManagerSuite) TestHandlerManager_CheckHandler1Struct() {
hv := s.hm.FindHandler("/test/handler1", 1)
s.Equal("v1", hv.Version)
s.True(hv.UseCache)
s.Equal([]handlerParameter{
handlerParameter{
Name: "ReqInt",
Type: "int",
Description: "Required integer argument",
Key: "req_int",
IsRequired: true,
RawType: hv.Parameters[0].RawType,
getMethod: hv.Parameters[0].getMethod,
structField: hv.Parameters[0].structField,
},
handlerParameter{
Name: "Int",
Type: "int",
Description: "Unrequired integer argument",
Key: "int",
IsRequired: false,
RawType: hv.Parameters[1].RawType,
getMethod: hv.Parameters[1].getMethod,
structField: hv.Parameters[1].structField,
},
}, hv.Parameters)
}
func (s *HandlersManagerSuite) TestHandlerManager_CallHandler1V1_ReturnResult() {
pg := &ParametersGetter{
map[string][]string{
"req_int": []string{"123"},
},
}
res, err := s.hm.CallHandler(context.TODO(), s.hm.FindHandler("/test/handler1", 1), pg)
s.NoError(err)
s.Equal(&test_handler1.V1Res{"Test", 123}, res)
}
func (s *HandlersManagerSuite) TestHandlerManager_CallHandler1V1WuthoutReqArg_ReturnError() {
pg := &ParametersGetter{
map[string][]string{},
}
_, err := s.hm.CallHandler(context.TODO(), s.hm.FindHandler("/test/handler1", 1), pg)
s.Error(err)
}