github.com/jmooring/hugo@v0.47.1/media/mediaType_test.go (about)

     1  // Copyright 2017-present 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  	"testing"
    18  
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  func TestDefaultTypes(t *testing.T) {
    23  	for _, test := range []struct {
    24  		tp               Type
    25  		expectedMainType string
    26  		expectedSubType  string
    27  		expectedSuffix   string
    28  		expectedType     string
    29  		expectedString   string
    30  	}{
    31  		{CalendarType, "text", "calendar", "ics", "text/calendar", "text/calendar"},
    32  		{CSSType, "text", "css", "css", "text/css", "text/css"},
    33  		{SCSSType, "text", "x-scss", "scss", "text/x-scss", "text/x-scss"},
    34  		{CSVType, "text", "csv", "csv", "text/csv", "text/csv"},
    35  		{HTMLType, "text", "html", "html", "text/html", "text/html"},
    36  		{JavascriptType, "application", "javascript", "js", "application/javascript", "application/javascript"},
    37  		{JSONType, "application", "json", "json", "application/json", "application/json"},
    38  		{RSSType, "application", "rss", "xml", "application/rss+xml", "application/rss+xml"},
    39  		{SVGType, "image", "svg", "svg", "image/svg+xml", "image/svg+xml"},
    40  		{TextType, "text", "plain", "txt", "text/plain", "text/plain"},
    41  		{XMLType, "application", "xml", "xml", "application/xml", "application/xml"},
    42  	} {
    43  		require.Equal(t, test.expectedMainType, test.tp.MainType)
    44  		require.Equal(t, test.expectedSubType, test.tp.SubType)
    45  		require.Equal(t, test.expectedSuffix, test.tp.Suffix(), test.tp.String())
    46  		require.Equal(t, defaultDelimiter, test.tp.Delimiter)
    47  
    48  		require.Equal(t, test.expectedType, test.tp.Type())
    49  		require.Equal(t, test.expectedString, test.tp.String())
    50  
    51  	}
    52  
    53  }
    54  
    55  func TestGetByType(t *testing.T) {
    56  	types := Types{HTMLType, RSSType}
    57  
    58  	mt, found := types.GetByType("text/HTML")
    59  	require.True(t, found)
    60  	require.Equal(t, mt, HTMLType)
    61  
    62  	_, found = types.GetByType("text/nono")
    63  	require.False(t, found)
    64  
    65  	mt, found = types.GetByType("application/rss+xml")
    66  	require.True(t, found)
    67  	require.Equal(t, mt, RSSType)
    68  
    69  	mt, found = types.GetByType("application/rss")
    70  	require.True(t, found)
    71  	require.Equal(t, mt, RSSType)
    72  }
    73  
    74  func TestGetByMainSubType(t *testing.T) {
    75  	assert := require.New(t)
    76  	f, found := DefaultTypes.GetByMainSubType("text", "plain")
    77  	assert.True(found)
    78  	assert.Equal(f, TextType)
    79  	_, found = DefaultTypes.GetByMainSubType("foo", "plain")
    80  	assert.False(found)
    81  }
    82  
    83  func TestGetFirstBySuffix(t *testing.T) {
    84  	assert := require.New(t)
    85  	f, found := DefaultTypes.GetFirstBySuffix("xml")
    86  	assert.True(found)
    87  	assert.Equal(Type{MainType: "application", SubType: "rss", OldSuffix: "xml", Delimiter: ".", Suffixes: []string{"xml"}, fileSuffix: "xml"}, f)
    88  }
    89  
    90  func TestFromTypeString(t *testing.T) {
    91  	f, err := fromString("text/html")
    92  	require.NoError(t, err)
    93  	require.Equal(t, HTMLType.Type(), f.Type())
    94  
    95  	f, err = fromString("application/custom")
    96  	require.NoError(t, err)
    97  	require.Equal(t, Type{MainType: "application", SubType: "custom", OldSuffix: "", fileSuffix: ""}, f)
    98  
    99  	f, err = fromString("application/custom+sfx")
   100  	require.NoError(t, err)
   101  	require.Equal(t, Type{MainType: "application", SubType: "custom", OldSuffix: "sfx"}, f)
   102  
   103  	_, err = fromString("noslash")
   104  	require.Error(t, err)
   105  
   106  	f, err = fromString("text/xml; charset=utf-8")
   107  	require.NoError(t, err)
   108  	require.Equal(t, Type{MainType: "text", SubType: "xml", OldSuffix: ""}, f)
   109  	require.Equal(t, "", f.Suffix())
   110  }
   111  
   112  // Add a test for the SVG case
   113  // https://github.com/gohugoio/hugo/issues/4920
   114  func TestFromExtensionMultipleSuffixes(t *testing.T) {
   115  	assert := require.New(t)
   116  	tp, found := DefaultTypes.GetBySuffix("svg")
   117  	assert.True(found)
   118  	assert.Equal("image/svg+xml", tp.String())
   119  	assert.Equal("svg", tp.fileSuffix)
   120  	assert.Equal(".svg", tp.FullSuffix())
   121  	tp, found = DefaultTypes.GetByType("image/svg+xml")
   122  	assert.True(found)
   123  	assert.Equal("image/svg+xml", tp.String())
   124  	assert.True(found)
   125  	assert.Equal(".svg", tp.FullSuffix())
   126  
   127  }
   128  
   129  func TestDecodeTypes(t *testing.T) {
   130  
   131  	var tests = []struct {
   132  		name        string
   133  		maps        []map[string]interface{}
   134  		shouldError bool
   135  		assert      func(t *testing.T, name string, tt Types)
   136  	}{
   137  		{
   138  			"Redefine JSON",
   139  			[]map[string]interface{}{
   140  				{
   141  					"application/json": map[string]interface{}{
   142  						"suffixes": []string{"jasn"}}}},
   143  			false,
   144  			func(t *testing.T, name string, tt Types) {
   145  				require.Len(t, tt, len(DefaultTypes))
   146  				json, found := tt.GetBySuffix("jasn")
   147  				require.True(t, found)
   148  				require.Equal(t, "application/json", json.String(), name)
   149  			}},
   150  		{
   151  			"Suffix from key, multiple file suffixes",
   152  			[]map[string]interface{}{
   153  				{
   154  					"application/hugo+hg": map[string]interface{}{
   155  						"Suffixes": []string{"hg1", "hg2"},
   156  					}}},
   157  			false,
   158  			func(t *testing.T, name string, tt Types) {
   159  				require.Len(t, tt, len(DefaultTypes)+1)
   160  				hg, found := tt.GetBySuffix("hg")
   161  				require.True(t, found)
   162  				require.Equal(t, "hg", hg.OldSuffix)
   163  				require.Equal(t, "hg", hg.Suffix())
   164  				require.Equal(t, ".hg", hg.FullSuffix())
   165  				require.Equal(t, "application/hugo+hg", hg.String(), name)
   166  				hg, found = tt.GetBySuffix("hg2")
   167  				require.True(t, found)
   168  				require.Equal(t, "hg", hg.OldSuffix)
   169  				require.Equal(t, "hg2", hg.Suffix())
   170  				require.Equal(t, ".hg2", hg.FullSuffix())
   171  				require.Equal(t, "application/hugo+hg", hg.String(), name)
   172  
   173  				hg, found = tt.GetByType("application/hugo+hg")
   174  				require.True(t, found)
   175  
   176  			}},
   177  		{
   178  			"Add custom media type",
   179  			[]map[string]interface{}{
   180  				{
   181  					"text/hugo": map[string]interface{}{
   182  						"suffix": "hgo"}}},
   183  			false,
   184  			func(t *testing.T, name string, tt Types) {
   185  				require.Len(t, tt, len(DefaultTypes)+1)
   186  				// Make sure we have not broken the default config.
   187  
   188  				_, found := tt.GetBySuffix("json")
   189  				require.True(t, found)
   190  
   191  				hugo, found := tt.GetBySuffix("hgo")
   192  				require.True(t, found)
   193  				require.Equal(t, "text/hugo+hgo", hugo.String(), name)
   194  			}},
   195  	}
   196  
   197  	for _, test := range tests {
   198  		result, err := DecodeTypes(test.maps...)
   199  		if test.shouldError {
   200  			require.Error(t, err, test.name)
   201  		} else {
   202  			require.NoError(t, err, test.name)
   203  			test.assert(t, test.name, result)
   204  		}
   205  	}
   206  }