func (s *ZService) parseReqMsg(req *http.Request) (reqMsg *ZMsg, err error) { msgName, err := s.parseMsgName(req.RequestURI) if err != nil { return nil, err } reqMsg = NewZMsg() reqMsg.SetName(msgName) if req.ContentLength > 0 && req.Header.Get("Content-Type") == ZC_MSG_PAYLOAD_OBJECT { payload := make([]byte, req.ContentLength) l, err := req.Body.Read(payload) if l != len(payload) { return nil, errors.New("read payload failed") } reqMsg.SetPayload(payload, req.Header.Get("Content-Type")) err = reqMsg.decodeObject() if err != nil { log.Info(string(payload)) return nil, errors.New("unmarshal payload failed") } return reqMsg, nil } else if len(req.URL.RawQuery) > 0 { urlValues, err := url.ParseQuery(req.URL.RawQuery) if err != nil { return nil, errors.New("unmarshal raw query params failed") } for key, values := range urlValues { reqMsg.Put(key, values[0]) } reqMsg.encodeObject() log.Info("get req msg from raw query: %", reqMsg) return reqMsg, nil } return nil, errors.New("parse request failed") }
func TestZObject(t *testing.T) { o := NewZObject() o.PutString("name", "lihailei") o.PutInt("age", 1234) o.PutBool("vip", false) o.AddInt("myson", 1) o.AddInt("myson", 2) if o.GetString("name") != "lihailei" { t.Error("wrong name") } if o.GetBool("vip") != false { t.Error("wrong vip flag") } if len(o.GetBools("vips")) != 0 { t.Error("wrong vip flags") } if len(o.GetStrings("names")) != 0 { t.Error("wrong names") } o2 := NewZObject() o2.PutString("name", "lihailei2") o2.PutInt("age", 233) o2.PutBool("vip", true) o.AddObject("sons", o2) o3 := NewZObject() o3.PutString("name", "lihailei3") o3.PutInt("age", 23) o3.PutBool("vip", false) o.AddObject("sons", o3) opayload, _ := json.Marshal(o) o2payload, _ := json.Marshal(o2) o3payload, _ := json.Marshal(o3) log.Info("o: ", string(opayload)) log.Info("o2: ", string(o2payload)) log.Info("o3: ", string(o3payload)) o4 := NewZObject() o5 := o4 o4["testkey"] = "testvalue" log.Info(o4, o5) var gt = getMap() log.Infof("%p", gt) log.Info(len(gt)) gt["lihailei"] = 2 log.Info(gt) }
func TestMarshalParams(t *testing.T) { m := NewZMsg() m.SetName("hello") m.SetVersion(uint8(1)) m.Put("text", "hello world") m.Put("from", "lihailei") m.Put("to", "zhidong") m.encodeObject() payload, payloadFormat := m.GetPayload() if payloadFormat != ZC_MSG_PAYLOAD_OBJECT { t.Log("wrong palyload format") t.FailNow() } s := NewZMsg() s.SetName(m.GetName()) s.SetVersion(m.GetVersion()) s.SetPayload(payload, payloadFormat) s.decodeObject() spayload, spayloadFormat := s.GetPayload() if spayloadFormat != ZC_MSG_PAYLOAD_OBJECT { t.Error("wrong palyload format") } if len(payload) != len(spayload) || len(m.ZObject) != len(s.ZObject) { t.Errorf("wrong marshaled payload: %s => %s\n", string(payload), string(spayload)) } for key, value := range m.ZObject { if value != s.Get(key) { t.Errorf("wrong marshaled payload: %s => %s\n", string(payload), string(spayload)) } } zo := NewZObject() zo.Put("name", "lihailei") create := NewZMsg() create.Put("zc-class", "account") create.Put("zc-object", zo) create.encodeObject() createPayload, _ := create.GetPayload() log.Info(string(createPayload)) }