request_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package protocol
  2. import enc "sarama/encoding"
  3. import (
  4. "bytes"
  5. "testing"
  6. )
  7. var (
  8. requestSimple = []byte{
  9. 0x00, 0x00, 0x00, 0x17, // msglen
  10. 0x06, 0x66,
  11. 0x00, 0xD2,
  12. 0x00, 0x00, 0x12, 0x34,
  13. 0x00, 0x08, 'm', 'y', 'C', 'l', 'i', 'e', 'n', 't',
  14. 0x00, 0x03, 'a', 'b', 'c'}
  15. )
  16. type testRequestBody struct {
  17. }
  18. func (s *testRequestBody) key() int16 {
  19. return 0x666
  20. }
  21. func (s *testRequestBody) version() int16 {
  22. return 0xD2
  23. }
  24. func (s *testRequestBody) Encode(pe enc.PacketEncoder) error {
  25. return pe.PutString("abc")
  26. }
  27. func TestRequest(t *testing.T) {
  28. request := request{correlation_id: 0x1234, id: "myClient", body: new(testRequestBody)}
  29. testEncodable(t, "simple", &request, requestSimple)
  30. }
  31. // not specific to request tests, just helper functions for testing structures that
  32. // implement the encoder or decoder interfaces that needed somewhere to live
  33. func testEncodable(t *testing.T, name string, in enc.Encoder, expect []byte) {
  34. packet, err := enc.Encode(in)
  35. if err != nil {
  36. t.Error(err)
  37. } else if !bytes.Equal(packet, expect) {
  38. t.Error("Encoding", name, "failed\ngot ", packet, "\nwant", expect)
  39. }
  40. }
  41. func testDecodable(t *testing.T, name string, out enc.Decoder, in []byte) {
  42. err := enc.Decode(in, out)
  43. if err != nil {
  44. t.Error("Decoding", name, "failed:", err)
  45. }
  46. }