peek_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package util
  2. import (
  3. "github.com/stretchr/testify/require"
  4. "io"
  5. "strings"
  6. "testing"
  7. )
  8. func TestPeak_LimitReached(t *testing.T) {
  9. underlying := io.NopCloser(strings.NewReader("1234567890"))
  10. peaked, err := Peek(underlying, 5)
  11. if err != nil {
  12. t.Fatal(err)
  13. }
  14. require.Equal(t, []byte("12345"), peaked.PeekedBytes)
  15. require.Equal(t, true, peaked.LimitReached)
  16. all, err := io.ReadAll(peaked)
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. require.Equal(t, []byte("1234567890"), all)
  21. require.Equal(t, []byte("12345"), peaked.PeekedBytes)
  22. require.Equal(t, true, peaked.LimitReached)
  23. }
  24. func TestPeak_LimitNotReached(t *testing.T) {
  25. underlying := io.NopCloser(strings.NewReader("1234567890"))
  26. peaked, err := Peek(underlying, 15)
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. all, err := io.ReadAll(peaked)
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. require.Equal(t, []byte("1234567890"), all)
  35. require.Equal(t, []byte("1234567890"), peaked.PeekedBytes)
  36. require.Equal(t, false, peaked.LimitReached)
  37. }
  38. func TestPeak_Nil(t *testing.T) {
  39. peaked, err := Peek(nil, 15)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. all, err := io.ReadAll(peaked)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. require.Equal(t, []byte(""), all)
  48. require.Equal(t, []byte(""), peaked.PeekedBytes)
  49. require.Equal(t, false, peaked.LimitReached)
  50. }