Skip to content

Commit

Permalink
Regression in media type suffix lookup
Browse files Browse the repository at this point in the history
Introduced in Hugo 0.82.0.

Fixes #8406
  • Loading branch information
bep committed Apr 20, 2021
1 parent 9d96078 commit 6e9d2bf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion media/mediaType.go
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down
28 changes: 23 additions & 5 deletions media/mediaType_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package media

import (
"encoding/json"
"sort"
"testing"

qt "github.com/frankban/quicktest"
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6e9d2bf

Please sign in to comment.