forked from Layr-Labs/optimism-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
43 lines (36 loc) · 1.02 KB
/
reader_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package proxyd
import (
"github.com/stretchr/testify/require"
"io"
"strings"
"testing"
)
func TestLimitReader(t *testing.T) {
data := "hellohellohellohello"
r := LimitReader(strings.NewReader(data), 3)
buf := make([]byte, 3)
// Buffer reads OK
n, err := r.Read(buf)
require.NoError(t, err)
require.Equal(t, 3, n)
// Buffer is over limit
n, err = r.Read(buf)
require.Equal(t, ErrLimitReaderOverLimit, err)
require.Equal(t, 0, n)
// Buffer on initial read is over size
buf = make([]byte, 16)
r = LimitReader(strings.NewReader(data), 3)
n, err = r.Read(buf)
require.NoError(t, err)
require.Equal(t, 3, n)
// test with read all where the limit is less than the data
r = LimitReader(strings.NewReader(data), 3)
out, err := io.ReadAll(r)
require.Equal(t, ErrLimitReaderOverLimit, err)
require.Equal(t, "hel", string(out))
// test with read all where the limit is more than the data
r = LimitReader(strings.NewReader(data), 21)
out, err = io.ReadAll(r)
require.NoError(t, err)
require.Equal(t, data, string(out))
}