|
|
@@ -2,19 +2,10 @@ package sarama
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
+ "reflect"
|
|
|
"testing"
|
|
|
)
|
|
|
|
|
|
-var (
|
|
|
- requestSimple = []byte{
|
|
|
- 0x00, 0x00, 0x00, 0x17, // msglen
|
|
|
- 0x06, 0x66,
|
|
|
- 0x00, 0xD2,
|
|
|
- 0x00, 0x00, 0x12, 0x34,
|
|
|
- 0x00, 0x08, 'm', 'y', 'C', 'l', 'i', 'e', 'n', 't',
|
|
|
- 0x00, 0x03, 'a', 'b', 'c'}
|
|
|
-)
|
|
|
-
|
|
|
type testRequestBody struct {
|
|
|
}
|
|
|
|
|
|
@@ -30,11 +21,6 @@ func (s *testRequestBody) encode(pe packetEncoder) error {
|
|
|
return pe.putString("abc")
|
|
|
}
|
|
|
|
|
|
-func TestRequest(t *testing.T) {
|
|
|
- request := request{correlationID: 0x1234, id: "myClient", body: new(testRequestBody)}
|
|
|
- testEncodable(t, "simple", &request, requestSimple)
|
|
|
-}
|
|
|
-
|
|
|
// not specific to request tests, just helper functions for testing structures that
|
|
|
// implement the encoder or decoder interfaces that needed somewhere to live
|
|
|
|
|
|
@@ -53,3 +39,24 @@ func testDecodable(t *testing.T, name string, out decoder, in []byte) {
|
|
|
t.Error("Decoding", name, "failed:", err)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+func testRequest(t *testing.T, name string, rb requestBody, expected []byte) {
|
|
|
+ // Encoder request
|
|
|
+ req := &request{correlationID: 123, clientID: "foo", body: rb}
|
|
|
+ packet, err := encode(req)
|
|
|
+ headerSize := 14 + len("foo")
|
|
|
+ if err != nil {
|
|
|
+ t.Error(err)
|
|
|
+ } else if !bytes.Equal(packet[headerSize:], expected) {
|
|
|
+ t.Error("Encoding", name, "failed\ngot ", packet, "\nwant", expected)
|
|
|
+ }
|
|
|
+ // Decoder request
|
|
|
+ decoded, err := decodeRequest(bytes.NewReader(packet))
|
|
|
+ if err != nil {
|
|
|
+ t.Error("Failed to decode request", err)
|
|
|
+ } else if decoded.correlationID != 123 || decoded.clientID != "foo" {
|
|
|
+ t.Errorf("Decoded header is not valid: %v", decoded)
|
|
|
+ } else if !reflect.DeepEqual(rb, decoded.body) {
|
|
|
+ t.Errorf("Decoded request does not match the encoded one\n encoded: %v\n decoded: %v", rb, decoded)
|
|
|
+ }
|
|
|
+}
|