Regression in media type suffix lookup
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 20 Apr 2021 10:05:25 +0000 (12:05 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 20 Apr 2021 10:52:30 +0000 (12:52 +0200)
Introduced in Hugo 0.82.0.

Fixes #8406

media/mediaType.go
media/mediaType_test.go

index a35d80e3e9439a92d98811d8c523a1a807d00da2..cba26ed2b516c91042a21b12e8d3d6855d5ea19b 100644 (file)
@@ -303,7 +303,7 @@ func (t Types) GetBySuffix(suffix string) (tp Type, si SuffixInfo, found bool) {
 }
 
 func (m Type) hasSuffix(suffix string) bool {
-       return strings.Contains(m.suffixesCSV, suffix)
+       return strings.Contains(","+m.suffixesCSV+",", ","+suffix+",")
 }
 
 // GetByMainSubType gets a media type given a main and a sub type e.g. "text" and "plain".
index e44ab27ec6ec9be57728f17bd277874831b177d7..587d54e10a7543e4458c47e4cd91fb7ceaa53c4d 100644 (file)
@@ -15,6 +15,7 @@ package media
 
 import (
        "encoding/json"
+       "sort"
        "testing"
 
        qt "github.com/frankban/quicktest"
@@ -98,11 +99,28 @@ func TestBySuffix(t *testing.T) {
 
 func TestGetFirstBySuffix(t *testing.T) {
        c := qt.New(t)
-       _, f, found := DefaultTypes.GetFirstBySuffix("xml")
-       c.Assert(found, qt.Equals, true)
-       c.Assert(f, qt.Equals, SuffixInfo{
-               Suffix:     "xml",
-               FullSuffix: ".xml"})
+
+       types := DefaultTypes
+
+       // Issue #8406
+       geoJSON := newMediaTypeWithMimeSuffix("application", "geo", "json", []string{"geojson", "gjson"})
+       types = append(types, geoJSON)
+       sort.Sort(types)
+
+       check := func(suffix string, expectedType Type) {
+               t, f, found := types.GetFirstBySuffix(suffix)
+               c.Assert(found, qt.Equals, true)
+               c.Assert(f, qt.Equals, SuffixInfo{
+                       Suffix:     suffix,
+                       FullSuffix: "." + suffix})
+               c.Assert(t, qt.Equals, expectedType)
+       }
+
+       check("js", JavascriptType)
+       check("json", JSONType)
+       check("geojson", geoJSON)
+       check("gjson", geoJSON)
+
 }
 
 func TestFromTypeString(t *testing.T) {