OLD | NEW |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package mime | 5 package mime |
6 | 6 |
7 import "testing" | 7 import ( |
| 8 » "testing" |
| 9 ) |
8 | 10 |
9 var typeTests = initMimeForTests() | 11 var typeTests = initMimeForTests() |
10 | 12 |
11 func TestTypeByExtension(t *testing.T) { | 13 func TestTypeByExtension(t *testing.T) { |
12 for ext, want := range typeTests { | 14 for ext, want := range typeTests { |
13 val := TypeByExtension(ext) | 15 val := TypeByExtension(ext) |
14 if val != want { | 16 if val != want { |
15 t.Errorf("TypeByExtension(%q) = %q, want %q", ext, val,
want) | 17 t.Errorf("TypeByExtension(%q) = %q, want %q", ext, val,
want) |
16 } | 18 } |
17 | |
18 } | 19 } |
19 } | 20 } |
20 | 21 |
21 func TestCustomExtension(t *testing.T) { | 22 func TestTypeByExtensionCase(t *testing.T) { |
22 » custom := "text/xml; charset=iso-8859-1" | 23 » const custom = "test/test; charset=iso-8859-1" |
23 » if error := AddExtensionType(".xml", custom); error != nil { | 24 » const caps = "test/test; WAS=ALLCAPS" |
24 » » t.Fatalf("error %s for AddExtension(%s)", error, custom) | 25 » if err := AddExtensionType(".TEST", caps); err != nil { |
| 26 » » t.Fatalf("error %s for AddExtension(%s)", err, custom) |
25 } | 27 } |
26 » if registered := TypeByExtension(".xml"); registered != custom { | 28 » if err := AddExtensionType(".tesT", custom); err != nil { |
27 » » t.Fatalf("registered %s instead of %s", registered, custom) | 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) |
28 } | 43 } |
29 } | 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 } |
OLD | NEW |