request_test.go 1.2 KB

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