ntfy/util/peek_test.go

56 lines
1.3 KiB
Go
Raw Permalink Normal View History

2022-01-02 23:56:12 +01:00
package util
import (
"github.com/stretchr/testify/require"
"io"
"strings"
"testing"
)
func TestPeak_LimitReached(t *testing.T) {
underlying := io.NopCloser(strings.NewReader("1234567890"))
2022-04-03 18:39:52 +02:00
peaked, err := Peek(underlying, 5)
2022-01-02 23:56:12 +01:00
if err != nil {
t.Fatal(err)
}
2022-04-03 18:39:52 +02:00
require.Equal(t, []byte("12345"), peaked.PeekedBytes)
2022-01-02 23:56:12 +01:00
require.Equal(t, true, peaked.LimitReached)
all, err := io.ReadAll(peaked)
if err != nil {
t.Fatal(err)
}
require.Equal(t, []byte("1234567890"), all)
2022-04-03 18:39:52 +02:00
require.Equal(t, []byte("12345"), peaked.PeekedBytes)
2022-01-02 23:56:12 +01:00
require.Equal(t, true, peaked.LimitReached)
}
func TestPeak_LimitNotReached(t *testing.T) {
underlying := io.NopCloser(strings.NewReader("1234567890"))
2022-04-03 18:39:52 +02:00
peaked, err := Peek(underlying, 15)
2022-01-02 23:56:12 +01:00
if err != nil {
t.Fatal(err)
}
all, err := io.ReadAll(peaked)
if err != nil {
t.Fatal(err)
}
require.Equal(t, []byte("1234567890"), all)
2022-04-03 18:39:52 +02:00
require.Equal(t, []byte("1234567890"), peaked.PeekedBytes)
2022-01-02 23:56:12 +01:00
require.Equal(t, false, peaked.LimitReached)
}
func TestPeak_Nil(t *testing.T) {
2022-04-03 18:39:52 +02:00
peaked, err := Peek(nil, 15)
2022-01-02 23:56:12 +01:00
if err != nil {
t.Fatal(err)
}
all, err := io.ReadAll(peaked)
if err != nil {
t.Fatal(err)
}
require.Equal(t, []byte(""), all)
2022-04-03 18:39:52 +02:00
require.Equal(t, []byte(""), peaked.PeekedBytes)
2022-01-02 23:56:12 +01:00
require.Equal(t, false, peaked.LimitReached)
}