-
Notifications
You must be signed in to change notification settings - Fork 7
/
handlers_manager_test.go
256 lines (224 loc) · 8.24 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
255
256
package gorpc
import (
"context"
"encoding/json"
"fmt"
"reflect"
"sort"
"testing"
test_handler1 "github.com/sergei-svistunov/gorpc/test/handler1"
test_handler_common_type_in_different_versions_args "github.com/sergei-svistunov/gorpc/test/handler_common_type_in_different_versions_arguments"
test_handler_common_type_in_different_versions "github.com/sergei-svistunov/gorpc/test/handler_common_type_in_different_versions_return_values"
test_handler_common_type_in_return_and_arguments "github.com/sergei-svistunov/gorpc/test/handler_common_type_in_return_and_arguments"
test_handler_foreign_arguments "github.com/sergei-svistunov/gorpc/test/handler_foreign_arguments"
test_handler_foreign_return_values "github.com/sergei-svistunov/gorpc/test/handler_foreign_return_values"
"github.com/stretchr/testify/suite"
)
// Suite definition
type HandlersManagerSuite struct {
suite.Suite
hm *HandlersManager
}
func (s *HandlersManagerSuite) SetupTest() {
s.hm = NewHandlersManager("github.com/sergei-svistunov/gorpc", HandlersManagerCallbacks{})
if !s.NoError(s.hm.RegisterHandler(test_handler1.NewHandler())) {
handler := &test_handler1.Handler{}
s.T().Fatalf("Can't register '%s'", handler.Caption())
}
err := s.hm.RegisterHandler(test_handler_foreign_arguments.NewHandler())
s.Error(err)
s.Equal(err.Error(), fmt.Sprintf(`Handler '%s' version '%s' parameter: Structure must be fully defined in the same package. Type 'args.V1Args' is not.`, `/test/handler_foreign_arguments`, `V1`))
err = s.hm.RegisterHandler(test_handler_foreign_return_values.NewHandler())
s.Error(err)
s.Equal(err.Error(), fmt.Sprintf(`Handler '%s' version '%s' return value: Structure must be fully defined in the same package. Type 'return_values.V1Res' is not.`, `/test/handler_foreign_return_values`, `V1`))
err = s.hm.RegisterHandler(test_handler_common_type_in_different_versions.NewHandler())
s.Error(err)
err = s.hm.RegisterHandler(test_handler_common_type_in_different_versions_args.NewHandler())
s.Error(err)
err = s.hm.RegisterHandler(test_handler_common_type_in_return_and_arguments.NewHandler())
s.Nil(err)
}
func TestRunSuite(t *testing.T) {
suite.Run(t, new(HandlersManagerSuite))
}
// Tests
func (s *HandlersManagerSuite) TestHandlerManager_CheckHandlersPaths() {
var expectedHandlersList = []string{"/test/handler1", "/test/handler_common_type_in_return_and_arguments"}
var existHandlersList = s.hm.GetHandlersPaths()
sort.Strings(expectedHandlersList)
sort.Strings(existHandlersList)
s.Equal(expectedHandlersList, existHandlersList)
}
func (s *HandlersManagerSuite) TestHandlerManager_FindExistsHandler() {
s.NotNil(s.hm.FindHandler("/test/handler1", 1))
}
func (s *HandlersManagerSuite) TestHandlerManager_CheckHandler1Struct() {
hv1 := s.hm.FindHandler("/test/handler1", 1)
hv2 := s.hm.FindHandler("/test/handler1", 2)
s.Equal("v1", hv1.Version)
s.Equal([]HandlerParameter{
HandlerParameter{
Name: "ReqInt",
Description: "Required integer argument",
Key: "req_int",
IsRequired: true,
RawType: hv1.Request.Fields[0].RawType,
getMethod: hv1.Request.Fields[0].getMethod,
structField: hv1.Request.Fields[0].structField,
},
HandlerParameter{
Name: "Int",
Description: "Unrequired integer argument",
Key: "int",
IsRequired: false,
RawType: hv1.Request.Fields[1].RawType,
getMethod: hv1.Request.Fields[1].getMethod,
structField: hv1.Request.Fields[1].structField,
},
}, hv1.Request.Fields)
s.Equal("v2", hv2.Version)
s.Equal([]HandlerError{
HandlerError{
UserMessage: "Error 1 description",
Err: hv2.Errors[0].Err,
Code: "ERROR_TYPE1",
},
HandlerError{
UserMessage: "Error 2 description",
Err: hv2.Errors[1].Err,
Code: "ERROR_TYPE2",
},
HandlerError{
UserMessage: "Error 3 description",
Err: hv2.Errors[2].Err,
Code: "ERROR_TYPE3",
},
}, hv2.Errors)
}
func (s *HandlersManagerSuite) TestHandlerManager_CallHandler1V1_ReturnResult() {
pg := &ParametersGetter{
map[string][]string{
"req_int": []string{"123"},
},
}
hanlerVersion := s.hm.FindHandler("/test/handler1", 1)
if hanlerVersion == nil {
s.NotNil(hanlerVersion)
}
params, err := s.hm.UnmarshalParameters(context.TODO(), hanlerVersion, pg)
s.NoError(err)
if res, err := s.hm.CallHandler(context.TODO(), hanlerVersion, params); err != nil {
s.Fail("error: %s", err.Error())
} else {
s.Equal(&test_handler1.V1Res{"Test", 123}, res)
}
}
func (s *HandlersManagerSuite) TestHandlerManager_PrepareParametersWithError() {
pg := &ParametersGetter{
map[string][]string{},
}
hanlerVersion := s.hm.FindHandler("/test/handler1", 1)
if hanlerVersion == nil {
s.NotNil(hanlerVersion)
}
_, err := s.hm.UnmarshalParameters(context.TODO(), hanlerVersion, pg)
s.Error(err)
}
func TestUnmarshalJsonParameters(t *testing.T) {
type Request struct {
IntField int `key:"int" json:"int" description:"int field"`
OptionalIntField *int `key:"opt_int" json:"opt_int,omitempty" description:"optional int field"`
BoolField bool `key:"bool" json:"bool" description:"bool field"`
OptionalBoolField *bool `key:"opt_bool" json:"opt_bool,omitempty" description:"optional bool field"`
StrField string `key:"str" json:"str" description:"str field"`
OptionalStrField *string `key:"opt_str" json:"opt_str,omitempty" description:"optional str field"`
}
request := &Request{
IntField: 1,
BoolField: true,
StrField: "test",
}
err := unmarshalJsonParameters(t, request)
if err != nil {
t.Fatal(err)
}
}
func TestUnmarshalOptionalJsonParameters(t *testing.T) {
type Request struct {
IntField int `key:"int" json:"int" description:"int field"`
OptionalIntField *int `key:"opt_int" json:"opt_int,omitempty" description:"optional int field"`
BoolField bool `key:"bool" json:"bool" description:"bool field"`
OptionalBoolField *bool `key:"opt_bool" json:"opt_bool,omitempty" description:"optional bool field"`
StrField string `key:"str" json:"str" description:"str field"`
OptionalStrField *string `key:"opt_str" json:"opt_str,omitempty" description:"optional str field"`
}
i := 2
b := true
s := "optional"
request := &Request{
IntField: 1,
OptionalIntField: &i,
BoolField: true,
OptionalBoolField: &b,
StrField: "test",
OptionalStrField: &s,
}
err := unmarshalJsonParameters(t, request)
if err != nil {
t.Fatal(err)
}
}
func TestUnmarshalJsonParametersNested(t *testing.T) {
type Nested1 struct {
Foo int `key:"int" json:"int" description:"foo field"`
}
type Nested2 struct {
Foo *int `key:"int" json:"int" description:"optional foo field"`
}
type Request struct {
NestedField Nested1 `key:"nested" json:"nested" description:"nested field"`
OptionalNestedField *Nested2 `key:"opt_nested" json:"opt_nested,omitempty" description:"nested field"`
NestedSlice []Nested1 `key:"nested_slice" json:"nested_slice,omitempty" description:"nested slice"`
OptionalNestedSlice *[]Nested1 `key:"opt_nested_slice" json:"opt_nested_slice,omitempty" description:"nested slice"`
NestedMap map[string]Nested1 `key:"nested_map" json:"nested_map" description:"nested map"`
OptionalNestedMap *map[string]Nested1 `key:"opt_nested_map" json:"opt_nested_map,omitempty" description:"nested map"`
}
i := 1
request := &Request{
NestedField: Nested1{
Foo: 1,
},
OptionalNestedField: &Nested2{
Foo: &i,
},
NestedSlice: []Nested1{Nested1{1}, Nested1{1}},
NestedMap: map[string]Nested1{
"1": Nested1{1},
"2": Nested1{2},
},
}
err := unmarshalJsonParameters(t, request)
if err != nil {
t.Fatal(err)
}
}
func unmarshalJsonParameters(t *testing.T, request interface{}) error {
handlerRequest, err := processRequestType(reflect.TypeOf(request))
if err != nil {
return err
}
body, err := json.Marshal(request)
if err != nil {
return err
}
requestValue, err := unmarshalRequest(handlerRequest, &JsonParametersGetter{
Req: string(body),
})
if err != nil {
return err
}
if !reflect.DeepEqual(request, requestValue.Interface()) {
return fmt.Errorf("Unmarshalled request is not equal to expected.\nExpected:%+v\nActual:%+v", request, requestValue.Interface())
}
return nil
}