2021-10-25 04:49:49 -05:00
|
|
|
package httpclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-10 08:37:51 -05:00
|
|
|
"io"
|
2021-10-25 04:49:49 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCountBytesReader(t *testing.T) {
|
|
|
|
tcs := []struct {
|
|
|
|
body string
|
|
|
|
expectedBytesCount int64
|
|
|
|
}{
|
|
|
|
{body: "d", expectedBytesCount: 1},
|
|
|
|
{body: "dummy", expectedBytesCount: 5},
|
|
|
|
}
|
|
|
|
|
|
|
|
for index, tc := range tcs {
|
|
|
|
t.Run(fmt.Sprintf("Test CountBytesReader %d", index), func(t *testing.T) {
|
2022-08-10 08:37:51 -05:00
|
|
|
body := io.NopCloser(strings.NewReader(tc.body))
|
2021-10-25 04:49:49 -05:00
|
|
|
var actualBytesRead int64
|
|
|
|
|
|
|
|
readCloser := CountBytesReader(body, func(bytesRead int64) {
|
|
|
|
actualBytesRead = bytesRead
|
|
|
|
})
|
|
|
|
|
2022-08-10 08:37:51 -05:00
|
|
|
bodyBytes, err := io.ReadAll(readCloser)
|
2021-10-25 04:49:49 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
err = readCloser.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expectedBytesCount, actualBytesRead)
|
|
|
|
require.Equal(t, string(bodyBytes), tc.body)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|