-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimage_test.go
97 lines (91 loc) · 2.11 KB
/
image_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package utils_test
import (
"bytes"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/dollarsignteam/go-utils"
)
func TestIsImageContentType(t *testing.T) {
tests := []struct {
name string
fileType string
expected bool
}{
{"valid image", "image/jpeg", true},
{"invalid image", "text/plain", false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fh := &multipart.FileHeader{
Header: map[string][]string{
"Content-Type": {test.fileType},
},
}
result := utils.Image.IsImageContentType(fh)
assert.Equal(t, test.expected, result)
})
}
}
func TestIsImage(t *testing.T) {
tests := []struct {
name string
filename string
content []byte
expected bool
}{
{
name: "valid jpg",
filename: "test.jpg",
content: []byte("\xFF\xD8\xFF"),
expected: true,
},
{
name: "valid png",
filename: "test.png",
content: []byte("\x89PNG\x0D\x0A\x1A\x0A"),
expected: true,
},
{
name: "invalid file header",
filename: "test.txt",
content: []byte{0x00, 0x01, 0x02, 0x03},
expected: false,
},
{
name: "empty file header",
filename: "test.txt",
content: []byte{},
expected: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile("file", test.filename)
part.Write(test.content)
writer.Close()
req := httptest.NewRequest(http.MethodPost, "/", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
c := echo.New().NewContext(req, nil)
fh, err := c.FormFile("file")
if assert.NoError(t, err) {
result := utils.Image.IsImage(fh)
assert.Equal(t, test.expected, result)
}
})
}
}
func TestIsImage_InvalidFile(t *testing.T) {
fh := &multipart.FileHeader{
Filename: "invalid-file.txt",
}
result := utils.Image.IsImage(fh)
assert.False(t, result)
assert.False(t, utils.Image.IsImage(nil))
assert.False(t, utils.Image.IsImageContentType(nil))
}