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.
14 func setMimeInit(fn
func()) (cleanup
func()) {
17 return func() { testInitMime
= nil }
20 func clearMimeTypes() {
21 setMimeTypes(map[string]string{}, map[string]string{})
24 func setType(ext
, typ
string) {
25 if !strings
.HasPrefix(ext
, ".") {
26 panic("missing leading dot")
28 if err
:= setExtensionType(ext
, typ
); err
!= nil {
29 panic("bad test data: " + err
.Error())
33 func TestTypeByExtension(t
*testing
.T
) {
35 // initMimeForTests returns the platform-specific extension =>
36 // type tests. On Unix and Plan 9, this also tests the parsing
37 // of MIME text files (in testdata/*). On Windows, we test the
38 // real registry on the machine and assume that ".png" exists
39 // there, which empirically it always has, for all versions of
41 typeTests
:= initMimeForTests()
43 for ext
, want
:= range typeTests
{
44 val
:= TypeByExtension(ext
)
46 t
.Errorf("TypeByExtension(%q) = %q, want %q", ext
, val
, want
)
51 func TestTypeByExtension_LocalData(t
*testing
.T
) {
52 cleanup
:= setMimeInit(func() {
54 setType(".foo", "x/foo")
55 setType(".bar", "x/bar")
56 setType(".Bar", "x/bar; capital=1")
60 tests
:= map[string]string{
63 ".Bar": "x/bar; capital=1",
65 ".t1": "", // testdata shouldn't be used
68 for ext
, want
:= range tests
{
69 val
:= TypeByExtension(ext
)
71 t
.Errorf("TypeByExtension(%q) = %q, want %q", ext
, val
, want
)
76 func TestTypeByExtensionCase(t
*testing
.T
) {
77 const custom
= "test/test; charset=iso-8859-1"
78 const caps
= "test/test; WAS=ALLCAPS"
80 cleanup
:= setMimeInit(func() {
82 setType(".TEST", caps
)
83 setType(".tesT", custom
)
87 // case-sensitive lookup
88 if got
:= TypeByExtension(".tesT"); got
!= custom
{
89 t
.Fatalf("for .tesT, got %q; want %q", got
, custom
)
91 if got
:= TypeByExtension(".TEST"); got
!= caps
{
92 t
.Fatalf("for .TEST, got %q; want %s", got
, caps
)
96 if got
:= TypeByExtension(".TesT"); got
!= custom
{
97 t
.Fatalf("for .TesT, got %q; want %q", got
, custom
)
101 func TestExtensionsByType(t
*testing
.T
) {
102 cleanup
:= setMimeInit(func() {
104 setType(".gif", "image/gif")
105 setType(".a", "foo/letter")
106 setType(".b", "foo/letter")
107 setType(".B", "foo/letter")
108 setType(".PNG", "image/png")
117 {typ
: "image/gif", want
: []string{".gif"}},
118 {typ
: "image/png", want
: []string{".png"}}, // lowercase
119 {typ
: "foo/letter", want
: []string{".a", ".b"}},
120 {typ
: "x/unknown", want
: nil},
123 for _
, tt
:= range tests
{
124 got
, err
:= ExtensionsByType(tt
.typ
)
125 if err
!= nil && tt
.wantErr
!= "" && strings
.Contains(err
.Error(), tt
.wantErr
) {
129 t
.Errorf("ExtensionsByType(%q) error: %v", tt
.typ
, err
)
132 if tt
.wantErr
!= "" {
133 t
.Errorf("ExtensionsByType(%q) = %q, %v; want error substring %q", tt
.typ
, got
, err
, tt
.wantErr
)
136 if !reflect
.DeepEqual(got
, tt
.want
) {
137 t
.Errorf("ExtensionsByType(%q) = %q; want %q", tt
.typ
, got
, tt
.want
)
142 func TestLookupMallocs(t
*testing
.T
) {
143 t
.Skip("skipping test on gccgo until it has better escape analysis")
144 n
:= testing
.AllocsPerRun(10000, func() {
145 TypeByExtension(".html")
146 TypeByExtension(".HtML")
149 t
.Errorf("allocs = %v; want 0", n
)