| 1 |
// Copyright 2024 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 |
"fmt" |
| 18 |
"testing" |
| 19 |
|
| 20 |
qt "github.com/frankban/quicktest" |
| 21 |
) |
| 22 |
|
| 23 |
func TestDecodeTypes(t *testing.T) { |
| 24 |
c := qt.New(t) |
| 25 |
|
| 26 |
tests := []struct { |
| 27 |
name string |
| 28 |
m map[string]any |
| 29 |
shouldError bool |
| 30 |
assert func(t *testing.T, name string, tt Types) |
| 31 |
}{ |
| 32 |
{ |
| 33 |
"Redefine JSON", |
| 34 |
map[string]any{ |
| 35 |
"application/json": map[string]any{ |
| 36 |
"suffixes": []string{"jasn"}, |
| 37 |
}, |
| 38 |
}, |
| 39 |
|
| 40 |
false, |
| 41 |
func(t *testing.T, name string, tt Types) { |
| 42 |
for _, ttt := range tt { |
| 43 |
if _, ok := DefaultTypes.GetByType(ttt.Type); !ok { |
| 44 |
fmt.Println(ttt.Type, "not found in default types") |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
c.Assert(len(tt), qt.Equals, len(DefaultTypes)) |
| 49 |
json, si, found := tt.GetBySuffix("jasn") |
| 50 |
c.Assert(found, qt.Equals, true) |
| 51 |
c.Assert(json.String(), qt.Equals, "application/json") |
| 52 |
c.Assert(si.FullSuffix, qt.Equals, ".jasn") |
| 53 |
}, |
| 54 |
}, |
| 55 |
{ |
| 56 |
"MIME suffix in key, multiple file suffixes, custom delimiter", |
| 57 |
map[string]any{ |
| 58 |
"application/hugo+hg": map[string]any{ |
| 59 |
"suffixes": []string{"hg1", "hG2"}, |
| 60 |
"Delimiter": "_", |
| 61 |
}, |
| 62 |
}, |
| 63 |
false, |
| 64 |
func(t *testing.T, name string, tt Types) { |
| 65 |
c.Assert(len(tt), qt.Equals, len(DefaultTypes)+1) |
| 66 |
hg, si, found := tt.GetBySuffix("hg2") |
| 67 |
c.Assert(found, qt.Equals, true) |
| 68 |
c.Assert(hg.FirstSuffix.Suffix, qt.Equals, "hg1") |
| 69 |
c.Assert(hg.FirstSuffix.FullSuffix, qt.Equals, "_hg1") |
| 70 |
c.Assert(si.Suffix, qt.Equals, "hg2") |
| 71 |
c.Assert(si.FullSuffix, qt.Equals, "_hg2") |
| 72 |
c.Assert(hg.String(), qt.Equals, "application/hugo+hg") |
| 73 |
|
| 74 |
_, found = tt.GetByType("application/hugo+hg") |
| 75 |
c.Assert(found, qt.Equals, true) |
| 76 |
}, |
| 77 |
}, |
| 78 |
{ |
| 79 |
"Add custom media type", |
| 80 |
map[string]any{ |
| 81 |
"text/hugo+hgo": map[string]any{ |
| 82 |
"Suffixes": []string{"hgo2"}, |
| 83 |
}, |
| 84 |
}, |
| 85 |
false, |
| 86 |
func(t *testing.T, name string, tp Types) { |
| 87 |
c.Assert(len(tp), qt.Equals, len(DefaultTypes)+1) |
| 88 |
// Make sure we have not broken the default config. |
| 89 |
|
| 90 |
_, _, found := tp.GetBySuffix("json") |
| 91 |
c.Assert(found, qt.Equals, true) |
| 92 |
|
| 93 |
hugo, _, found := tp.GetBySuffix("hgo2") |
| 94 |
c.Assert(found, qt.Equals, true) |
| 95 |
c.Assert(hugo.String(), qt.Equals, "text/hugo+hgo") |
| 96 |
}, |
| 97 |
}, |
| 98 |
} |
| 99 |
|
| 100 |
for _, test := range tests { |
| 101 |
result, err := DecodeTypes(test.m) |
| 102 |
if test.shouldError { |
| 103 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 104 |
} else { |
| 105 |
c.Assert(err, qt.IsNil) |
| 106 |
test.assert(t, test.name, result.Config) |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func TestDefaultTypes(t *testing.T) { |
| 112 |
c := qt.New(t) |
| 113 |
for _, test := range []struct { |
| 114 |
tp Type |
| 115 |
expectedMainType string |
| 116 |
expectedSubType string |
| 117 |
expectedSuffixes string |
| 118 |
expectedType string |
| 119 |
expectedString string |
| 120 |
}{ |
| 121 |
{Builtin.CalendarType, "text", "calendar", "ics", "text/calendar", "text/calendar"}, |
| 122 |
{Builtin.CSSType, "text", "css", "css", "text/css", "text/css"}, |
| 123 |
{Builtin.SCSSType, "text", "x-scss", "scss", "text/x-scss", "text/x-scss"}, |
| 124 |
{Builtin.CSVType, "text", "csv", "csv", "text/csv", "text/csv"}, |
| 125 |
{Builtin.HTMLType, "text", "html", "html,htm", "text/html", "text/html"}, |
| 126 |
{Builtin.MarkdownType, "text", "markdown", "md,mdown,markdown", "text/markdown", "text/markdown"}, |
| 127 |
{Builtin.EmacsOrgModeType, "text", "org", "org", "text/org", "text/org"}, |
| 128 |
{Builtin.PandocType, "text", "pandoc", "pandoc,pdc", "text/pandoc", "text/pandoc"}, |
| 129 |
{Builtin.ReStructuredTextType, "text", "rst", "rst", "text/rst", "text/rst"}, |
| 130 |
{Builtin.AsciiDocType, "text", "asciidoc", "adoc,asciidoc,ad", "text/asciidoc", "text/asciidoc"}, |
| 131 |
{Builtin.JavascriptType, "text", "javascript", "js,jsm,mjs", "text/javascript", "text/javascript"}, |
| 132 |
{Builtin.TypeScriptType, "text", "typescript", "ts", "text/typescript", "text/typescript"}, |
| 133 |
{Builtin.TSXType, "text", "tsx", "tsx", "text/tsx", "text/tsx"}, |
| 134 |
{Builtin.JSXType, "text", "jsx", "jsx", "text/jsx", "text/jsx"}, |
| 135 |
{Builtin.JSONType, "application", "json", "json", "application/json", "application/json"}, |
| 136 |
{Builtin.RSSType, "application", "rss", "xml,rss", "application/rss+xml", "application/rss+xml"}, |
| 137 |
{Builtin.SVGType, "image", "svg", "svg", "image/svg+xml", "image/svg+xml"}, |
| 138 |
{Builtin.TextType, "text", "plain", "txt", "text/plain", "text/plain"}, |
| 139 |
{Builtin.XMLType, "application", "xml", "xml", "application/xml", "application/xml"}, |
| 140 |
{Builtin.TOMLType, "application", "toml", "toml", "application/toml", "application/toml"}, |
| 141 |
{Builtin.YAMLType, "application", "yaml", "yaml,yml", "application/yaml", "application/yaml"}, |
| 142 |
{Builtin.PDFType, "application", "pdf", "pdf", "application/pdf", "application/pdf"}, |
| 143 |
{Builtin.TrueTypeFontType, "font", "ttf", "ttf", "font/ttf", "font/ttf"}, |
| 144 |
{Builtin.OpenTypeFontType, "font", "otf", "otf", "font/otf", "font/otf"}, |
| 145 |
{Builtin.WOFFFontType, "font", "woff", "woff", "font/woff", "font/woff"}, |
| 146 |
{Builtin.WOFF2FontType, "font", "woff2", "woff2", "font/woff2", "font/woff2"}, |
| 147 |
{Builtin.SourceMapType, "application", "source-map", "map", "application/source-map", "application/source-map"}, |
| 148 |
} { |
| 149 |
c.Assert(test.tp.MainType, qt.Equals, test.expectedMainType) |
| 150 |
c.Assert(test.tp.SubType, qt.Equals, test.expectedSubType) |
| 151 |
c.Assert(test.tp.SuffixesCSV, qt.Equals, test.expectedSuffixes) |
| 152 |
c.Assert(test.tp.Type, qt.Equals, test.expectedType) |
| 153 |
c.Assert(test.tp.String(), qt.Equals, test.expectedString) |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
c.Assert(len(DefaultTypes), qt.Equals, 47) |
| 158 |
} |