Debian Codegraph @

media/mediaType_test.go

raw
1 // Copyright 2019 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package media
15
16 import (
17 "encoding/json"
18 "os"
19 "path/filepath"
20 "sort"
21 "strings"
22 "testing"
23
24 qt "github.com/frankban/quicktest"
25 "github.com/gohugoio/hugo/common/paths"
26 )
27
28 func TestGetByType(t *testing.T) {
29 c := qt.New(t)
30
31 types := DefaultTypes
32
33 mt, found := types.GetByType("text/HTML")
34 c.Assert(found, qt.Equals, true)
35 c.Assert(mt.SubType, qt.Equals, "html")
36
37 _, found = types.GetByType("text/nono")
38 c.Assert(found, qt.Equals, false)
39
40 mt, found = types.GetByType("application/rss+xml")
41 c.Assert(found, qt.Equals, true)
42 c.Assert(mt.SubType, qt.Equals, "rss")
43
44 mt, found = types.GetByType("application/rss")
45 c.Assert(found, qt.Equals, true)
46 c.Assert(mt.SubType, qt.Equals, "rss")
47 }
48
49 func TestGetByMainSubType(t *testing.T) {
50 c := qt.New(t)
51 f, found := DefaultTypes.GetByMainSubType("text", "plain")
52 c.Assert(found, qt.Equals, true)
53 c.Assert(f.SubType, qt.Equals, "plain")
54 _, found = DefaultTypes.GetByMainSubType("foo", "plain")
55 c.Assert(found, qt.Equals, false)
56 }
57
58 func TestBySuffix(t *testing.T) {
59 c := qt.New(t)
60 formats := DefaultTypes.BySuffix("xml")
61 c.Assert(len(formats), qt.Equals, 2)
62 c.Assert(formats[0].SubType, qt.Equals, "rss")
63 c.Assert(formats[1].SubType, qt.Equals, "xml")
64 }
65
66 func TestGetFirstBySuffix(t *testing.T) {
67 c := qt.New(t)
68
69 types := make(Types, len(DefaultTypes))
70 copy(types, DefaultTypes)
71
72 // Issue #8406
73 geoJSON := newMediaTypeWithMimeSuffix("application", "geo", "json", []string{"geojson", "gjson"})
74 types = append(types, geoJSON)
75 sort.Sort(types)
76
77 check := func(suffix string, expectedType Type) {
78 t, f, found := types.GetFirstBySuffix(suffix)
79 c.Assert(found, qt.Equals, true)
80 c.Assert(f, qt.Equals, SuffixInfo{
81 Suffix: suffix,
82 FullSuffix: "." + suffix,
83 })
84 c.Assert(t, qt.Equals, expectedType)
85 }
86
87 check("js", Builtin.JavascriptType)
88 check("json", Builtin.JSONType)
89 check("geojson", geoJSON)
90 check("gjson", geoJSON)
91 }
92
93 func TestFromTypeString(t *testing.T) {
94 c := qt.New(t)
95 f, err := FromString("text/html")
96 c.Assert(err, qt.IsNil)
97 c.Assert(f.Type, qt.Equals, Builtin.HTMLType.Type)
98
99 f, err = FromString("application/custom")
100 c.Assert(err, qt.IsNil)
101 c.Assert(f, qt.Equals, Type{Type: "application/custom", MainType: "application", SubType: "custom", mimeSuffix: ""})
102
103 f, err = FromString("application/custom+sfx")
104 c.Assert(err, qt.IsNil)
105 c.Assert(f, qt.Equals, Type{Type: "application/custom+sfx", MainType: "application", SubType: "custom", mimeSuffix: "sfx"})
106
107 _, err = FromString("noslash")
108 c.Assert(err, qt.Not(qt.IsNil))
109
110 f, err = FromString("text/xml; charset=utf-8")
111 c.Assert(err, qt.IsNil)
112
113 c.Assert(f, qt.Equals, Type{Type: "text/xml", MainType: "text", SubType: "xml", mimeSuffix: ""})
114 }
115
116 func TestFromStringAndExt(t *testing.T) {
117 c := qt.New(t)
118 f, err := FromStringAndExt("text/html", "html", "htm")
119 c.Assert(err, qt.IsNil)
120 c.Assert(f, qt.Equals, Builtin.HTMLType)
121 f, err = FromStringAndExt("text/html", ".html", ".htm")
122 c.Assert(err, qt.IsNil)
123 c.Assert(f, qt.Equals, Builtin.HTMLType)
124 }
125
126 // Add a test for the SVG case
127 // https://github.com/gohugoio/hugo/issues/4920
128 func TestFromExtensionMultipleSuffixes(t *testing.T) {
129 c := qt.New(t)
130 tp, si, found := DefaultTypes.GetBySuffix("svg")
131 c.Assert(found, qt.Equals, true)
132 c.Assert(tp.String(), qt.Equals, "image/svg+xml")
133 c.Assert(si.Suffix, qt.Equals, "svg")
134 c.Assert(si.FullSuffix, qt.Equals, ".svg")
135 c.Assert(tp.FirstSuffix.Suffix, qt.Equals, si.Suffix)
136 c.Assert(tp.FirstSuffix.FullSuffix, qt.Equals, si.FullSuffix)
137 ftp, found := DefaultTypes.GetByType("image/svg+xml")
138 c.Assert(found, qt.Equals, true)
139 c.Assert(ftp.String(), qt.Equals, "image/svg+xml")
140 c.Assert(found, qt.Equals, true)
141 }
142
143 func TestFromContent(t *testing.T) {
144 c := qt.New(t)
145
146 files, err := filepath.Glob("./testdata/resource.*")
147 c.Assert(err, qt.IsNil)
148
149 for _, filename := range files {
150 name := filepath.Base(filename)
151 c.Run(name, func(c *qt.C) {
152 content, err := os.ReadFile(filename)
153 c.Assert(err, qt.IsNil)
154 ext := strings.TrimPrefix(paths.Ext(filename), ".")
155 var exts []string
156 if ext == "jpg" {
157 exts = append(exts, "foo", "bar", "jpg")
158 } else {
159 exts = []string{ext}
160 }
161 expected, _, found := DefaultTypes.GetFirstBySuffix(ext)
162 c.Assert(found, qt.IsTrue)
163 got := FromContent(DefaultTypes, exts, content)
164 c.Assert(got, qt.Equals, expected)
165 })
166 }
167 }
168
169 func TestFromContentFakes(t *testing.T) {
170 c := qt.New(t)
171
172 files, err := filepath.Glob("./testdata/fake.*")
173 c.Assert(err, qt.IsNil)
174
175 for _, filename := range files {
176 name := filepath.Base(filename)
177 c.Run(name, func(c *qt.C) {
178 content, err := os.ReadFile(filename)
179 c.Assert(err, qt.IsNil)
180 ext := strings.TrimPrefix(paths.Ext(filename), ".")
181 got := FromContent(DefaultTypes, []string{ext}, content)
182 c.Assert(got, qt.Equals, zero)
183 })
184 }
185 }
186
187 func TestToJSON(t *testing.T) {
188 c := qt.New(t)
189 b, err := json.Marshal(Builtin.MPEGType)
190 c.Assert(err, qt.IsNil)
191 c.Assert(string(b), qt.Equals, `{"mainType":"video","subType":"mpeg","delimiter":".","type":"video/mpeg","string":"video/mpeg","suffixes":["mpg","mpeg"]}`)
192 }
193
194 func BenchmarkTypeOps(b *testing.B) {
195 mt := Builtin.MPEGType
196 mts := DefaultTypes
197 for b.Loop() {
198 ff := mt.FirstSuffix
199 _ = ff.FullSuffix
200 _ = mt.IsZero()
201 c, err := mt.MarshalJSON()
202 if c == nil || err != nil {
203 b.Fatal("failed")
204 }
205 _ = mt.String()
206 _ = ff.Suffix
207 _ = mt.Suffixes
208 _ = mt.Type
209 _ = mts.BySuffix("xml")
210 _, _ = mts.GetByMainSubType("application", "xml")
211 _, _, _ = mts.GetBySuffix("xml")
212 _, _ = mts.GetByType("application")
213 _, _, _ = mts.GetFirstBySuffix("xml")
214
215 }
216 }