修复handler.tpl没有return, 添加单元测试例子

This commit is contained in:
eson 2023-06-12 17:10:27 +08:00
parent ff05e127bd
commit f4873d1720
10 changed files with 66 additions and 212 deletions

2
go.mod
View File

@ -13,7 +13,7 @@ require (
)
require (
github.com/474420502/requests v1.30.0
github.com/474420502/requests v1.32.1
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/tidwall/gjson v1.12.0 // indirect

4
go.sum
View File

@ -32,8 +32,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/474420502/random v0.4.1 h1:HUUyLXRWMijVb7CJoEC16f0aFQOW25Lkr80Mut6PoKU=
github.com/474420502/requests v1.30.0 h1:IVEee0wALTweoXrFsEipQEMakcSQpE5NahyMVUliMNc=
github.com/474420502/requests v1.30.0/go.mod h1:043PKfW//QR069XTYG5WT7t+z+d+8/C5PJtfWzpsf+o=
github.com/474420502/requests v1.32.1 h1:ivr9iMXrYqfdKWS0q15HEFlt1Cjeeu75Hjhbmikg9us=
github.com/474420502/requests v1.32.1/go.mod h1:rrDBE6AfPr0Cclo1lL4Jgw1mIW6TKSCvFgXDuEqlw70=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=

View File

@ -23,6 +23,7 @@ func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
return
}
// 从Token里获取对应的信息
@ -34,6 +35,7 @@ func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
return
}
{{if .HasRequest}}var req types.{{.RequestType}}
@ -58,5 +60,6 @@ func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
return
}
}

View File

@ -1,35 +1,11 @@
package main
import (
"flag"
"fmt"
"fusenapi/server/home-user-auth/internal/config"
"fusenapi/server/home-user-auth/internal/handler"
"fusenapi/server/home-user-auth/internal/svc"
"testing"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
)
// var configFile = flag.String("f", "etc/home-user-auth.yaml", "the config file")
func GetTestServer() *rest.Server {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
server := rest.MustNewServer(c.RestConf)
defer server.Stop()
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
return server
}
func TestMain(t *testing.T) {
// log.Println(model.RawFieldNames[FsCanteenType]())
main()

View File

@ -1,37 +0,0 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/server/home-user-auth/internal/logic"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
)
func UserBasicInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.Request
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
l := logic.NewUserBasicInfoLogic(r.Context(), svcCtx)
resp := l.UserBasicInfo(&req)
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@ -1,37 +0,0 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/server/home-user-auth/internal/logic"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
)
func UserFontsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.Request
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
l := logic.NewUserFontsLogic(r.Context(), svcCtx)
resp := l.UserFonts(&req)
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@ -1,37 +0,0 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/server/home-user-auth/internal/logic"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
)
func UserGetTypeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.Request
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
l := logic.NewUserGetTypeLogic(r.Context(), svcCtx)
resp := l.UserGetType(&req)
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@ -1,37 +0,0 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/server/home-user-auth/internal/logic"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
)
func UserSaveBasicInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.RequestBasicInfoForm
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
l := logic.NewUserSaveBasicInfoLogic(r.Context(), svcCtx)
resp := l.UserSaveBasicInfo(&req)
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@ -1,37 +0,0 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/server/home-user-auth/internal/logic"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
)
func UserStatusConfigHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.Request
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
l := logic.NewUserStatusConfigLogic(r.Context(), svcCtx)
resp := l.UserStatusConfig(&req)
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@ -0,0 +1,60 @@
package server_test
import (
"flag"
"fmt"
"fusenapi/server/home-user-auth/internal/config"
"fusenapi/server/home-user-auth/internal/handler"
"fusenapi/server/home-user-auth/internal/svc"
"log"
"testing"
"github.com/474420502/requests"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
)
var configFile = flag.String("f", "../etc/home-user-auth.yaml", "the config file")
func GetTestServer() *rest.Server {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
server := rest.MustNewServer(c.RestConf)
defer server.Stop()
ctx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
return server
}
func TestCaseAddressList(t *testing.T) {
server := GetTestServer()
// http.NewRequest("POST", "http://localhost:8888/user/login", body io.Reader)
ses := requests.NewSession()
tp := ses.Post("http://localhost:8888/user/login")
tp.SetBodyJson(map[string]interface{}{
"name": "devenv@sina.cn",
"pwd": "$2y$13$6UFDMZQMEfqFYiNLpiUCi.B3fpvGEamPAjIgzUqv/u7jT05nB3pOC",
})
resp, err := tp.TestInServer(server)
if err != nil {
t.Error(err)
}
result := resp.Json()
token := result.Get("data.token")
ses.Header.Add("Authorization", token.String())
log.Println(resp.ContentString())
resp, err = ses.Get("http://localhost:8888/user/address-list").TestInServer(server)
if err != nil {
panic(err)
}
log.Println(string(resp.Content()))
}