正在显示
5 个修改的文件
包含
189 行增加
和
46 行删除
| 1 | package controllers | 1 | package controllers |
| 2 | 2 | ||
| 3 | -import "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" | 3 | +import ( |
| 4 | + "encoding/hex" | ||
| 5 | + "encoding/json" | ||
| 6 | + "fmt" | ||
| 7 | + "github.com/astaxie/beego" | ||
| 8 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" | ||
| 9 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
| 10 | + "github.com/astaxie/beego/httplib" | ||
| 11 | + "io/ioutil" | ||
| 12 | + "net/http" | ||
| 13 | + "path" | ||
| 14 | + "reflect" | ||
| 15 | + "strings" | ||
| 16 | +) | ||
| 4 | 17 | ||
| 5 | type ABController struct { | 18 | type ABController struct { |
| 6 | BaseController | 19 | BaseController |
| 7 | } | 20 | } |
| 8 | 21 | ||
| 22 | +var aHost string | ||
| 23 | +var bHost string | ||
| 24 | + | ||
| 25 | +const ( | ||
| 26 | + QueryResponseError = 1 //请求应答错误 | ||
| 27 | + CompareResultError=2 //应答对比错误 | ||
| 28 | +) | ||
| 29 | + | ||
| 30 | +func init(){ | ||
| 31 | + aHost =beego.AppConfig.String("AHost") | ||
| 32 | + bHost =beego.AppConfig.String("BHost") | ||
| 33 | +} | ||
| 34 | + | ||
| 9 | //Compare | 35 | //Compare |
| 10 | func(this *ABController)Compare(){ | 36 | func(this *ABController)Compare(){ |
| 11 | - var msg *mybeego.Message | 37 | + var ( |
| 38 | + msg *mybeego.Message | ||
| 39 | + status string = "OK" | ||
| 40 | + ) | ||
| 12 | defer func(){ | 41 | defer func(){ |
| 13 | this.Resp(msg) | 42 | this.Resp(msg) |
| 14 | }() | 43 | }() |
| 15 | - //this.ByteBody | ||
| 16 | - //msg = this.GenMessage(aB.Compare(request)) | 44 | + method := this.Ctx.Request.Method |
| 45 | + head :=this.Ctx.Request.Header | ||
| 46 | + body :=this.ByteBody | ||
| 47 | + newRequest:= NewRequest(bHost,method,head,body) | ||
| 48 | + aResp,err :=makeHttpRequest(aHost,method,head,body) | ||
| 49 | + if err!=nil{ | ||
| 50 | + status ="FAIL" | ||
| 51 | + } | ||
| 52 | + log.Debug(fmt.Sprintf("[%v]- Method:%v \n Head:\n%v \nBody:\n%v \n Response:\n%v",status,method,head,body,aResp)) | ||
| 53 | + this.Data["json"] = aResp | ||
| 54 | + | ||
| 55 | + Compare :=func(){ | ||
| 56 | + defer func(){ | ||
| 57 | + if p:=recover();p!=nil{ | ||
| 58 | + log.Error(p) | ||
| 59 | + } | ||
| 60 | + }() | ||
| 61 | + //判断已经在错误列表里面就不再判断请求 | ||
| 62 | + bResp,err :=makeHttpRequest(bHost,method,head,body) | ||
| 63 | + if err!=nil{ | ||
| 64 | + log.Error(err) | ||
| 65 | + errorHandler(QueryResponseError,newRequest) | ||
| 66 | + return | ||
| 67 | + } | ||
| 68 | + cResult,err :=compareResponse(aResp,bResp) | ||
| 69 | + if err!=nil{ | ||
| 70 | + log.Error(err) | ||
| 71 | + errorHandler(QueryResponseError,newRequest) | ||
| 72 | + return | ||
| 73 | + } | ||
| 74 | + if !cResult{ | ||
| 75 | + errorHandler(CompareResultError,newRequest) | ||
| 76 | + log.Warn(fmt.Sprintf("" + | ||
| 77 | + "[Companre Error] - ErrorType:%v " + | ||
| 78 | + "\nMethod:\n %v " + | ||
| 79 | + "\nHead:\n %v " + | ||
| 80 | + "\nAResponse:\n %v " + | ||
| 81 | + "\nBResponse:\n %v",CompareResultError,method,head,string(aResp),string(bResp))) | ||
| 82 | + }else{ | ||
| 83 | + //记录到记录行里面 | ||
| 84 | + log.Info("[Success] \nMethod:\n %v " + | ||
| 85 | + "\nHead:\n %v " + | ||
| 86 | + "\nBody:\n%v",method,head,string(body)) | ||
| 87 | + } | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + go Compare() | ||
| 91 | + this.ServeJSON() | ||
| 92 | +} | ||
| 93 | +func compareResponse(aResp,bResp []byte)(result bool,err error){ | ||
| 94 | + defer func(){ | ||
| 95 | + if p:=recover();p!=nil{ | ||
| 96 | + log.Error(p) | ||
| 97 | + } | ||
| 98 | + }() | ||
| 99 | + var mapA = make(map[string]interface{}) | ||
| 100 | + var mapB = make(map[string]interface{}) | ||
| 101 | + | ||
| 102 | + if err =json.Unmarshal(aResp,&mapA);err!=nil{ | ||
| 103 | + result = false | ||
| 104 | + return | ||
| 105 | + } | ||
| 106 | + if err =json.Unmarshal(bResp,&mapB);err!=nil{ | ||
| 107 | + result = false | ||
| 108 | + return | ||
| 109 | + } | ||
| 110 | + result = reflect.DeepEqual(mapA,mapB) | ||
| 111 | + return | ||
| 112 | +} | ||
| 113 | +func errorHandler(errType int,req Request){ | ||
| 114 | + //写redis error | ||
| 115 | +} | ||
| 116 | +func makeHttpRequest(host,method string,head map[string][]string,body []byte)(data []byte,err error){ | ||
| 117 | + var ( | ||
| 118 | + resp *http.Response | ||
| 119 | + ) | ||
| 120 | + rawUrl := path.Join(host,method) | ||
| 121 | + req := httplib.NewBeegoRequest(rawUrl,"Post") | ||
| 122 | + for k,v:=range head{ | ||
| 123 | + req.Header(k,strings.Join(v,",")) | ||
| 124 | + } | ||
| 125 | + req.Body(body) | ||
| 126 | + resp,err =req.DoRequest() | ||
| 127 | + if err!=nil{ | ||
| 128 | + return | ||
| 129 | + } | ||
| 130 | + data,err = ioutil.ReadAll(resp.Body) | ||
| 131 | + defer resp.Body.Close() | ||
| 132 | + return | ||
| 133 | +} | ||
| 134 | + | ||
| 135 | + | ||
| 136 | +type Request struct { | ||
| 137 | + Host string | ||
| 138 | + Method string | ||
| 139 | + Head map[string][]string | ||
| 140 | + Body string //hex | ||
| 141 | +} | ||
| 142 | +func NewRequest(host,method string,head map[string][]string,body []byte)Request{ | ||
| 143 | + return Request{ | ||
| 144 | + Host:host, | ||
| 145 | + Method:method, | ||
| 146 | + Head:head, | ||
| 147 | + Body:hex.EncodeToString(body), | ||
| 148 | + } | ||
| 149 | +} | ||
| 150 | +func(req Request)GetBody()([]byte){ | ||
| 151 | + data,err:= hex.DecodeString(req.Body) | ||
| 152 | + if err!=nil{ | ||
| 153 | + log.Error(err) | ||
| 154 | + return []byte{} | ||
| 155 | + } | ||
| 156 | + return data | ||
| 17 | } | 157 | } |
controllers/ab_test.go
0 → 100644
| 1 | +package controllers | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "reflect" | ||
| 6 | + "testing" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +func Test_DeepEqual(t *testing.T) { | ||
| 10 | + input := make(map[string]interface{}) | ||
| 11 | + input["A"] =1 | ||
| 12 | + input["B"] ="2" | ||
| 13 | + input["C"]=map[string]interface{}{"C1":1,"C2":"2"} | ||
| 14 | + | ||
| 15 | + input1 := make(map[string]interface{}) | ||
| 16 | + input1["A"] =1 | ||
| 17 | + input1["B"] ="2" | ||
| 18 | + input1["C"]=map[string]interface{}{"C1":1,"C2":"2"} | ||
| 19 | + | ||
| 20 | + input2 := make(map[string]interface{}) | ||
| 21 | + input2["A"] =1 | ||
| 22 | + input2["B"] ="3" | ||
| 23 | + input2["C"]=map[string]interface{}{"C1":1,"C2":"2"} | ||
| 24 | + | ||
| 25 | + input3 := make(map[string]interface{}) | ||
| 26 | + input3["A"] =1 | ||
| 27 | + input3["B"] ="2" | ||
| 28 | + input3["C"]=map[string]interface{}{"C1":1,"C2":"3"} | ||
| 29 | + | ||
| 30 | + if !reflect.DeepEqual(input,input1){ | ||
| 31 | + t.Fatal(fmt.Sprintf("map should equal %v %v",input,input1)) | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + if reflect.DeepEqual(input,input2){ | ||
| 35 | + t.Fatal(fmt.Sprintf("map should not equal %v %v",input,input2)) | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + if reflect.DeepEqual(input,input3){ | ||
| 39 | + t.Fatal(fmt.Sprintf("map should not equal %v %v",input,input3)) | ||
| 40 | + } | ||
| 41 | +} |
tests/default_test.go
已删除
100644 → 0
| 1 | -package test | ||
| 2 | - | ||
| 3 | -import ( | ||
| 4 | - "net/http" | ||
| 5 | - "net/http/httptest" | ||
| 6 | - "testing" | ||
| 7 | - "runtime" | ||
| 8 | - "path/filepath" | ||
| 9 | - _ "ab/routers" | ||
| 10 | - | ||
| 11 | - "github.com/astaxie/beego" | ||
| 12 | - . "github.com/smartystreets/goconvey/convey" | ||
| 13 | -) | ||
| 14 | - | ||
| 15 | -func init() { | ||
| 16 | - _, file, _, _ := runtime.Caller(0) | ||
| 17 | - apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator)))) | ||
| 18 | - beego.TestBeegoInit(apppath) | ||
| 19 | -} | ||
| 20 | - | ||
| 21 | - | ||
| 22 | -// TestBeego is a sample to run an endpoint test | ||
| 23 | -func TestBeego(t *testing.T) { | ||
| 24 | - r, _ := http.NewRequest("GET", "/", nil) | ||
| 25 | - w := httptest.NewRecorder() | ||
| 26 | - beego.BeeApp.Handlers.ServeHTTP(w, r) | ||
| 27 | - | ||
| 28 | - beego.Trace("testing", "TestBeego", "Code[%d]\n%s", w.Code, w.Body.String()) | ||
| 29 | - | ||
| 30 | - Convey("Subject: Test Station Endpoint\n", t, func() { | ||
| 31 | - Convey("Status Code Should Be 200", func() { | ||
| 32 | - So(w.Code, ShouldEqual, 200) | ||
| 33 | - }) | ||
| 34 | - Convey("The Result Should Not Be Empty", func() { | ||
| 35 | - So(w.Body.Len(), ShouldBeGreaterThan, 0) | ||
| 36 | - }) | ||
| 37 | - }) | ||
| 38 | -} | ||
| 39 | - |
-
请 注册 或 登录 后发表评论