2023-03-24 09:25:49 +00:00
|
|
|
//go:build !integration
|
|
|
|
|
2023-08-31 08:37:03 +00:00
|
|
|
package handler
|
2021-06-23 10:51:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2021-09-06 12:59:09 +00:00
|
|
|
"strings"
|
2021-06-23 10:51:53 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
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) {
|
2022-03-04 06:53:49 +00:00
|
|
|
contentType, data, err := readContentType(uint64(len(tc.Expected)),
|
|
|
|
func(sz uint64) (io.Reader, error) {
|
|
|
|
return strings.NewReader(tc.Expected), nil
|
|
|
|
},
|
|
|
|
)
|
2021-09-06 12:59:09 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
2022-03-04 06:53:49 +00:00
|
|
|
require.Equal(t, tc.ContentType, contentType)
|
|
|
|
require.True(t, strings.HasPrefix(tc.Expected, string(data)))
|
2021-09-06 12:59:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|