-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreghan_p_test.go
326 lines (299 loc) · 9.84 KB
/
reghan_p_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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package warpginhandle_test
import (
"net/http"
"net/http/httptest"
"sort"
"strconv"
"testing"
"github.com/gin-gonic/gin"
restyv2 "github.com/go-resty/resty/v2"
"github.com/stretchr/testify/require"
"github.com/yyle88/erero"
"github.com/yyle88/reggin/warpginhandle"
)
var testServerURL string
func TestMain(m *testing.M) {
engine := gin.New()
engine.Use(gin.Recovery())
engine.POST("/aaa", func(c *gin.Context) {
res := map[string]string{
"case": "aaa",
}
c.JSON(http.StatusOK, res)
})
//没有参数
engine.POST("/bbb", warpginhandle.P0(bbbHandle, NewResp[map[string]string]))
//推荐使用这种写法这样在路由表里就能一眼看出调用的函数和返回的结果
engine.POST("/ccc", warpginhandle.P1(cccHandle, parseArg[map[string]int], NewResp[map[string]string]))
//推荐使用
engine.POST("/ddd", warpginhandle.P1(dddHandle, warpginhandle.BIND[map[string]int], NewResp[map[string]string]))
//使用普通JSON传递参数
engine.POST("/eee", warpginhandle.PX(eeeHandle, NewResp[map[string]string]))
//返回基本类型而非指针,测试返回基本类型,逻辑和前面的基本相同
engine.POST("/fff", warpginhandle.P0(fffHandle, NewResponse[string]))
//返回基本类型而非指针
engine.POST("/ggg", warpginhandle.PX(gggHandle, NewResponse[int]))
//返回基本类型而非指针
engine.POST("/hhh", warpginhandle.P1(hhhHandle, parseArg[map[string]int], NewResponse[bool]))
//哦对返回数组也非指针
engine.POST("/iii", warpginhandle.P1(iiiHandle, warpginhandle.BIND[map[string]int], NewResponse[[]string]))
//前面返回*map是不科学的,这里返回map相对好些,也是非指针的返回类型
engine.POST("/jjj", warpginhandle.P1(jjjHandle, parseArg[map[string]int], NewResponse[map[string]string]))
//这里带 gin.Context 做参数的那种处理函数的逻辑
engine.POST("/kkk", warpginhandle.C1(kkkHandle, warpginhandle.BIND[map[string]int], NewResponse[map[string]string]))
//这里带 gin.Context 做参数的,但这里处理函数的返回的指针类型
engine.POST("/lll", warpginhandle.C1(lllHandle, parseArg[map[string]int], NewResp[map[string]string]))
//这里替换成使用 form 取参数的逻辑,这是gin自带的
engine.GET("/mmm", warpginhandle.C1(mmmHandle, warpginhandle.Q[mmmParam], NewResp[map[string]string]))
//这里替换成使用 json 取参数的逻辑,假如想用 json 接收 QueryParams 就可以这样
engine.GET("/nnn", warpginhandle.C1(nnnHandle, warpginhandle.QueryJson[nnnParam], NewResp[map[string]string]))
//这里替换成使用 form 取参数的逻辑,假如想用 form 接收 QueryParams 就可以这样,但推荐使用上面的 gin 自带的通过 form 获取参数
engine.GET("/ooo", warpginhandle.C1(oooHandle, warpginhandle.QueryForm[oooParam], NewResp[map[string]string]))
serverUt := httptest.NewServer(engine)
defer serverUt.Close()
testServerURL = serverUt.URL
m.Run()
}
func bbbHandle() (*map[string]string, error) {
res := map[string]string{
"case": "bbb",
}
return &res, nil
}
func cccHandle(arg *map[string]int) (*map[string]string, error) {
res := map[string]string{}
for k, v := range *arg {
res[k] = strconv.Itoa(v)
}
res["case"] = "ccc"
return &res, nil
}
func dddHandle(arg *map[string]int) (*map[string]string, error) {
if len(*arg) == 0 {
return nil, erero.New("wrong")
}
res := make(map[string]string, len(*arg))
for k, v := range *arg {
res[k] = strconv.Itoa(v)
}
return &res, nil
}
func eeeHandle(arg *map[string]int) (*map[string]string, error) {
res := map[string]string{}
for k, v := range *arg {
res[k] = strconv.Itoa(v)
}
res["case"] = "eee"
return &res, nil
}
func fffHandle() (string, error) {
return "abc", nil
}
func gggHandle(arg *map[string]int) (int, error) {
res := 0
for _, v := range *arg {
res += v
}
return res, nil
}
func hhhHandle(arg *map[string]int) (bool, error) {
cnt := len(*arg)
res := cnt > 3
return res, nil
}
func iiiHandle(arg *map[string]int) ([]string, error) {
if len(*arg) == 0 {
return nil, erero.New("wrong")
}
res := make([]string, 0, len(*arg))
for k := range *arg {
res = append(res, k)
}
sort.Strings(res)
return res, nil
}
func jjjHandle(arg *map[string]int) (map[string]string, error) {
if len(*arg) == 0 {
return nil, erero.New("wrong")
}
res := make(map[string]string, len(*arg))
for k, v := range *arg {
res[k] = strconv.Itoa(v)
}
return res, nil
}
func parseArg[argType any](c *gin.Context) (arg *argType, err error) {
var req argType
if err = c.ShouldBindJSON(&req); err != nil {
return nil, erero.WithMessage(err, "CAN NOT BIND REQ")
}
return &req, nil
}
func TestAaa(t *testing.T) {
var res map[string]string
response, err := restyv2.New().R().SetResult(&res).Post(testServerURL + "/aaa")
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
t.Log(string(response.Body()))
require.Equal(t, "aaa", res["case"])
}
func TestBbb(t *testing.T) {
var data map[string]string
var res = ExampleResponse{Data: &data}
response, err := restyv2.New().R().SetResult(&res).Post(testServerURL + "/bbb")
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
t.Log(string(response.Body()))
require.Equal(t, 0, res.Code)
require.Equal(t, "bbb", data["case"])
}
func TestCcc(t *testing.T) {
var data map[string]string
var res = ExampleResponse{Data: &data}
response, err := restyv2.New().R().SetBody(map[string]int{
"a": 100,
}).SetResult(&res).Post(testServerURL + "/ccc")
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
t.Log(string(response.Body()))
require.Equal(t, 0, res.Code)
require.Equal(t, "ccc", data["case"])
require.Equal(t, "100", data["a"])
}
func TestDdd(t *testing.T) {
{
var data map[string]string
var res = ExampleResponse{Data: &data}
response, err := restyv2.New().R().SetBody(map[string]int{
"a": 100,
}).SetResult(&res).Post(testServerURL + "/ddd")
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
t.Log(string(response.Body()))
require.Equal(t, 0, res.Code)
require.Equal(t, map[string]string{"a": "100"}, data)
}
{
var data map[string]string
var res = ExampleResponse{Data: &data}
response, err := restyv2.New().R().SetBody(map[string]int{}).SetResult(&res).Post(testServerURL + "/ddd")
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
t.Log(string(response.Body()))
require.Equal(t, -1, res.Code)
}
}
func TestEee(t *testing.T) {
var data map[string]string
var res = ExampleResponse{Data: &data}
response, err := restyv2.New().R().SetBody(map[string]int{
"a": 100,
}).SetResult(&res).Post(testServerURL + "/eee")
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
t.Log(string(response.Body()))
require.Equal(t, 0, res.Code)
require.Equal(t, "eee", data["case"])
require.Equal(t, "100", data["a"])
}
func TestFff(t *testing.T) {
var data string
var res = ExampleResponse{Data: &data}
response, err := restyv2.New().R().SetResult(&res).Post(testServerURL + "/fff")
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
t.Log(string(response.Body()))
require.Equal(t, 0, res.Code)
require.Equal(t, "abc", data)
}
func TestGgg(t *testing.T) {
var data int
var res = ExampleResponse{Data: &data}
response, err := restyv2.New().R().SetBody(map[string]int{
"a": 100,
"b": 200,
}).SetResult(&res).Post(testServerURL + "/ggg")
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
t.Log(string(response.Body()))
require.Equal(t, 0, res.Code)
require.Equal(t, 300, data)
}
func TestHhh(t *testing.T) {
{
var data bool
var res = ExampleResponse{Data: &data}
response, err := restyv2.New().R().SetBody(map[string]int{
"a": 100,
"b": 200,
}).SetResult(&res).Post(testServerURL + "/hhh")
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
t.Log(string(response.Body()))
require.Equal(t, 0, res.Code)
require.Equal(t, false, data)
}
{
var data bool
var res = ExampleResponse{Data: &data}
response, err := restyv2.New().R().SetBody(map[string]int{
"a": 100,
"b": 200,
"c": 300,
"d": 400,
}).SetResult(&res).Post(testServerURL + "/hhh")
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
t.Log(string(response.Body()))
require.Equal(t, 0, res.Code)
require.Equal(t, true, data)
}
}
func TestIii(t *testing.T) {
{
var data []string
var res = ExampleResponse{Data: &data}
response, err := restyv2.New().R().SetBody(map[string]int{
"a": 100,
"b": 200,
}).SetResult(&res).Post(testServerURL + "/iii")
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
t.Log(string(response.Body()))
require.Equal(t, 0, res.Code)
require.Equal(t, []string{"a", "b"}, data)
}
{
var data []string
var res = ExampleResponse{Data: &data}
response, err := restyv2.New().R().SetBody(map[string]int{}).SetResult(&res).Post(testServerURL + "/iii")
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
t.Log(string(response.Body()))
require.Equal(t, -1, res.Code)
}
}
func TestJjj(t *testing.T) {
{
var data map[string]string
var res = ExampleResponse{Data: &data}
response, err := restyv2.New().R().SetBody(map[string]int{
"a": 100,
"b": 200,
}).SetResult(&res).Post(testServerURL + "/jjj")
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
t.Log(string(response.Body()))
require.Equal(t, 0, res.Code)
require.Equal(t, map[string]string{"a": "100", "b": "200"}, data)
}
{
var data map[string]string
var res = ExampleResponse{Data: &data}
response, err := restyv2.New().R().SetBody(map[string]int{}).SetResult(&res).Post(testServerURL + "/jjj")
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode())
t.Log(string(response.Body()))
require.Equal(t, -1, res.Code)
}
}