)
var (
- mimeLock sync.RWMutex
- mimeTypesLower = map[string]string{
- ".css": "text/css; charset=utf-8",
- ".gif": "image/gif",
- ".htm": "text/html; charset=utf-8",
- ".html": "text/html; charset=utf-8",
- ".jpg": "image/jpeg",
- ".js": "application/x-javascript",
- ".pdf": "application/pdf",
- ".png": "image/png",
- ".svg": "image/svg+xml",
- ".xml": "text/xml; charset=utf-8",
- }
- mimeTypes = clone(mimeTypesLower)
- extensions = invert(mimeTypesLower)
+ mimeLock sync.RWMutex // guards following 3 maps
+ mimeTypes map[string]string // ".Z" => "application/x-compress"
+ mimeTypesLower map[string]string // ".z" => "application/x-compress"
+
+ // extensions maps from MIME type to list of lowercase file
+ // extensions: "image/jpeg" => [".jpg", ".jpeg"]
+ extensions map[string][]string
)
+// setMimeTypes is used by initMime's non-test path, and by tests.
+// The two maps must not be the same, or nil.
+func setMimeTypes(lowerExt, mixExt map[string]string) {
+ if lowerExt == nil || mixExt == nil {
+ panic("nil map")
+ }
+ mimeTypesLower = lowerExt
+ mimeTypes = mixExt
+ extensions = invert(lowerExt)
+}
+
+var builtinTypesLower = map[string]string{
+ ".css": "text/css; charset=utf-8",
+ ".gif": "image/gif",
+ ".htm": "text/html; charset=utf-8",
+ ".html": "text/html; charset=utf-8",
+ ".jpg": "image/jpeg",
+ ".js": "application/x-javascript",
+ ".pdf": "application/pdf",
+ ".png": "image/png",
+ ".svg": "image/svg+xml",
+ ".xml": "text/xml; charset=utf-8",
+}
+
func clone(m map[string]string) map[string]string {
m2 := make(map[string]string, len(m))
for k, v := range m {
m2[k] = v
if strings.ToLower(k) != k {
- panic("keys in mimeTypesLower must be lowercase")
+ panic("keys in builtinTypesLower must be lowercase")
}
}
return m2
var once sync.Once // guards initMime
+var testInitMime, osInitMime func()
+
+func initMime() {
+ if fn := testInitMime; fn != nil {
+ fn()
+ } else {
+ setMimeTypes(builtinTypesLower, clone(builtinTypesLower))
+ osInitMime()
+ }
+}
+
// TypeByExtension returns the MIME type associated with the file extension ext.
// The extension ext should begin with a leading dot, as in ".html".
// When ext has no associated type, TypeByExtension returns "".
defer mimeLock.RUnlock()
// Case-sensitive lookup.
- v := mimeTypes[ext]
- if v != "" {
+ if v := mimeTypes[ext]; v != "" {
return v
}
// a leading dot, as in ".html".
func AddExtensionType(ext, typ string) error {
if !strings.HasPrefix(ext, ".") {
- return fmt.Errorf(`mime: extension %q misses dot`, ext)
+ return fmt.Errorf("mime: extension %q missing leading dot", ext)
}
once.Do(initMime)
return setExtensionType(ext, typ)
import (
"reflect"
- "sort"
"strings"
+ "sync"
"testing"
)
-var typeTests = initMimeForTests()
+func setMimeInit(fn func()) (cleanup func()) {
+ once = sync.Once{}
+ testInitMime = fn
+ return func() { testInitMime = nil }
+}
+
+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 {
}
}
+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"
- if err := AddExtensionType(".TEST", caps); err != nil {
- t.Fatalf("error %s for AddExtension(%s)", err, custom)
- }
- if err := AddExtensionType(".tesT", custom); err != nil {
- t.Fatalf("error %s for AddExtension(%s)", err, custom)
- }
+
+ cleanup := setMimeInit(func() {
+ clearMimeTypes()
+ setType(".TEST", caps)
+ setType(".tesT", custom)
+ })
+ defer cleanup()
// case-sensitive lookup
if got := TypeByExtension(".tesT"); got != custom {
}
func TestExtensionsByType(t *testing.T) {
- for want, typ := range typeTests {
- val, err := ExtensionsByType(typ)
+ 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("error %s for ExtensionsByType(%q)", err, typ)
+ t.Errorf("ExtensionsByType(%q) error: %v", tt.typ, err)
continue
}
- if len(val) != 1 {
- t.Errorf("ExtensionsByType(%q) = %v; expected exactly 1 entry", typ, val)
+ if tt.wantErr != "" {
+ t.Errorf("ExtensionsByType(%q) = %q, %v; want error substring %q", tt.typ, got, err, tt.wantErr)
continue
}
- // We always expect lower case, test data includes upper-case.
- want = strings.ToLower(want)
- if val[0] != want {
- t.Errorf("ExtensionsByType(%q) = %q, want %q", typ, val[0], want)
+ if !reflect.DeepEqual(got, tt.want) {
+ t.Errorf("ExtensionsByType(%q) = %q; want %q", tt.typ, got, tt.want)
}
}
}
-func TestExtensionsByTypeMultiple(t *testing.T) {
- const typ = "text/html"
- exts, err := ExtensionsByType(typ)
- if err != nil {
- t.Fatalf("ExtensionsByType(%q) error: %v", typ, err)
- }
- sort.Strings(exts)
- if want := []string{".htm", ".html"}; !reflect.DeepEqual(exts, want) {
- t.Errorf("ExtensionsByType(%q) = %v; want %v", typ, exts, want)
- }
-}
-
-func TestExtensionsByTypeNoDuplicates(t *testing.T) {
- const (
- typ = "text/html"
- ext = ".html"
- )
- AddExtensionType(ext, typ)
- AddExtensionType(ext, typ)
- exts, err := ExtensionsByType(typ)
- if err != nil {
- t.Fatalf("ExtensionsByType(%q) error: %v", typ, err)
- }
- count := 0
- for _, v := range exts {
- if v == ext {
- count++
- }
- }
- if count != 1 {
- t.Errorf("ExtensionsByType(%q) = %v; want %v once", typ, exts, ext)
- }
-}
-
func TestLookupMallocs(t *testing.T) {
n := testing.AllocsPerRun(10000, func() {
TypeByExtension(".html")