2020-10-08 13:59:03 +00:00
|
|
|
package images
|
|
|
|
|
|
|
|
import (
|
2020-10-26 15:16:41 +00:00
|
|
|
"errors"
|
2020-10-08 13:59:03 +00:00
|
|
|
"image"
|
2022-08-09 15:04:28 +00:00
|
|
|
"net/http"
|
2022-08-09 22:54:44 +00:00
|
|
|
"net/http/httptest"
|
2020-10-08 13:59:03 +00:00
|
|
|
"testing"
|
2020-10-08 15:30:18 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
path = "../_assets/tests/"
|
2020-10-08 13:59:03 +00:00
|
|
|
)
|
|
|
|
|
2020-10-09 11:17:04 +00:00
|
|
|
func TestDecode(t *testing.T) {
|
2020-10-08 13:59:03 +00:00
|
|
|
|
2020-10-08 15:30:18 +00:00
|
|
|
cs := []struct {
|
2020-10-08 13:59:03 +00:00
|
|
|
Filepath string
|
2020-10-08 15:30:18 +00:00
|
|
|
Error bool
|
|
|
|
Nil bool
|
|
|
|
Bounds image.Rectangle
|
2020-10-08 13:59:03 +00:00
|
|
|
}{
|
|
|
|
{
|
2020-10-08 15:30:18 +00:00
|
|
|
"elephant.jpg",
|
2020-10-08 13:59:03 +00:00
|
|
|
false,
|
|
|
|
false,
|
|
|
|
image.Rectangle{
|
|
|
|
Min: image.Point{X: 0, Y: 0},
|
|
|
|
Max: image.Point{X: 80, Y: 80},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-10-08 15:30:18 +00:00
|
|
|
"status.png",
|
2020-10-08 13:59:03 +00:00
|
|
|
false,
|
|
|
|
false,
|
|
|
|
image.Rectangle{
|
|
|
|
Min: image.Point{X: 0, Y: 0},
|
|
|
|
Max: image.Point{X: 256, Y: 256},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-10-08 15:30:18 +00:00
|
|
|
"spin.gif",
|
2020-10-08 13:59:03 +00:00
|
|
|
false,
|
|
|
|
false,
|
|
|
|
image.Rectangle{
|
|
|
|
Min: image.Point{X: 0, Y: 0},
|
|
|
|
Max: image.Point{X: 256, Y: 256},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-10-08 15:30:18 +00:00
|
|
|
"rose.webp",
|
2020-10-08 13:59:03 +00:00
|
|
|
false,
|
|
|
|
false,
|
|
|
|
image.Rectangle{
|
|
|
|
Min: image.Point{X: 0, Y: 0},
|
|
|
|
Max: image.Point{X: 400, Y: 301},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-10-08 15:30:18 +00:00
|
|
|
"test.aac",
|
2020-10-08 13:59:03 +00:00
|
|
|
true,
|
|
|
|
true,
|
|
|
|
image.Rectangle{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-10-08 15:30:18 +00:00
|
|
|
for _, c := range cs {
|
2020-10-09 11:17:04 +00:00
|
|
|
img, err := Decode(path + c.Filepath)
|
2020-10-08 13:59:03 +00:00
|
|
|
|
2020-10-08 15:30:18 +00:00
|
|
|
if c.Error {
|
2020-10-08 13:59:03 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-10-08 15:30:18 +00:00
|
|
|
if c.Nil {
|
2020-10-08 13:59:03 +00:00
|
|
|
require.Nil(t, img)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
require.NotNil(t, img)
|
|
|
|
}
|
|
|
|
|
2020-10-08 15:30:18 +00:00
|
|
|
require.Exactly(t, c.Bounds, img.Bounds())
|
|
|
|
}
|
|
|
|
}
|
2020-10-26 15:16:41 +00:00
|
|
|
|
2021-09-21 09:00:44 +00:00
|
|
|
func TestDecodeFromURL(t *testing.T) {
|
2022-08-09 22:54:44 +00:00
|
|
|
s := httptest.NewServer(http.FileServer(http.Dir(path)))
|
|
|
|
defer s.Close()
|
2022-08-09 15:04:28 +00:00
|
|
|
|
2021-09-21 09:00:44 +00:00
|
|
|
cs := []struct {
|
|
|
|
Filepath string
|
|
|
|
Nil bool
|
|
|
|
Bounds image.Rectangle
|
|
|
|
}{
|
|
|
|
{
|
2022-08-09 22:54:44 +00:00
|
|
|
s.URL + "/2x1.png",
|
2021-09-21 09:00:44 +00:00
|
|
|
false,
|
|
|
|
image.Rectangle{
|
|
|
|
Min: image.Point{X: 0, Y: 0},
|
|
|
|
Max: image.Point{X: 2, Y: 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-08-09 22:54:44 +00:00
|
|
|
s.URL + "/1.jpg",
|
2021-09-21 09:00:44 +00:00
|
|
|
false,
|
|
|
|
image.Rectangle{
|
|
|
|
Min: image.Point{X: 0, Y: 0},
|
|
|
|
Max: image.Point{X: 1, Y: 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-08-09 22:54:44 +00:00
|
|
|
s.URL + "/1.gif",
|
2021-09-21 09:00:44 +00:00
|
|
|
false,
|
|
|
|
image.Rectangle{
|
|
|
|
Min: image.Point{X: 0, Y: 0},
|
|
|
|
Max: image.Point{X: 1, Y: 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-01-18 18:54:54 +00:00
|
|
|
s.URL + "/1.webp",
|
2021-09-21 09:00:44 +00:00
|
|
|
false,
|
|
|
|
image.Rectangle{
|
|
|
|
Min: image.Point{X: 0, Y: 0},
|
|
|
|
Max: image.Point{X: 1, Y: 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-01-18 18:54:54 +00:00
|
|
|
s.URL + "/1.webp",
|
2021-09-21 09:00:44 +00:00
|
|
|
true,
|
|
|
|
image.Rectangle{
|
|
|
|
Min: image.Point{X: 0, Y: 0},
|
|
|
|
Max: image.Point{X: 10, Y: 10},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cs {
|
|
|
|
img, err := DecodeFromURL(c.Filepath)
|
|
|
|
|
|
|
|
if c.Nil {
|
|
|
|
require.Nil(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Exactly(t, c.Bounds, img.Bounds())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 08:10:00 +00:00
|
|
|
func TestDecodeFromURL_WithErrors(t *testing.T) {
|
|
|
|
s := httptest.NewServer(http.FileServer(http.Dir(path)))
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
_, err := DecodeFromURL("https://doesnt-exist.com")
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2020-10-26 15:16:41 +00:00
|
|
|
func TestGetType(t *testing.T) {
|
2020-10-27 14:42:42 +00:00
|
|
|
cs := []struct {
|
|
|
|
Buf []byte
|
2020-10-26 15:16:41 +00:00
|
|
|
Value ImageType
|
|
|
|
}{
|
|
|
|
{testJpegBytes, JPEG},
|
|
|
|
{testPngBytes, PNG},
|
|
|
|
{testGifBytes, GIF},
|
|
|
|
{testWebpBytes, WEBP},
|
|
|
|
{testAacBytes, UNKNOWN},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cs {
|
|
|
|
require.Exactly(t, c.Value, GetType(c.Buf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetMimeType(t *testing.T) {
|
2020-10-27 14:42:42 +00:00
|
|
|
cs := []struct {
|
|
|
|
Buf []byte
|
2020-10-26 15:16:41 +00:00
|
|
|
Value string
|
|
|
|
Error error
|
|
|
|
}{
|
|
|
|
{testJpegBytes, "jpeg", nil},
|
|
|
|
{testPngBytes, "png", nil},
|
|
|
|
{testGifBytes, "gif", nil},
|
|
|
|
{testWebpBytes, "webp", nil},
|
|
|
|
{testAacBytes, "", errors.New("image format not supported")},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cs {
|
|
|
|
mt, err := GetMimeType(c.Buf)
|
|
|
|
if c.Error == nil {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, c.Error.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Exactly(t, c.Value, mt)
|
|
|
|
}
|
|
|
|
}
|