作者 yangfu

test

package controllers
import (
"fmt"
"strconv"
"strings"
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/validation"
"gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
"gitlab.fjmaimaimai.com/mmm-go/ability/services/auth"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
"strconv"
//"strconv"
"strings"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/validation"
)
type BaseController struct {
... ... @@ -41,7 +41,6 @@ func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Mess
}
return
}
//GenMessage genarate a response message
func (this *BaseController)GenMessage(rsp interface{},err error)*mybeego.Message{
var msg *mybeego.Message
... ... @@ -50,7 +49,7 @@ func (this *BaseController)GenMessage(rsp interface{},err error)*mybeego.Message
msg.Data = rsp
return msg
}
log.Error(err)
//log.Error(err)
if e,ok :=err.(common.Error);ok{
msg = mybeego.NewMessage(e.Code)
msg.Data = rsp
... ... @@ -59,7 +58,7 @@ func (this *BaseController)GenMessage(rsp interface{},err error)*mybeego.Message
msg = mybeego.NewMessage(1)
return msg
}
//获取请求头信息
func GetRequestHeader(ctx *context.Context)*protocol.RequestHeader{
h :=&protocol.RequestHeader{}
h.AccessToken = ctx.Input.Header("x-mmm-accesstoken")
... ... @@ -72,6 +71,7 @@ func GetRequestHeader(ctx *context.Context)*protocol.RequestHeader{
return h
}
//过滤器
func FilterComm(ctx *context.Context){
if strings.HasSuffix(ctx.Request.RequestURI,"login"){
return
... ...
package controllers
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
"log"
"reflect"
"testing"
)
func Test_GenMessage(t *testing.T){
input := []struct {
Rsp interface{}
Error error
Exceprt *mybeego.Message
}{
{Rsp:"test",Error:fmt.Errorf("test"),Exceprt:mybeego.NewMessage(1)},
{Rsp:"test-A",Error:common.NewErrorWithMsg(100,"test-A"),Exceprt:mybeego.NewErrMessage(100,"test-A")},
}
for i:=range input{
o :=input[i]
out :=DefaultController.GenMessage(o.Rsp,o.Error)
if !reflect.DeepEqual(out,o.Exceprt){
log.Fatal("not equal ",out,o.Exceprt)
}
}
}
func Benchmark_GenMessage(b *testing.B) {
o := struct {
Rsp interface{}
Error error
Exceprt *mybeego.Message
}{Rsp:"test",Error:fmt.Errorf("test"),Exceprt:mybeego.NewMessage(1)}
for i:=0;i<b.N;i++{
out :=DefaultController.GenMessage(o.Rsp,o.Error)
if out.Errmsg!=o.Exceprt.Errmsg || out.Errno!=o.Exceprt.Errno{
log.Fatal("not equal ",out,o.Exceprt)
}
}
}
... ...
... ... @@ -4,6 +4,7 @@ import (
"encoding/json"
"gitlab.fjmaimaimai.com/mmm-go/ability/controllers"
"gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
... ...
... ... @@ -132,7 +132,7 @@ func CheckUuid(request *protocol.CheckUuidRequest)(rsp *protocol.CheckUuidRespon
}
logUuid,err=models.GetLogUuidByUuid(request.Uuid)
if err==nil && logUuid!=nil{
err = common.NewErrorWithMsg(4142,"uuid not empty")
err = common.NewErrorWithMsg(4142,"uuid not valid")
}
models.AddLogUuid(&models.LogUuid{Uuid:request.Uuid})
rsp =&protocol.CheckUuidResponse{}
... ...