github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/src/mime/type_test.go (about) 1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package mime 6 7 import ( 8 "testing" 9 ) 10 11 var typeTests = initMimeForTests() 12 13 func TestTypeByExtension(t *testing.T) { 14 for ext, want := range typeTests { 15 val := TypeByExtension(ext) 16 if val != want { 17 t.Errorf("TypeByExtension(%q) = %q, want %q", ext, val, want) 18 } 19 } 20 } 21 22 func TestTypeByExtensionCase(t *testing.T) { 23 const custom = "test/test; charset=iso-8859-1" 24 const caps = "test/test; WAS=ALLCAPS" 25 if err := AddExtensionType(".TEST", caps); err != nil { 26 t.Fatalf("error %s for AddExtension(%s)", err, custom) 27 } 28 if err := AddExtensionType(".tesT", custom); err != nil { 29 t.Fatalf("error %s for AddExtension(%s)", err, custom) 30 } 31 32 // case-sensitive lookup 33 if got := TypeByExtension(".tesT"); got != custom { 34 t.Fatalf("for .tesT, got %q; want %q", got, custom) 35 } 36 if got := TypeByExtension(".TEST"); got != caps { 37 t.Fatalf("for .TEST, got %q; want %s", got, caps) 38 } 39 40 // case-insensitive 41 if got := TypeByExtension(".TesT"); got != custom { 42 t.Fatalf("for .TesT, got %q; want %q", got, custom) 43 } 44 } 45 46 func TestLookupMallocs(t *testing.T) { 47 n := testing.AllocsPerRun(10000, func() { 48 TypeByExtension(".html") 49 TypeByExtension(".HtML") 50 }) 51 if n > 0 { 52 t.Errorf("allocs = %v; want 0", n) 53 } 54 }