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