提交 4d3ef462 authored 作者: mooncake9527's avatar mooncake9527

common controller tester

上级 a59a88b7
package xcommon package xcommon
import ( import (
"context"
"net/http" "net/http"
"testing"
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/response"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/mapUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/xcommon/validator" "gitlab.wanzhuangkj.com/tush/xpkg/xcommon/validator"
) )
...@@ -47,13 +53,56 @@ func (e *Controller) Bind(c *gin.Context, req interface{}) error { ...@@ -47,13 +53,56 @@ func (e *Controller) Bind(c *gin.Context, req interface{}) error {
return nil return nil
} }
// type ControllerTester struct {
//func (x *Controller) GetUser(ctx context.Context) *biz.User { Headers map[string]string
// val := ctx.Value(ctxUtils.KeyUser) Addr string
// if val != nil { client *httpcli.Client
// if u, ok := val.(*biz.User); ok { }
// return u
// } func NewControllerTester(host string, header map[string]string) *ControllerTester {
// } c := ControllerTester{
// return nil Headers: header,
//} Addr: host,
client: httpcli.NewClient(&http.Client{
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
},
Timeout: 30 * time.Second,
}),
}
return &c
}
func (x *ControllerTester) Before(t *testing.T) {
// if Headers["Authorization"] != "" {
// return
// }
// loginReply, err := Login()
// if err != nil {
// t.Error(err)
// }
// UserID = loginReply.UserID
// Authorization = "Bearer " + loginReply.Token
// Headers["Authorization"] = Authorization
}
func (x *ControllerTester) After(t *testing.T) {
}
func (x *ControllerTester) CommonHttp(method, URL string, in any, reply *response.Result) error {
if method == http.MethodDelete || method == http.MethodGet {
in = mapUtils.StructToMapWithJsonTag(in)
}
if err := x.client.NewRequest().
SetHeaders(x.Headers).
SetURL(URL).SetContentType("application/json").
Do(context.Background(), method, in).
BindJSON(&reply).
Err(); err != nil {
return err
}
return nil
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论