diff options
Diffstat (limited to 'media/mediaType_test.go')
-rw-r--r-- | media/mediaType_test.go | 121 |
1 files changed, 87 insertions, 34 deletions
diff --git a/media/mediaType_test.go b/media/mediaType_test.go index f3ddb086c..6385528ee 100644 --- a/media/mediaType_test.go +++ b/media/mediaType_test.go @@ -28,21 +28,21 @@ func TestDefaultTypes(t *testing.T) { expectedType string expectedString string }{ - {CalendarType, "text", "calendar", "ics", "text/calendar", "text/calendar+ics"}, - {CSSType, "text", "css", "css", "text/css", "text/css+css"}, - {SCSSType, "text", "x-scss", "scss", "text/x-scss", "text/x-scss+scss"}, - {CSVType, "text", "csv", "csv", "text/csv", "text/csv+csv"}, - {HTMLType, "text", "html", "html", "text/html", "text/html+html"}, - {JavascriptType, "application", "javascript", "js", "application/javascript", "application/javascript+js"}, - {JSONType, "application", "json", "json", "application/json", "application/json+json"}, - {RSSType, "application", "rss", "xml", "application/rss", "application/rss+xml"}, - {SVGType, "image", "svg", "svg", "image/svg", "image/svg+svg"}, - {TextType, "text", "plain", "txt", "text/plain", "text/plain+txt"}, - {XMLType, "application", "xml", "xml", "application/xml", "application/xml+xml"}, + {CalendarType, "text", "calendar", "ics", "text/calendar", "text/calendar"}, + {CSSType, "text", "css", "css", "text/css", "text/css"}, + {SCSSType, "text", "x-scss", "scss", "text/x-scss", "text/x-scss"}, + {CSVType, "text", "csv", "csv", "text/csv", "text/csv"}, + {HTMLType, "text", "html", "html", "text/html", "text/html"}, + {JavascriptType, "application", "javascript", "js", "application/javascript", "application/javascript"}, + {JSONType, "application", "json", "json", "application/json", "application/json"}, + {RSSType, "application", "rss", "xml", "application/rss+xml", "application/rss+xml"}, + {SVGType, "image", "svg", "svg", "image/svg+xml", "image/svg+xml"}, + {TextType, "text", "plain", "txt", "text/plain", "text/plain"}, + {XMLType, "application", "xml", "xml", "application/xml", "application/xml"}, } { require.Equal(t, test.expectedMainType, test.tp.MainType) require.Equal(t, test.expectedSubType, test.tp.SubType) - require.Equal(t, test.expectedSuffix, test.tp.Suffix) + require.Equal(t, test.expectedSuffix, test.tp.Suffix(), test.tp.String()) require.Equal(t, defaultDelimiter, test.tp.Delimiter) require.Equal(t, test.expectedType, test.tp.Type()) @@ -61,34 +61,68 @@ func TestGetByType(t *testing.T) { _, found = types.GetByType("text/nono") require.False(t, found) + + mt, found = types.GetByType("application/rss+xml") + require.True(t, found) + require.Equal(t, mt, RSSType) + + mt, found = types.GetByType("application/rss") + require.True(t, found) + require.Equal(t, mt, RSSType) +} + +func TestGetByMainSubType(t *testing.T) { + assert := require.New(t) + f, found := DefaultTypes.GetByMainSubType("text", "plain") + assert.True(found) + assert.Equal(f, TextType) + _, found = DefaultTypes.GetByMainSubType("foo", "plain") + assert.False(found) } func TestGetFirstBySuffix(t *testing.T) { assert := require.New(t) f, found := DefaultTypes.GetFirstBySuffix("xml") assert.True(found) - assert.Equal(Type{MainType: "application", SubType: "rss", Suffix: "xml", Delimiter: "."}, f) + assert.Equal(Type{MainType: "application", SubType: "rss", OldSuffix: "xml", Delimiter: ".", Suffixes: []string{"xml"}, fileSuffix: "xml"}, f) } func TestFromTypeString(t *testing.T) { - f, err := FromString("text/html") + f, err := fromString("text/html") require.NoError(t, err) - require.Equal(t, HTMLType, f) + require.Equal(t, HTMLType.Type(), f.Type()) - f, err = FromString("application/custom") + f, err = fromString("application/custom") require.NoError(t, err) - require.Equal(t, Type{MainType: "application", SubType: "custom", Suffix: "custom", Delimiter: defaultDelimiter}, f) + require.Equal(t, Type{MainType: "application", SubType: "custom", OldSuffix: "", fileSuffix: ""}, f) - f, err = FromString("application/custom+pdf") + f, err = fromString("application/custom+sfx") require.NoError(t, err) - require.Equal(t, Type{MainType: "application", SubType: "custom", Suffix: "pdf", Delimiter: defaultDelimiter}, f) + require.Equal(t, Type{MainType: "application", SubType: "custom", OldSuffix: "sfx"}, f) - _, err = FromString("noslash") + _, err = fromString("noslash") require.Error(t, err) - f, err = FromString("text/xml; charset=utf-8") + f, err = fromString("text/xml; charset=utf-8") require.NoError(t, err) - require.Equal(t, Type{MainType: "text", SubType: "xml", Suffix: "xml", Delimiter: "."}, f) + require.Equal(t, Type{MainType: "text", SubType: "xml", OldSuffix: ""}, f) + require.Equal(t, "", f.Suffix()) +} + +// Add a test for the SVG case +// https://github.com/gohugoio/hugo/issues/4920 +func TestFromExtensionMultipleSuffixes(t *testing.T) { + assert := require.New(t) + tp, found := DefaultTypes.GetBySuffix("svg") + assert.True(found) + assert.Equal("image/svg+xml", tp.String()) + assert.Equal("svg", tp.fileSuffix) + assert.Equal(".svg", tp.FullSuffix()) + tp, found = DefaultTypes.GetByType("image/svg+xml") + assert.True(found) + assert.Equal("image/svg+xml", tp.String()) + assert.True(found) + assert.Equal(".svg", tp.FullSuffix()) } @@ -105,13 +139,40 @@ func TestDecodeTypes(t *testing.T) { []map[string]interface{}{ { "application/json": map[string]interface{}{ - "suffix": "jsn"}}}, + "suffixes": []string{"jasn"}}}}, false, func(t *testing.T, name string, tt Types) { require.Len(t, tt, len(DefaultTypes)) - json, found := tt.GetBySuffix("jsn") + json, found := tt.GetBySuffix("jasn") require.True(t, found) - require.Equal(t, "application/json+jsn", json.String(), name) + require.Equal(t, "application/json", json.String(), name) + }}, + { + "Suffix from key, multiple file suffixes", + []map[string]interface{}{ + { + "application/hugo+hg": map[string]interface{}{ + "Suffixes": []string{"hg1", "hg2"}, + }}}, + false, + func(t *testing.T, name string, tt Types) { + require.Len(t, tt, len(DefaultTypes)+1) + hg, found := tt.GetBySuffix("hg") + require.True(t, found) + require.Equal(t, "hg", hg.OldSuffix) + require.Equal(t, "hg", hg.Suffix()) + require.Equal(t, ".hg", hg.FullSuffix()) + require.Equal(t, "application/hugo+hg", hg.String(), name) + hg, found = tt.GetBySuffix("hg2") + require.True(t, found) + require.Equal(t, "hg", hg.OldSuffix) + require.Equal(t, "hg2", hg.Suffix()) + require.Equal(t, ".hg2", hg.FullSuffix()) + require.Equal(t, "application/hugo+hg", hg.String(), name) + + hg, found = tt.GetByType("application/hugo+hg") + require.True(t, found) + }}, { "Add custom media type", @@ -123,6 +184,7 @@ func TestDecodeTypes(t *testing.T) { func(t *testing.T, name string, tt Types) { require.Len(t, tt, len(DefaultTypes)+1) // Make sure we have not broken the default config. + _, found := tt.GetBySuffix("json") require.True(t, found) @@ -130,15 +192,6 @@ func TestDecodeTypes(t *testing.T) { require.True(t, found) require.Equal(t, "text/hugo+hgo", hugo.String(), name) }}, - { - "Add media type invalid key", - []map[string]interface{}{ - { - "text/hugo+hgo": map[string]interface{}{}}}, - true, - func(t *testing.T, name string, tt Types) { - - }}, } for _, test := range tests { |