最新可以跑单元测试

This commit is contained in:
eson
2023-06-12 18:08:34 +08:00
parent e0b0e2aff8
commit fc806b71c6
13 changed files with 165 additions and 86 deletions

View File

@@ -6,7 +6,6 @@ import (
"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"
@@ -14,30 +13,29 @@ import (
"github.com/zeromicro/go-zero/rest"
)
var configFile = flag.String("f", "../etc/home-user-auth.yaml", "the config file")
var testConfigFile = flag.String("f", "../etc/home-user-auth.yaml", "the config file")
var cnf config.Config
func GetTestServer() *rest.Server {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
conf.MustLoad(*testConfigFile, &cnf)
server := rest.MustNewServer(c.RestConf)
server := rest.MustNewServer(cnf.RestConf)
defer server.Stop()
ctx := svc.NewServiceContext(c)
ctx := svc.NewServiceContext(cnf)
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
fmt.Printf("Starting server at %s:%d...\n", cnf.Host, cnf.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 := ses.Post(fmt.Sprintf("http://%s:%d/user/login", cnf.Host, cnf.Port))
tp.SetBodyJson(map[string]interface{}{
"name": "devenv@sina.cn",
"pwd": "$2y$13$6UFDMZQMEfqFYiNLpiUCi.B3fpvGEamPAjIgzUqv/u7jT05nB3pOC",
@@ -48,13 +46,28 @@ func TestCaseAddressList(t *testing.T) {
}
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)
if !token.Exists() {
t.Error("data.token is not exists")
}
log.Println(string(resp.Content()))
ses.Header.Add("Authorization", token.String())
resp, err = ses.Get(fmt.Sprintf("http://%s:%d/user/address-list", cnf.Host, cnf.Port)).TestInServer(server)
if err != nil {
t.Error(err)
}
result = resp.Json().Get("code")
if !result.Exists() {
t.Error("code is not exists")
}
if result.Int() != 200 {
t.Error("code != 200")
}
result = resp.Json().Get("msg")
if !result.Exists() {
t.Error("msg is not exists")
}
if result.String() != "success" {
t.Error(result.String())
}
}