2020-11-23 11:14:08 +00:00
|
|
|
package aghio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-12 10:27:44 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-11-23 11:14:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLimitReadCloser(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2021-03-25 15:03:29 +00:00
|
|
|
want error
|
2020-11-23 11:14:08 +00:00
|
|
|
name string
|
|
|
|
n int64
|
|
|
|
}{{
|
2021-03-25 15:03:29 +00:00
|
|
|
want: nil,
|
2020-11-23 11:14:08 +00:00
|
|
|
name: "positive",
|
|
|
|
n: 1,
|
|
|
|
}, {
|
2021-03-25 15:03:29 +00:00
|
|
|
want: nil,
|
2020-11-23 11:14:08 +00:00
|
|
|
name: "zero",
|
|
|
|
n: 0,
|
|
|
|
}, {
|
2021-03-25 15:03:29 +00:00
|
|
|
want: fmt.Errorf("aghio: invalid n in LimitReadCloser: -1"),
|
2020-11-23 11:14:08 +00:00
|
|
|
name: "negative",
|
|
|
|
n: -1,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
_, err := LimitReadCloser(nil, tc.n)
|
|
|
|
assert.Equal(t, tc.want, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLimitedReadCloser_Read(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2021-03-25 15:03:29 +00:00
|
|
|
err error
|
2020-11-23 11:14:08 +00:00
|
|
|
name string
|
|
|
|
rStr string
|
2021-03-25 15:03:29 +00:00
|
|
|
limit int64
|
2020-11-23 11:14:08 +00:00
|
|
|
want int
|
|
|
|
}{{
|
2021-03-25 15:03:29 +00:00
|
|
|
err: nil,
|
2020-11-23 11:14:08 +00:00
|
|
|
name: "perfectly_match",
|
|
|
|
rStr: "abc",
|
2021-03-25 15:03:29 +00:00
|
|
|
limit: 3,
|
2020-11-23 11:14:08 +00:00
|
|
|
want: 3,
|
|
|
|
}, {
|
2021-03-25 15:03:29 +00:00
|
|
|
err: io.EOF,
|
2020-11-23 11:14:08 +00:00
|
|
|
name: "eof",
|
|
|
|
rStr: "",
|
2021-03-25 15:03:29 +00:00
|
|
|
limit: 3,
|
2020-11-23 11:14:08 +00:00
|
|
|
want: 0,
|
|
|
|
}, {
|
|
|
|
err: &LimitReachedError{
|
|
|
|
Limit: 0,
|
|
|
|
},
|
2021-03-25 15:03:29 +00:00
|
|
|
name: "limit_reached",
|
|
|
|
rStr: "abc",
|
|
|
|
limit: 0,
|
|
|
|
want: 0,
|
2020-11-23 11:14:08 +00:00
|
|
|
}, {
|
2021-03-25 15:03:29 +00:00
|
|
|
err: nil,
|
2020-11-23 11:14:08 +00:00
|
|
|
name: "truncated",
|
|
|
|
rStr: "abc",
|
2021-03-25 15:03:29 +00:00
|
|
|
limit: 2,
|
2020-11-23 11:14:08 +00:00
|
|
|
want: 2,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-05-21 11:55:42 +00:00
|
|
|
readCloser := io.NopCloser(strings.NewReader(tc.rStr))
|
2020-11-23 11:14:08 +00:00
|
|
|
buf := make([]byte, tc.limit+1)
|
|
|
|
|
|
|
|
lreader, err := LimitReadCloser(readCloser, tc.limit)
|
2021-03-25 15:03:29 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-23 11:14:08 +00:00
|
|
|
|
|
|
|
n, err := lreader.Read(buf)
|
2021-02-12 10:27:44 +00:00
|
|
|
require.Equal(t, tc.err, err)
|
|
|
|
assert.Equal(t, tc.want, n)
|
2020-11-23 11:14:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLimitedReadCloser_LimitReachedError(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2021-03-25 15:03:29 +00:00
|
|
|
err error
|
2020-11-23 11:14:08 +00:00
|
|
|
name string
|
|
|
|
want string
|
|
|
|
}{{
|
|
|
|
err: &LimitReachedError{
|
|
|
|
Limit: 0,
|
|
|
|
},
|
2021-03-25 15:03:29 +00:00
|
|
|
name: "simplest",
|
|
|
|
want: "attempted to read more than 0 bytes",
|
2020-11-23 11:14:08 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
assert.Equal(t, tc.want, tc.err.Error())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|