-
Notifications
You must be signed in to change notification settings - Fork 37
/
ginrpc_test.go
120 lines (99 loc) · 2.99 KB
/
ginrpc_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
package ginrpc
import (
"fmt"
"net/http"
"testing"
"github.com/gin-gonic/gin"
"github.com/xxjwxc/ginrpc/api"
)
func TestModelObj(t *testing.T) {
base := New(WithCtx(func(c *gin.Context) interface{} {
return api.NewCtx(c)
}))
router := gin.Default()
base.Register(router, new(Hello))
}
func TestModelFunc(t *testing.T) {
// base := Default()
// base.Model(func(c *gin.Context) interface{} {
// return api.NewCtx(c)
// })
base := New(WithCtx(func(c *gin.Context) interface{} {
return api.NewCtx(c)
}))
router := gin.Default()
base.RegisterHandlerFunc(router, []string{"post", "get"}, "/test", testFun1)
router.POST("/test1", base.HandlerFunc(testFun1))
router.POST("/test2", base.HandlerFunc(testFun2))
router.POST("/test3", base.HandlerFunc(testFun3))
router.POST("/test4", base.HandlerFunc(testFun4))
// router.Run(":8080")
}
// ReqTest test req
type ReqTest1 struct {
AccessToken string `json:"access_token"` // access_token
UserName string `json:"user_name" binding:"required"` // user name
Password string `json:"password"` // password
}
// testFun1 Default function callback address on gin
func testFun1(c *gin.Context) { // gin 默认的函数回调地址
fmt.Println(c.Params)
c.String(200, "ok")
}
// testFun2 Customize the function callback address of context
func testFun2(c *api.Context) { // 自定义context的函数回调地址
fmt.Println(c.Params)
c.JSON(http.StatusOK, "ok")
}
// testFun3 Callback with custom context and parsed req parameters
func testFun3(c *api.Context, req *ReqTest1) { // 带自定义context跟已解析的req参数回调方式
fmt.Println(c.Params)
fmt.Println(req)
c.JSON(http.StatusOK, "ok")
}
// testFun4 Callback with go-gin context and parsed req parameters
func testFun4(c *gin.Context, req ReqTest1) { // 带默认context跟已解析的req参数回调方式
fmt.Println(c.Params)
fmt.Println(req)
c.JSON(http.StatusOK, req)
}
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
// Hello ...
type Hello struct {
Index int
}
// @Router /api/appuser/userlist [POST][router aaa,bbb,ccc]
func (s *Hello) AppUserList(c *gin.Context) {
}
// HelloS ...
// @Router /block [post,get]
func (s *Hello) HelloS(c *api.Context, req *ReqTest1) {
fmt.Println(c.Params)
fmt.Println(req)
c.JSON(http.StatusOK, "ok")
}
// HelloS2 ...
func (s *Hello) HelloS2(c *api.Context, req *ReqTest1) {
fmt.Println(c.Params)
fmt.Println(req)
fmt.Println(s.Index)
c.JSON(http.StatusOK, "ok")
}
// HelloS3 ...
func (s *Hello) HelloS3(c *api.Context, req *ReqTest1) (*ReqTest1, error) {
fmt.Println(c.Params)
fmt.Println(req)
fmt.Println(s.Index)
return req, nil
}
func TestObj(t *testing.T) {
base := New(WithCtx(func(c *gin.Context) interface{} {
return api.NewCtx(c)
}), WithDebug(true), WithBigCamel(true))
group := gin.Default().Group("/xxjwxc")
h := new(Hello)
h.Index = 123
base.Register(group, h) //, new(api.Hello))
// router.Run(":8080")
}