ntfy/util/peek_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

56 lines
1.3 KiB
Go
Raw Normal View History

2022-01-03 01:56:12 +03: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 19:39:52 +03:00
peaked, err := Peek(underlying, 5)
2022-01-03 01:56:12 +03:00
if err != nil {
t.Fatal(err)
}
2022-04-03 19:39:52 +03:00
require.Equal(t, []byte("12345"), peaked.PeekedBytes)
2022-01-03 01:56:12 +03: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 19:39:52 +03:00
require.Equal(t, []byte("12345"), peaked.PeekedBytes)
2022-01-03 01:56:12 +03:00
require.Equal(t, true, peaked.LimitReached)
}
func TestPeak_LimitNotReached(t *testing.T) {
underlying := io.NopCloser(strings.NewReader("1234567890"))
2022-04-03 19:39:52 +03:00
peaked, err := Peek(underlying, 15)
2022-01-03 01:56:12 +03: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 19:39:52 +03:00
require.Equal(t, []byte("1234567890"), peaked.PeekedBytes)
2022-01-03 01:56:12 +03:00
require.Equal(t, false, peaked.LimitReached)
}
func TestPeak_Nil(t *testing.T) {
2022-04-03 19:39:52 +03:00
peaked, err := Peek(nil, 15)
2022-01-03 01:56:12 +03: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 19:39:52 +03:00
require.Equal(t, []byte(""), peaked.PeekedBytes)
2022-01-03 01:56:12 +03:00
require.Equal(t, false, peaked.LimitReached)
}