forked from h2non/imaginary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_test.go
103 lines (93 loc) · 2.4 KB
/
type_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
98
99
100
101
102
103
package main
import (
"testing"
"github.com/h2non/bimg"
)
func TestExtractImageTypeFromMime(t *testing.T) {
files := []struct {
mime string
expected string
}{
{"image/jpeg", "jpeg"},
{"/png", "png"},
{"png", ""},
{"multipart/form-data; encoding=utf-8", "form-data"},
{"", ""},
}
for _, file := range files {
if ExtractImageTypeFromMime(file.mime) != file.expected {
t.Fatalf("Invalid mime type: %s != %s", file.mime, file.expected)
}
}
}
func TestIsImageTypeSupported(t *testing.T) {
files := []struct {
name string
expected bool
}{
{"image/jpeg", true},
{"image/png", true},
{"image/webp", true},
{"IMAGE/JPEG", true},
{"png", false},
{"multipart/form-data; encoding=utf-8", false},
{"application/json", false},
{"image/gif", bimg.IsImageTypeSupportedByVips(bimg.GIF).Load},
{"image/svg+xml", bimg.IsImageTypeSupportedByVips(bimg.SVG).Load},
{"image/svg", bimg.IsImageTypeSupportedByVips(bimg.SVG).Load},
{"image/tiff", bimg.IsImageTypeSupportedByVips(bimg.TIFF).Load},
{"application/pdf", bimg.IsImageTypeSupportedByVips(bimg.PDF).Load},
{"text/plain", false},
{"blablabla", false},
{"", false},
}
for _, file := range files {
if IsImageMimeTypeSupported(file.name) != file.expected {
t.Fatalf("Invalid type: %s != %t", file.name, file.expected)
}
}
}
func TestImageType(t *testing.T) {
files := []struct {
name string
expected bimg.ImageType
}{
{"jpeg", bimg.JPEG},
{"png", bimg.PNG},
{"webp", bimg.WEBP},
{"tiff", bimg.TIFF},
{"gif", bimg.GIF},
{"svg", bimg.SVG},
{"pdf", bimg.PDF},
{"multipart/form-data; encoding=utf-8", bimg.UNKNOWN},
{"json", bimg.UNKNOWN},
{"text", bimg.UNKNOWN},
{"blablabla", bimg.UNKNOWN},
{"", bimg.UNKNOWN},
}
for _, file := range files {
if ImageType(file.name) != file.expected {
t.Fatalf("Invalid type: %s != %s", file.name, bimg.ImageTypes[file.expected])
}
}
}
func TestGetImageMimeType(t *testing.T) {
files := []struct {
name bimg.ImageType
expected string
}{
{bimg.JPEG, "image/jpeg"},
{bimg.PNG, "image/png"},
{bimg.WEBP, "image/webp"},
{bimg.TIFF, "image/tiff"},
{bimg.GIF, "image/gif"},
{bimg.PDF, "application/pdf"},
{bimg.SVG, "image/svg+xml"},
{bimg.UNKNOWN, "image/jpeg"},
}
for _, file := range files {
if GetImageMimeType(file.name) != file.expected {
t.Fatalf("Invalid type: %s != %s", bimg.ImageTypes[file.name], file.expected)
}
}
}