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()) {
23 func clearMimeTypes() {
24 setMimeTypes(map[string]string{}, map[string]string{})
27 func setType(ext
, typ
string) {
28 if !strings
.HasPrefix(ext
, ".") {
29 panic("missing leading dot")
31 if err
:= setExtensionType(ext
, typ
); err
!= nil {
32 panic("bad test data: " + err
.Error())
36 func TestTypeByExtension(t
*testing
.T
) {
38 // initMimeForTests returns the platform-specific extension =>
39 // type tests. On Unix and Plan 9, this also tests the parsing
40 // of MIME text files (in testdata/*). On Windows, we test the
41 // real registry on the machine and assume that ".png" exists
42 // there, which empirically it always has, for all versions of
44 typeTests
:= initMimeForTests()
46 for ext
, want
:= range typeTests
{
47 val
:= TypeByExtension(ext
)
49 t
.Errorf("TypeByExtension(%q) = %q, want %q", ext
, val
, want
)
54 func TestTypeByExtension_LocalData(t
*testing
.T
) {
55 cleanup
:= setMimeInit(func() {
57 setType(".foo", "x/foo")
58 setType(".bar", "x/bar")
59 setType(".Bar", "x/bar; capital=1")
63 tests
:= map[string]string{
66 ".Bar": "x/bar; capital=1",
68 ".t1": "", // testdata shouldn't be used
71 for ext
, want
:= range tests
{
72 val
:= TypeByExtension(ext
)
74 t
.Errorf("TypeByExtension(%q) = %q, want %q", ext
, val
, want
)
79 func TestTypeByExtensionCase(t
*testing
.T
) {
80 const custom
= "test/test; charset=iso-8859-1"
81 const caps
= "test/test; WAS=ALLCAPS"
83 cleanup
:= setMimeInit(func() {
85 setType(".TEST", caps
)
86 setType(".tesT", custom
)
90 // case-sensitive lookup
91 if got
:= TypeByExtension(".tesT"); got
!= custom
{
92 t
.Fatalf("for .tesT, got %q; want %q", got
, custom
)
94 if got
:= TypeByExtension(".TEST"); got
!= caps
{
95 t
.Fatalf("for .TEST, got %q; want %s", got
, caps
)
99 if got
:= TypeByExtension(".TesT"); got
!= custom
{
100 t
.Fatalf("for .TesT, got %q; want %q", got
, custom
)
104 func TestExtensionsByType(t
*testing
.T
) {
105 cleanup
:= setMimeInit(func() {
107 setType(".gif", "image/gif")
108 setType(".a", "foo/letter")
109 setType(".b", "foo/letter")
110 setType(".B", "foo/letter")
111 setType(".PNG", "image/png")
120 {typ
: "image/gif", want
: []string{".gif"}},
121 {typ
: "image/png", want
: []string{".png"}}, // lowercase
122 {typ
: "foo/letter", want
: []string{".a", ".b"}},
123 {typ
: "x/unknown", want
: nil},
126 for _
, tt
:= range tests
{
127 got
, err
:= ExtensionsByType(tt
.typ
)
128 if err
!= nil && tt
.wantErr
!= "" && strings
.Contains(err
.Error(), tt
.wantErr
) {
132 t
.Errorf("ExtensionsByType(%q) error: %v", tt
.typ
, err
)
135 if tt
.wantErr
!= "" {
136 t
.Errorf("ExtensionsByType(%q) = %q, %v; want error substring %q", tt
.typ
, got
, err
, tt
.wantErr
)
139 if !reflect
.DeepEqual(got
, tt
.want
) {
140 t
.Errorf("ExtensionsByType(%q) = %q; want %q", tt
.typ
, got
, tt
.want
)
145 func TestLookupMallocs(t
*testing
.T
) {
146 t
.Skip("skipping test on gccgo until it has better escape analysis")
147 n
:= testing
.AllocsPerRun(10000, func() {
148 TypeByExtension(".html")
149 TypeByExtension(".HtML")
152 t
.Errorf("allocs = %v; want 0", n
)
156 func BenchmarkTypeByExtension(b
*testing
.B
) {
160 for _
, ext
:= range []string{
165 b
.Run(ext
, func(b
*testing
.B
) {
166 b
.RunParallel(func(pb
*testing
.PB
) {
175 func BenchmarkExtensionsByType(b
*testing
.B
) {
179 for _
, typ
:= range []string{
181 "text/html; charset=utf-8",
182 "application/octet-stream",
184 b
.Run(typ
, func(b
*testing
.B
) {
185 b
.RunParallel(func(pb
*testing
.PB
) {
187 if _
, err
:= ExtensionsByType(typ
); err
!= nil {
196 func TestExtensionsByType2(t
*testing
.T
) {
197 cleanup
:= setMimeInit(func() {
199 // Initialize built-in types like in type.go before osInitMime.
200 setMimeTypes(builtinTypesLower
, builtinTypesLower
)
208 {typ
: "image/jpeg", want
: []string{".jpeg", ".jpg"}},
211 for _
, tt
:= range tests
{
212 got
, err
:= ExtensionsByType(tt
.typ
)
214 t
.Errorf("ExtensionsByType(%q): %v", tt
.typ
, err
)
217 if !reflect
.DeepEqual(got
, tt
.want
) {
218 t
.Errorf("ExtensionsByType(%q) = %q; want %q", tt
.typ
, got
, tt
.want
)