fetch_request_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package protocol
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. var (
  7. fetchRequestNoBlocks = []byte{
  8. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9. 0x00, 0x00, 0x00, 0x00}
  10. fetchRequestWithProperties = []byte{
  11. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0xEF,
  12. 0x00, 0x00, 0x00, 0x00}
  13. fetchRequestOneBlock = []byte{
  14. 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15. 0x00, 0x00, 0x00, 0x01,
  16. 0x00, 0x05, 't', 'o', 'p', 'i', 'c',
  17. 0x00, 0x00, 0x00, 0x01,
  18. 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x56}
  19. )
  20. func testEncodable(t *testing.T, name string, in encoder, result []byte) {
  21. packet, err := encode(in)
  22. if err != nil {
  23. t.Error(err)
  24. }
  25. if !bytes.Equal(packet, result) {
  26. t.Error("Encoding", name, "failed\ngot ", packet, "\nwant", result)
  27. }
  28. }
  29. func TestFetchRequestEncoding(t *testing.T) {
  30. request := new(FetchRequest)
  31. testEncodable(t, "no blocks", request, fetchRequestNoBlocks)
  32. request.MaxWaitTime = 0x20
  33. request.MinBytes = 0xEF
  34. testEncodable(t, "with properties", request, fetchRequestWithProperties)
  35. request.MaxWaitTime = 0
  36. request.MinBytes = 0
  37. request.AddBlock("topic", 0x12, 0x34, 0x56)
  38. testEncodable(t, "one block", request, fetchRequestOneBlock)
  39. }