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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mime
import (
"reflect"
"strings"
"sync"
"testing"
)
func setMimeInit(fn func()) (cleanup func()) {
once = sync.Once{}
testInitMime = fn
return func() {
testInitMime = nil
once = sync.Once{}
}
}
func clearMimeTypes() {
setMimeTypes(map[string]string{}, map[string]string{})
}
func setType(ext, typ string) {
if !strings.HasPrefix(ext, ".") {
panic("missing leading dot")
}
if err := setExtensionType(ext, typ); err != nil {
panic("bad test data: " + err.Error())
}
}
func TestTypeByExtension(t *testing.T) {
once = sync.Once{}
// initMimeForTests returns the platform-specific extension =>
// type tests. On Unix and Plan 9, this also tests the parsing
// of MIME text files (in testdata/*). On Windows, we test the
// real registry on the machine and assume that ".png" exists
// there, which empirically it always has, for all versions of
// Windows.
typeTests := initMimeForTests()
for ext, want := range typeTests {
val := TypeByExtension(ext)
if val != want {
t.Errorf("TypeByExtension(%q) = %q, want %q", ext, val, want)
}
}
}
func TestTypeByExtension_LocalData(t *testing.T) {
cleanup := setMimeInit(func() {
clearMimeTypes()
setType(".foo", "x/foo")
setType(".bar", "x/bar")
setType(".Bar", "x/bar; capital=1")
})
defer cleanup()
tests := map[string]string{
".foo": "x/foo",
".bar": "x/bar",
".Bar": "x/bar; capital=1",
".sdlkfjskdlfj": "",
".t1": "", // testdata shouldn't be used
}
for ext, want := range tests {
val := TypeByExtension(ext)
if val != want {
t.Errorf("TypeByExtension(%q) = %q, want %q", ext, val, want)
}
}
}
func TestTypeByExtensionCase(t *testing.T) {
const custom = "test/test; charset=iso-8859-1"
const caps = "test/test; WAS=ALLCAPS"
cleanup := setMimeInit(func() {
clearMimeTypes()
setType(".TEST", caps)
setType(".tesT", custom)
})
defer cleanup()
// case-sensitive lookup
if got := TypeByExtension(".tesT"); got != custom {
t.Fatalf("for .tesT, got %q; want %q", got, custom)
}
if got := TypeByExtension(".TEST"); got != caps {
t.Fatalf("for .TEST, got %q; want %s", got, caps)
}
// case-insensitive
if got := TypeByExtension(".TesT"); got != custom {
t.Fatalf("for .TesT, got %q; want %q", got, custom)
}
}
func TestExtensionsByType(t *testing.T) {
cleanup := setMimeInit(func() {
clearMimeTypes()
setType(".gif", "image/gif")
setType(".a", "foo/letter")
setType(".b", "foo/letter")
setType(".B", "foo/letter")
setType(".PNG", "image/png")
})
defer cleanup()
tests := []struct {
typ string
want []string
wantErr string
}{
{typ: "image/gif", want: []string{".gif"}},
{typ: "image/png", want: []string{".png"}}, // lowercase
{typ: "foo/letter", want: []string{".a", ".b"}},
{typ: "x/unknown", want: nil},
}
for _, tt := range tests {
got, err := ExtensionsByType(tt.typ)
if err != nil && tt.wantErr != "" && strings.Contains(err.Error(), tt.wantErr) {
continue
}
if err != nil {
t.Errorf("ExtensionsByType(%q) error: %v", tt.typ, err)
continue
}
if tt.wantErr != "" {
t.Errorf("ExtensionsByType(%q) = %q, %v; want error substring %q", tt.typ, got, err, tt.wantErr)
continue
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ExtensionsByType(%q) = %q; want %q", tt.typ, got, tt.want)
}
}
}
func TestLookupMallocs(t *testing.T) {
t.Skip("skipping test on gccgo until it has better escape analysis")
n := testing.AllocsPerRun(10000, func() {
TypeByExtension(".html")
TypeByExtension(".HtML")
})
if n > 0 {
t.Errorf("allocs = %v; want 0", n)
}
}
func BenchmarkTypeByExtension(b *testing.B) {
initMime()
b.ResetTimer()
for _, ext := range []string{
".html",
".HTML",
".unused",
} {
b.Run(ext, func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
TypeByExtension(ext)
}
})
})
}
}
func BenchmarkExtensionsByType(b *testing.B) {
initMime()
b.ResetTimer()
for _, typ := range []string{
"text/html",
"text/html; charset=utf-8",
"application/octet-stream",
} {
b.Run(typ, func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := ExtensionsByType(typ); err != nil {
b.Fatal(err)
}
}
})
})
}
}
func TestExtensionsByType2(t *testing.T) {
cleanup := setMimeInit(func() {
clearMimeTypes()
// Initialize built-in types like in type.go before osInitMime.
setMimeTypes(builtinTypesLower, builtinTypesLower)
})
defer cleanup()
tests := []struct {
typ string
want []string
}{
{typ: "image/jpeg", want: []string{".jpeg", ".jpg"}},
}
for _, tt := range tests {
got, err := ExtensionsByType(tt.typ)
if err != nil {
t.Errorf("ExtensionsByType(%q): %v", tt.typ, err)
continue
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ExtensionsByType(%q) = %q; want %q", tt.typ, got, tt.want)
}
}
}
|