diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-08-28 14:18:12 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-08-28 17:00:53 +0200 |
commit | ebb56e8bdbfaf4f955326017e40b2805850871e9 (patch) | |
tree | 64935a5bfaeb5b2cc8e5c600ad2403d32d3de65c /media/mediaType_test.go | |
parent | 6b9934a26615ea614b1774770532cae9762a58d3 (diff) |
Improve minifier MIME type resolution
This commit also removes the deprecated `Suffix` from MediaType. Now use `Suffixes` and put the MIME type suffix in the type, e.g. `application/svg+xml`.
Fixes #5093
Diffstat (limited to 'media/mediaType_test.go')
-rw-r--r-- | media/mediaType_test.go | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/media/mediaType_test.go b/media/mediaType_test.go index 6385528ee..bf356582f 100644 --- a/media/mediaType_test.go +++ b/media/mediaType_test.go @@ -80,11 +80,19 @@ func TestGetByMainSubType(t *testing.T) { assert.False(found) } +func TestBySuffix(t *testing.T) { + assert := require.New(t) + formats := DefaultTypes.BySuffix("xml") + assert.Equal(2, len(formats)) + assert.Equal("rss", formats[0].SubType) + assert.Equal("xml", formats[1].SubType) +} + func TestGetFirstBySuffix(t *testing.T) { assert := require.New(t) f, found := DefaultTypes.GetFirstBySuffix("xml") assert.True(found) - assert.Equal(Type{MainType: "application", SubType: "rss", OldSuffix: "xml", Delimiter: ".", Suffixes: []string{"xml"}, fileSuffix: "xml"}, f) + assert.Equal(Type{MainType: "application", SubType: "rss", mimeSuffix: "xml", Delimiter: ".", Suffixes: []string{"xml"}, fileSuffix: "xml"}, f) } func TestFromTypeString(t *testing.T) { @@ -94,18 +102,18 @@ func TestFromTypeString(t *testing.T) { f, err = fromString("application/custom") require.NoError(t, err) - require.Equal(t, Type{MainType: "application", SubType: "custom", OldSuffix: "", fileSuffix: ""}, f) + require.Equal(t, Type{MainType: "application", SubType: "custom", mimeSuffix: "", fileSuffix: ""}, f) f, err = fromString("application/custom+sfx") require.NoError(t, err) - require.Equal(t, Type{MainType: "application", SubType: "custom", OldSuffix: "sfx"}, f) + require.Equal(t, Type{MainType: "application", SubType: "custom", mimeSuffix: "sfx"}, f) _, err = fromString("noslash") require.Error(t, err) f, err = fromString("text/xml; charset=utf-8") require.NoError(t, err) - require.Equal(t, Type{MainType: "text", SubType: "xml", OldSuffix: ""}, f) + require.Equal(t, Type{MainType: "text", SubType: "xml", mimeSuffix: ""}, f) require.Equal(t, "", f.Suffix()) } @@ -146,28 +154,24 @@ func TestDecodeTypes(t *testing.T) { json, found := tt.GetBySuffix("jasn") require.True(t, found) require.Equal(t, "application/json", json.String(), name) + require.Equal(t, ".jasn", json.FullSuffix()) }}, { - "Suffix from key, multiple file suffixes", + "MIME suffix in key, multiple file suffixes, custom delimiter", []map[string]interface{}{ { "application/hugo+hg": map[string]interface{}{ - "Suffixes": []string{"hg1", "hg2"}, + "suffixes": []string{"hg1", "hg2"}, + "Delimiter": "_", }}}, 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") + hg, found := tt.GetBySuffix("hg2") require.True(t, found) - require.Equal(t, "hg", hg.OldSuffix) + require.Equal(t, "hg", hg.mimeSuffix) require.Equal(t, "hg2", hg.Suffix()) - require.Equal(t, ".hg2", hg.FullSuffix()) + require.Equal(t, "_hg2", hg.FullSuffix()) require.Equal(t, "application/hugo+hg", hg.String(), name) hg, found = tt.GetByType("application/hugo+hg") @@ -178,8 +182,8 @@ func TestDecodeTypes(t *testing.T) { "Add custom media type", []map[string]interface{}{ { - "text/hugo": map[string]interface{}{ - "suffix": "hgo"}}}, + "text/hugo+hgo": map[string]interface{}{ + "Suffixes": []string{"hgo2"}}}}, false, func(t *testing.T, name string, tt Types) { require.Len(t, tt, len(DefaultTypes)+1) @@ -188,7 +192,7 @@ func TestDecodeTypes(t *testing.T) { _, found := tt.GetBySuffix("json") require.True(t, found) - hugo, found := tt.GetBySuffix("hgo") + hugo, found := tt.GetBySuffix("hgo2") require.True(t, found) require.Equal(t, "text/hugo+hgo", hugo.String(), name) }}, |