Debian Codegraph @

filetype_test.go

raw symbols SCIP
1 package filetype
2
3 import (
4 "testing"
5 "time"
6
7 "github.com/h2non/filetype/types"
8 )
9
10 func TestConcurrent(t *testing.T) {
11 go func() {
12 for i := 0; i < 10000; i++ {
13 types.NewType("xml", "text/xml")
14 }
15 }()
16 go func() {
17 for i := 0; i < 10000; i++ {
18 types.NewType("xml", "text/xml")
19 }
20 }()
21
22 time.Sleep(time.Second * 2)
23 }
24
25 func TestIs(t *testing.T) {
26 cases := []struct {
27 buf []byte
28 ext string
29 match bool
30 }{
31 {[]byte{0xFF, 0xD8, 0xFF}, "jpg", true},
32 {[]byte{0xFF, 0xD8, 0x00}, "jpg", false},
33 {[]byte{0x89, 0x50, 0x4E, 0x47}, "png", true},
34 }
35
36 for _, test := range cases {
37 if Is(test.buf, test.ext) != test.match {
38 t.Fatalf("Invalid match: %s", test.ext)
39 }
40 }
41
42 }
43
44 func TestIsType(t *testing.T) {
45 cases := []struct {
46 buf []byte
47 kind types.Type
48 match bool
49 }{
50 {[]byte{0xFF, 0xD8, 0xFF}, types.Get("jpg"), true},
51 {[]byte{0xFF, 0xD8, 0x00}, types.Get("jpg"), false},
52 {[]byte{0x89, 0x50, 0x4E, 0x47}, types.Get("png"), true},
53 }
54
55 for _, test := range cases {
56 if IsType(test.buf, test.kind) != test.match {
57 t.Fatalf("Invalid match: %s", test.kind.Extension)
58 }
59 }
60 }
61
62 func TestIsMIME(t *testing.T) {
63 cases := []struct {
64 buf []byte
65 mime string
66 match bool
67 }{
68 {[]byte{0xFF, 0xD8, 0xFF}, "image/jpeg", true},
69 {[]byte{0xFF, 0xD8, 0x00}, "image/jpeg", false},
70 {[]byte{0x89, 0x50, 0x4E, 0x47}, "image/png", true},
71 }
72
73 for _, test := range cases {
74 if IsMIME(test.buf, test.mime) != test.match {
75 t.Fatalf("Invalid match: %s", test.mime)
76 }
77 }
78 }
79
80 func TestIsSupported(t *testing.T) {
81 cases := []struct {
82 ext string
83 match bool
84 }{
85 {"jpg", true},
86 {"jpeg", false},
87 {"abc", false},
88 {"png", true},
89 {"mp4", true},
90 {"", false},
91 }
92
93 for _, test := range cases {
94 if IsSupported(test.ext) != test.match {
95 t.Fatalf("Invalid match: %s", test.ext)
96 }
97 }
98 }
99
100 func TestIsMIMESupported(t *testing.T) {
101 cases := []struct {
102 mime string
103 match bool
104 }{
105 {"image/jpeg", true},
106 {"foo/bar", false},
107 {"image/png", true},
108 {"video/mpeg", true},
109 }
110
111 for _, test := range cases {
112 if IsMIMESupported(test.mime) != test.match {
113 t.Fatalf("Invalid match: %s", test.mime)
114 }
115 }
116 }
117
118 func TestAddType(t *testing.T) {
119 AddType("foo", "foo/foo")
120
121 if !IsSupported("foo") {
122 t.Fatalf("Not supported extension")
123 }
124
125 if !IsMIMESupported("foo/foo") {
126 t.Fatalf("Not supported MIME type")
127 }
128 }
129
130 func TestGetType(t *testing.T) {
131 jpg := GetType("jpg")
132 if jpg == types.Unknown {
133 t.Fatalf("Type should be supported")
134 }
135
136 invalid := GetType("invalid")
137 if invalid != Unknown {
138 t.Fatalf("Type should not be supported")
139 }
140 }