forked from gabriel-vasile/mimetype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_test.go
79 lines (71 loc) · 2.04 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
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)
}
}
}
func TestIndexOutOfRange(t *testing.T) {
for _, n := range root.flatten() {
_ = n.matchFunc(nil)
}
}
// 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)
}
}
}
}