forked from gabriel-vasile/mimetype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_test.go
132 lines (119 loc) · 3.13 KB
/
tree_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package mimetype
import (
"fmt"
"mime"
"os"
"strings"
"testing"
)
// This test generates the doc file containing the table with the supported MIMEs.
func TestGenerateSupportedMimesFile(t *testing.T) {
f, err := os.OpenFile("supported_mimes.md", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
t.Fatal(err)
}
defer f.Close()
nodes := root.flatten()
header := fmt.Sprintf(`## %d Supported MIME types
This file is automatically generated when running tests. Do not edit manually.
Extension | MIME type | Aliases
--------- | --------- | -------
`, len(nodes))
if _, err := f.WriteString(header); err != nil {
t.Fatal(err)
}
for _, n := range nodes {
ext := n.extension
if ext == "" {
ext = "n/a"
}
aliases := strings.Join(n.aliases, ", ")
if aliases == "" {
aliases = "-"
}
str := fmt.Sprintf("**%s** | %s | %s\n", ext, n.mime, aliases)
if _, err := f.WriteString(str); err != nil {
t.Fatal(err)
}
}
}
// Check there are no panics for nil inputs.
func TestIndexOutOfRangePanic(t *testing.T) {
for _, n := range root.flatten() {
n.detector(nil, 0)
}
}
// MIME type equality ignores any optional MIME parameters, so, in order to not
// parse each alias when testing for equality, we must ensure they are
// registered with no parameters.
func TestMIMEFormat(t *testing.T) {
for _, n := range root.flatten() {
// All extensions must be dot prefixed so they are compatible
// with the stdlib mime package.
if n.Extension() != "" && !strings.HasPrefix(n.Extension(), ".") {
t.Fatalf("extension %s should be dot prefixed", n.Extension())
}
// All MIMEs must be correctly formatted.
_, _, err := mime.ParseMediaType(n.String())
if err != nil {
t.Fatalf("error parsing node MIME: %s", err)
}
// Aliases must have no optional MIME parameters.
for _, a := range n.aliases {
parsed, params, err := mime.ParseMediaType(a)
if err != nil {
t.Fatalf("error parsing node alias MIME: %s", err)
}
if parsed != a || len(params) > 0 {
t.Fatalf("node alias MIME should have no optional params; alias: %s, params: %v", a, params)
}
}
}
}
func TestLookup(t *testing.T) {
data := []struct {
mime string
m *MIME
}{
{root.mime, root},
{zip.mime, zip},
{zip.aliases[0], zip},
{xlsx.mime, xlsx},
}
for _, tt := range data {
t.Run(fmt.Sprintf("lookup %s", tt.mime), func(t *testing.T) {
if m := Lookup(tt.mime); m != tt.m {
t.Fatalf("failed to lookup: %s", tt.mime)
}
})
}
}
func TestExtend(t *testing.T) {
data := []struct {
mime string
ext string
parent *MIME
}{
{"foo", ".foo", nil},
{"bar", ".bar", root},
{"baz", ".baz", zip},
}
for _, tt := range data {
t.Run(fmt.Sprintf("extending to %s", tt.mime), func(t *testing.T) {
extend := Extend
if tt.parent != nil {
extend = tt.parent.Extend
} else {
tt.parent = root
}
extend(func(raw []byte, limit uint32) bool { return false }, tt.mime, tt.ext)
m := Lookup(tt.mime)
if m == nil {
t.Fatalf("mime %s not found", tt.mime)
}
if m.parent != tt.parent {
t.Fatalf("mime %s has wrong parent: want %s, got %s", tt.mime, tt.parent.mime, m.parent.mime)
}
})
}
}