diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-04-03 22:39:37 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-04-04 15:12:30 +0200 |
commit | f8d555cca59a48df9cde2e7323ff2d500e0590a3 (patch) | |
tree | d11b02e24b1c75ad48deaf9e016a66298222d4c7 /media/mediaType_test.go | |
parent | c9aee467d387c4c3489c23f120a7ef2fed4d12df (diff) |
media: Add DecodeTypes
And clean up the media package.
Diffstat (limited to 'media/mediaType_test.go')
-rw-r--r-- | media/mediaType_test.go | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/media/mediaType_test.go b/media/mediaType_test.go index c97ac782a..8d83c19f8 100644 --- a/media/mediaType_test.go +++ b/media/mediaType_test.go @@ -58,3 +58,81 @@ func TestGetByType(t *testing.T) { _, found = types.GetByType("text/nono") require.False(t, found) } + +func TestFromTypeString(t *testing.T) { + f, err := FromString("text/html") + require.NoError(t, err) + require.Equal(t, HTMLType, f) + + f, err = FromString("application/custom") + require.NoError(t, err) + require.Equal(t, Type{MainType: "application", SubType: "custom", Suffix: "custom"}, f) + + f, err = FromString("application/custom+pdf") + require.NoError(t, err) + require.Equal(t, Type{MainType: "application", SubType: "custom", Suffix: "pdf"}, f) + + f, err = FromString("noslash") + require.Error(t, err) + +} + +func TestDecodeTypes(t *testing.T) { + + var tests = []struct { + name string + maps []map[string]interface{} + shouldError bool + assert func(t *testing.T, name string, tt Types) + }{ + { + "Redefine JSON", + []map[string]interface{}{ + map[string]interface{}{ + "application/json": map[string]interface{}{ + "suffix": "jsn"}}}, + false, + func(t *testing.T, name string, tt Types) { + require.Len(t, tt, len(DefaultTypes)) + json, found := tt.GetBySuffix("jsn") + require.True(t, found) + require.Equal(t, "application/json+jsn", json.String(), name) + }}, + { + "Add custom media type", + []map[string]interface{}{ + map[string]interface{}{ + "text/hugo": map[string]interface{}{ + "suffix": "hgo"}}}, + false, + 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) + + hugo, found := tt.GetBySuffix("hgo") + require.True(t, found) + require.Equal(t, "text/hugo+hgo", hugo.String(), name) + }}, + { + "Add media type invalid key", + []map[string]interface{}{ + map[string]interface{}{ + "text/hugo+hgo": map[string]interface{}{}}}, + true, + func(t *testing.T, name string, tt Types) { + + }}, + } + + for _, test := range tests { + result, err := DecodeTypes(test.maps...) + if test.shouldError { + require.Error(t, err, test.name) + } else { + require.NoError(t, err, test.name) + test.assert(t, test.name, result) + } + } +} |