作者 yangfu

test

1 package controllers 1 package controllers
2 2
3 import ( 3 import (
  4 + "fmt"
  5 + "strconv"
  6 + "strings"
4 "crypto/sha256" 7 "crypto/sha256"
5 "encoding/hex" 8 "encoding/hex"
6 - "fmt"  
7 - "github.com/astaxie/beego"  
8 - "github.com/astaxie/beego/context"  
9 - "github.com/astaxie/beego/validation" 9 +
10 "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" 10 "gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
11 "gitlab.fjmaimaimai.com/mmm-go/ability/services/auth" 11 "gitlab.fjmaimaimai.com/mmm-go/ability/services/auth"
12 "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" 12 "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
13 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 13 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
14 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" 14 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
15 - "strconv"  
16 15
17 - //"strconv"  
18 - "strings" 16 + "github.com/astaxie/beego"
  17 + "github.com/astaxie/beego/context"
  18 + "github.com/astaxie/beego/validation"
19 ) 19 )
20 20
21 type BaseController struct { 21 type BaseController struct {
@@ -41,7 +41,6 @@ func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Mess @@ -41,7 +41,6 @@ func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Mess
41 } 41 }
42 return 42 return
43 } 43 }
44 -  
45 //GenMessage genarate a response message 44 //GenMessage genarate a response message
46 func (this *BaseController)GenMessage(rsp interface{},err error)*mybeego.Message{ 45 func (this *BaseController)GenMessage(rsp interface{},err error)*mybeego.Message{
47 var msg *mybeego.Message 46 var msg *mybeego.Message
@@ -50,7 +49,7 @@ func (this *BaseController)GenMessage(rsp interface{},err error)*mybeego.Message @@ -50,7 +49,7 @@ func (this *BaseController)GenMessage(rsp interface{},err error)*mybeego.Message
50 msg.Data = rsp 49 msg.Data = rsp
51 return msg 50 return msg
52 } 51 }
53 - log.Error(err) 52 + //log.Error(err)
54 if e,ok :=err.(common.Error);ok{ 53 if e,ok :=err.(common.Error);ok{
55 msg = mybeego.NewMessage(e.Code) 54 msg = mybeego.NewMessage(e.Code)
56 msg.Data = rsp 55 msg.Data = rsp
@@ -59,7 +58,7 @@ func (this *BaseController)GenMessage(rsp interface{},err error)*mybeego.Message @@ -59,7 +58,7 @@ func (this *BaseController)GenMessage(rsp interface{},err error)*mybeego.Message
59 msg = mybeego.NewMessage(1) 58 msg = mybeego.NewMessage(1)
60 return msg 59 return msg
61 } 60 }
62 - 61 +//获取请求头信息
63 func GetRequestHeader(ctx *context.Context)*protocol.RequestHeader{ 62 func GetRequestHeader(ctx *context.Context)*protocol.RequestHeader{
64 h :=&protocol.RequestHeader{} 63 h :=&protocol.RequestHeader{}
65 h.AccessToken = ctx.Input.Header("x-mmm-accesstoken") 64 h.AccessToken = ctx.Input.Header("x-mmm-accesstoken")
@@ -72,6 +71,7 @@ func GetRequestHeader(ctx *context.Context)*protocol.RequestHeader{ @@ -72,6 +71,7 @@ func GetRequestHeader(ctx *context.Context)*protocol.RequestHeader{
72 return h 71 return h
73 } 72 }
74 73
  74 +//过滤器
75 func FilterComm(ctx *context.Context){ 75 func FilterComm(ctx *context.Context){
76 if strings.HasSuffix(ctx.Request.RequestURI,"login"){ 76 if strings.HasSuffix(ctx.Request.RequestURI,"login"){
77 return 77 return
  1 +package controllers
  2 +
  3 +import (
  4 + "fmt"
  5 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
  6 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
  7 + "log"
  8 + "reflect"
  9 + "testing"
  10 +)
  11 +
  12 +func Test_GenMessage(t *testing.T){
  13 + input := []struct {
  14 + Rsp interface{}
  15 + Error error
  16 + Exceprt *mybeego.Message
  17 + }{
  18 + {Rsp:"test",Error:fmt.Errorf("test"),Exceprt:mybeego.NewMessage(1)},
  19 + {Rsp:"test-A",Error:common.NewErrorWithMsg(100,"test-A"),Exceprt:mybeego.NewErrMessage(100,"test-A")},
  20 + }
  21 +
  22 + for i:=range input{
  23 + o :=input[i]
  24 + out :=DefaultController.GenMessage(o.Rsp,o.Error)
  25 + if !reflect.DeepEqual(out,o.Exceprt){
  26 + log.Fatal("not equal ",out,o.Exceprt)
  27 + }
  28 + }
  29 +}
  30 +
  31 +func Benchmark_GenMessage(b *testing.B) {
  32 + o := struct {
  33 + Rsp interface{}
  34 + Error error
  35 + Exceprt *mybeego.Message
  36 + }{Rsp:"test",Error:fmt.Errorf("test"),Exceprt:mybeego.NewMessage(1)}
  37 + for i:=0;i<b.N;i++{
  38 + out :=DefaultController.GenMessage(o.Rsp,o.Error)
  39 + if out.Errmsg!=o.Exceprt.Errmsg || out.Errno!=o.Exceprt.Errno{
  40 + log.Fatal("not equal ",out,o.Exceprt)
  41 + }
  42 + }
  43 +}
@@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
4 "encoding/json" 4 "encoding/json"
5 "gitlab.fjmaimaimai.com/mmm-go/ability/controllers" 5 "gitlab.fjmaimaimai.com/mmm-go/ability/controllers"
6 "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" 6 "gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
  7 +
7 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" 8 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
8 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 9 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
9 10
@@ -132,7 +132,7 @@ func CheckUuid(request *protocol.CheckUuidRequest)(rsp *protocol.CheckUuidRespon @@ -132,7 +132,7 @@ func CheckUuid(request *protocol.CheckUuidRequest)(rsp *protocol.CheckUuidRespon
132 } 132 }
133 logUuid,err=models.GetLogUuidByUuid(request.Uuid) 133 logUuid,err=models.GetLogUuidByUuid(request.Uuid)
134 if err==nil && logUuid!=nil{ 134 if err==nil && logUuid!=nil{
135 - err = common.NewErrorWithMsg(4142,"uuid not empty") 135 + err = common.NewErrorWithMsg(4142,"uuid not valid")
136 } 136 }
137 models.AddLogUuid(&models.LogUuid{Uuid:request.Uuid}) 137 models.AddLogUuid(&models.LogUuid{Uuid:request.Uuid})
138 rsp =&protocol.CheckUuidResponse{} 138 rsp =&protocol.CheckUuidResponse{}