2021-06-23 10:51:53 +00:00
|
|
|
package downloader
|
|
|
|
|
|
|
|
import (
|
2021-09-06 12:59:09 +00:00
|
|
|
"bytes"
|
2021-06-23 10:51:53 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-09-06 12:59:09 +00:00
|
|
|
"strings"
|
2021-06-23 10:51:53 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReader(t *testing.T) {
|
|
|
|
data := []byte("test string")
|
|
|
|
err := fmt.Errorf("something wrong")
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
err error
|
|
|
|
buff []byte
|
|
|
|
}{
|
|
|
|
{err: nil, buff: make([]byte, len(data)+1)},
|
|
|
|
{err: nil, buff: make([]byte, len(data))},
|
|
|
|
{err: nil, buff: make([]byte, len(data)-1)},
|
|
|
|
{err: err, buff: make([]byte, len(data)+1)},
|
|
|
|
{err: err, buff: make([]byte, len(data))},
|
|
|
|
{err: err, buff: make([]byte, len(data)-1)},
|
|
|
|
} {
|
|
|
|
var res []byte
|
|
|
|
var err error
|
|
|
|
var n int
|
|
|
|
|
|
|
|
r := newReader(data, tc.err)
|
|
|
|
for err == nil {
|
|
|
|
n, err = r.Read(tc.buff)
|
|
|
|
res = append(res, tc.buff[:n]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tc.err == nil {
|
|
|
|
require.Equal(t, io.EOF, err)
|
|
|
|
} else {
|
|
|
|
require.Equal(t, tc.err, err)
|
|
|
|
}
|
|
|
|
require.Equal(t, data, res)
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 12:59:09 +00:00
|
|
|
|
|
|
|
func TestDetector(t *testing.T) {
|
|
|
|
txtContentType := "text/plain; charset=utf-8"
|
|
|
|
sb := strings.Builder{}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
sb.WriteString("Some txt content. Content-Type must be detected properly by detector.")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
Name string
|
|
|
|
ContentType string
|
|
|
|
Expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "less than 512b",
|
|
|
|
ContentType: txtContentType,
|
|
|
|
Expected: sb.String()[:256],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "more than 512b",
|
|
|
|
ContentType: txtContentType,
|
|
|
|
Expected: sb.String(),
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
detector := newDetector()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
detector.SetReader(bytes.NewBufferString(tc.Expected))
|
|
|
|
detector.Detect()
|
|
|
|
}()
|
|
|
|
|
|
|
|
detector.Wait()
|
|
|
|
require.Equal(t, tc.ContentType, detector.contentType)
|
|
|
|
|
|
|
|
data, err := io.ReadAll(detector.MultiReader())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.Expected, string(data))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|