| 122 | } |
| 123 | |
| 124 | func TestTarsProtocol_ResponseUnpack(t *testing.T) { |
| 125 | resp := &requestf.ResponsePacket{ |
| 126 | IVersion: basef.TARSVERSION, |
| 127 | CPacketType: 1, |
| 128 | IRequestId: 2, |
| 129 | IMessageType: 3, |
| 130 | IRet: 4, |
| 131 | SBuffer: []int8{1, 2, 3, 4, 5, 6, 7, 8}, |
| 132 | Status: map[string]string{"hello": "tarser"}, |
| 133 | SResultDesc: "succ", |
| 134 | Context: map[string]string{"hello": "tars"}, |
| 135 | } |
| 136 | buf := codec.NewBuffer() |
| 137 | err := resp.WriteTo(buf) |
| 138 | |
| 139 | // append package length to package header. |
| 140 | sbuf := bytes.NewBuffer(nil) |
| 141 | sbuf.Write(make([]byte, 4)) |
| 142 | sbuf.Write(buf.ToBytes()) |
| 143 | binary.BigEndian.PutUint32(sbuf.Bytes(), uint32(sbuf.Len())) |
| 144 | if err != nil { |
| 145 | t.Errorf("write buffer failed. got error:%v\n", err) |
| 146 | } |
| 147 | p := &TarsProtocol{} |
| 148 | |
| 149 | got, err := p.ResponseUnpack(sbuf.Bytes()) |
| 150 | if err != nil { |
| 151 | t.Errorf("unpack response failed. got error:%v\n", err) |
| 152 | } |
| 153 | |
| 154 | assert.Equal(t, got, resp, "Failed to test ResponseUnpack") |
| 155 | } |