作者 yangfu

compare

... ... @@ -7,5 +7,5 @@ redis_add_port = "127.0.0.1:6379"
redis_auth = "123456"
#远程
AHost = ""
BHost = ""
AHost = "127.0.0.1:8081"
BHost = "127.0.0.1:8082"
... ...
package controllers
import "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/astaxie/beego"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"github.com/astaxie/beego/httplib"
"io/ioutil"
"net/http"
"path"
"reflect"
"strings"
)
type ABController struct {
BaseController
}
var aHost string
var bHost string
const (
QueryResponseError = 1 //请求应答错误
CompareResultError=2 //应答对比错误
)
func init(){
aHost =beego.AppConfig.String("AHost")
bHost =beego.AppConfig.String("BHost")
}
//Compare
func(this *ABController)Compare(){
var msg *mybeego.Message
var (
msg *mybeego.Message
status string = "OK"
)
defer func(){
this.Resp(msg)
}()
//this.ByteBody
//msg = this.GenMessage(aB.Compare(request))
method := this.Ctx.Request.Method
head :=this.Ctx.Request.Header
body :=this.ByteBody
newRequest:= NewRequest(bHost,method,head,body)
aResp,err :=makeHttpRequest(aHost,method,head,body)
if err!=nil{
status ="FAIL"
}
log.Debug(fmt.Sprintf("[%v]- Method:%v \n Head:\n%v \nBody:\n%v \n Response:\n%v",status,method,head,body,aResp))
this.Data["json"] = aResp
Compare :=func(){
defer func(){
if p:=recover();p!=nil{
log.Error(p)
}
}()
//判断已经在错误列表里面就不再判断请求
bResp,err :=makeHttpRequest(bHost,method,head,body)
if err!=nil{
log.Error(err)
errorHandler(QueryResponseError,newRequest)
return
}
cResult,err :=compareResponse(aResp,bResp)
if err!=nil{
log.Error(err)
errorHandler(QueryResponseError,newRequest)
return
}
if !cResult{
errorHandler(CompareResultError,newRequest)
log.Warn(fmt.Sprintf("" +
"[Companre Error] - ErrorType:%v " +
"\nMethod:\n %v " +
"\nHead:\n %v " +
"\nAResponse:\n %v " +
"\nBResponse:\n %v",CompareResultError,method,head,string(aResp),string(bResp)))
}else{
//记录到记录行里面
log.Info("[Success] \nMethod:\n %v " +
"\nHead:\n %v " +
"\nBody:\n%v",method,head,string(body))
}
}
go Compare()
this.ServeJSON()
}
func compareResponse(aResp,bResp []byte)(result bool,err error){
defer func(){
if p:=recover();p!=nil{
log.Error(p)
}
}()
var mapA = make(map[string]interface{})
var mapB = make(map[string]interface{})
if err =json.Unmarshal(aResp,&mapA);err!=nil{
result = false
return
}
if err =json.Unmarshal(bResp,&mapB);err!=nil{
result = false
return
}
result = reflect.DeepEqual(mapA,mapB)
return
}
func errorHandler(errType int,req Request){
//写redis error
}
func makeHttpRequest(host,method string,head map[string][]string,body []byte)(data []byte,err error){
var (
resp *http.Response
)
rawUrl := path.Join(host,method)
req := httplib.NewBeegoRequest(rawUrl,"Post")
for k,v:=range head{
req.Header(k,strings.Join(v,","))
}
req.Body(body)
resp,err =req.DoRequest()
if err!=nil{
return
}
data,err = ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
return
}
type Request struct {
Host string
Method string
Head map[string][]string
Body string //hex
}
func NewRequest(host,method string,head map[string][]string,body []byte)Request{
return Request{
Host:host,
Method:method,
Head:head,
Body:hex.EncodeToString(body),
}
}
func(req Request)GetBody()([]byte){
data,err:= hex.DecodeString(req.Body)
if err!=nil{
log.Error(err)
return []byte{}
}
return data
}
\ No newline at end of file
... ...
package controllers
import (
"fmt"
"reflect"
"testing"
)
func Test_DeepEqual(t *testing.T) {
input := make(map[string]interface{})
input["A"] =1
input["B"] ="2"
input["C"]=map[string]interface{}{"C1":1,"C2":"2"}
input1 := make(map[string]interface{})
input1["A"] =1
input1["B"] ="2"
input1["C"]=map[string]interface{}{"C1":1,"C2":"2"}
input2 := make(map[string]interface{})
input2["A"] =1
input2["B"] ="3"
input2["C"]=map[string]interface{}{"C1":1,"C2":"2"}
input3 := make(map[string]interface{})
input3["A"] =1
input3["B"] ="2"
input3["C"]=map[string]interface{}{"C1":1,"C2":"3"}
if !reflect.DeepEqual(input,input1){
t.Fatal(fmt.Sprintf("map should equal %v %v",input,input1))
}
if reflect.DeepEqual(input,input2){
t.Fatal(fmt.Sprintf("map should not equal %v %v",input,input2))
}
if reflect.DeepEqual(input,input3){
t.Fatal(fmt.Sprintf("map should not equal %v %v",input,input3))
}
}
... ...
... ... @@ -6,5 +6,6 @@ import (
)
func init() {
beego.Router("/", &controllers.MainController{})
//beego.Router("/", &controllers.MainController{})
beego.Router("/*", &controllers.ABController{},"Post:Compare")
}
... ...
package test
import (
"net/http"
"net/http/httptest"
"testing"
"runtime"
"path/filepath"
_ "ab/routers"
"github.com/astaxie/beego"
. "github.com/smartystreets/goconvey/convey"
)
func init() {
_, file, _, _ := runtime.Caller(0)
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
beego.TestBeegoInit(apppath)
}
// TestBeego is a sample to run an endpoint test
func TestBeego(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Trace("testing", "TestBeego", "Code[%d]\n%s", w.Code, w.Body.String())
Convey("Subject: Test Station Endpoint\n", t, func() {
Convey("Status Code Should Be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("The Result Should Not Be Empty", func() {
So(w.Body.Len(), ShouldBeGreaterThan, 0)
})
})
}