-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmime_test.go
105 lines (95 loc) · 2.65 KB
/
mime_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
104
105
package xun
import (
"mime"
"testing"
"github.com/stretchr/testify/require"
)
func TestMime(t *testing.T) {
mime.AddExtensionType(".xun1", "xun/mime; charset=utf-16") // nolint: errcheck
mime.AddExtensionType(".xun2", "xun/mime") // nolint: errcheck
t.Run("mime_charset", func(t *testing.T) {
mime, charset := GetMimeType("test.xun1", []byte{})
require.Equal(t, "xun/mime", mime.String())
require.True(t, mime.Match(MimeType{Type: "xun", SubType: "mime"}))
require.Equal(t, "; charset=utf-16", charset)
})
t.Run("mime_no_charset", func(t *testing.T) {
mime, charset := GetMimeType("test.xun2", []byte{})
require.Equal(t, "xun/mime", mime.String())
require.True(t, mime.Match(MimeType{Type: "xun", SubType: "mime"}))
require.Equal(t, "; charset=utf-8", charset)
})
t.Run("http_mime", func(t *testing.T) {
mime, charset := GetMimeType("test.xun3", []byte(`<?xml version="1.0" encoding="UTF-8"?>`))
require.Equal(t, "text/xml", mime.String())
require.True(t, mime.Match(MimeType{Type: "text", SubType: "xml"}))
require.Equal(t, "; charset=utf-8", charset)
})
tests := []struct {
name string
m1 MimeType
m2 MimeType
want bool
}{
{
name: "full_match",
m1: MimeType{Type: "text", SubType: "html"},
m2: MimeType{Type: "text", SubType: "html"},
want: true,
},
{
name: "wildcard_sub_match",
m1: MimeType{Type: "text", SubType: "*"},
m2: MimeType{Type: "text", SubType: "html"},
want: true,
},
{
name: "wildcard_sub_match_2",
m1: MimeType{Type: "text", SubType: "html"},
m2: MimeType{Type: "text", SubType: "*"},
want: true,
},
{
name: "wildcard_type_match",
m1: MimeType{Type: "*", SubType: "html"},
m2: MimeType{Type: "text", SubType: "html"},
want: true,
},
{
name: "wildcard_type_match_2",
m1: MimeType{Type: "text", SubType: "html"},
m2: MimeType{Type: "*", SubType: "html"},
want: true,
},
{
name: "wildcard_match_2",
m1: MimeType{Type: "*", SubType: "*"},
m2: MimeType{Type: "text", SubType: "html"},
want: true,
},
{
name: "wildcard_match_2",
m1: MimeType{Type: "text", SubType: "html"},
m2: MimeType{Type: "*", SubType: "*"},
want: true,
},
{
name: "no_match",
m1: MimeType{Type: "text", SubType: "html"},
m2: MimeType{Type: "text", SubType: "json"},
want: false,
},
{
name: "no_match",
m1: MimeType{Type: "text", SubType: "html"},
m2: MimeType{Type: "application", SubType: "json"},
want: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
matched := test.m1.Match(test.m2)
require.Equal(t, test.want, matched)
})
}
}