2020-06-15 23:59:33 +00:00
|
|
|
package io
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-10-30 17:37:08 +00:00
|
|
|
"errors"
|
2020-06-15 23:59:33 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLimitedReadCloser_Exceeded(t *testing.T) {
|
2020-09-28 17:28:51 +00:00
|
|
|
b := &closer{Reader: bytes.NewBufferString("howdy")}
|
|
|
|
rc := NewLimitedReadCloser(b, 3)
|
2020-06-15 23:59:33 +00:00
|
|
|
|
|
|
|
out, err := ioutil.ReadAll(rc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte("how"), out)
|
|
|
|
assert.Equal(t, ErrReadLimitExceeded, rc.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLimitedReadCloser_Happy(t *testing.T) {
|
2020-09-28 17:28:51 +00:00
|
|
|
b := &closer{Reader: bytes.NewBufferString("ho")}
|
2020-06-15 23:59:33 +00:00
|
|
|
rc := NewLimitedReadCloser(b, 2)
|
|
|
|
|
|
|
|
out, err := ioutil.ReadAll(rc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte("ho"), out)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
2020-09-28 17:28:51 +00:00
|
|
|
func TestLimitedReadCloseWithErrorAndLimitExceeded(t *testing.T) {
|
|
|
|
b := &closer{
|
|
|
|
Reader: bytes.NewBufferString("howdy"),
|
2020-10-30 17:37:08 +00:00
|
|
|
err: errors.New("some error"),
|
2020-09-28 17:28:51 +00:00
|
|
|
}
|
|
|
|
rc := NewLimitedReadCloser(b, 3)
|
|
|
|
|
|
|
|
out, err := ioutil.ReadAll(rc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte("how"), out)
|
|
|
|
// LimitExceeded error trumps the close error.
|
|
|
|
assert.Equal(t, ErrReadLimitExceeded, rc.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLimitedReadCloseWithError(t *testing.T) {
|
|
|
|
closeErr := errors.New("some error")
|
|
|
|
b := &closer{
|
|
|
|
Reader: bytes.NewBufferString("howdy"),
|
2020-10-30 17:37:08 +00:00
|
|
|
err: closeErr,
|
2020-09-28 17:28:51 +00:00
|
|
|
}
|
|
|
|
rc := NewLimitedReadCloser(b, 10)
|
|
|
|
|
|
|
|
out, err := ioutil.ReadAll(rc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte("howdy"), out)
|
|
|
|
assert.Equal(t, closeErr, rc.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultipleCloseOnlyClosesOnce(t *testing.T) {
|
|
|
|
closeErr := errors.New("some error")
|
|
|
|
b := &closer{
|
|
|
|
Reader: bytes.NewBufferString("howdy"),
|
2020-10-30 17:37:08 +00:00
|
|
|
err: closeErr,
|
2020-09-28 17:28:51 +00:00
|
|
|
}
|
|
|
|
rc := NewLimitedReadCloser(b, 10)
|
|
|
|
|
|
|
|
out, err := ioutil.ReadAll(rc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte("howdy"), out)
|
|
|
|
assert.Equal(t, closeErr, rc.Close())
|
|
|
|
assert.Equal(t, closeErr, rc.Close())
|
|
|
|
assert.Equal(t, 1, b.closeCount)
|
|
|
|
}
|
|
|
|
|
2020-06-15 23:59:33 +00:00
|
|
|
type closer struct {
|
|
|
|
io.Reader
|
2020-10-30 17:37:08 +00:00
|
|
|
err error
|
2020-09-28 17:28:51 +00:00
|
|
|
closeCount int
|
2020-06-15 23:59:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 17:28:51 +00:00
|
|
|
func (c *closer) Close() error {
|
|
|
|
c.closeCount++
|
|
|
|
return c.err
|
|
|
|
}
|