1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
package openapi3
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestContent_Get(t *testing.T) {
fallback := NewMediaType()
wildcard := NewMediaType()
stripped := NewMediaType()
fullMatch := NewMediaType()
content := Content{
"*/*": fallback,
"application/*": wildcard,
"application/json": stripped,
"application/json;encoding=utf-8": fullMatch,
}
contentWithoutWildcards := Content{
"application/json": stripped,
"application/json;encoding=utf-8": fullMatch,
}
tests := []struct {
name string
content Content
mime string
want *MediaType
}{
{
name: "missing",
content: contentWithoutWildcards,
mime: "text/plain;encoding=utf-8",
want: nil,
},
{
name: "full match",
content: content,
mime: "application/json;encoding=utf-8",
want: fullMatch,
},
{
name: "stripped match",
content: content,
mime: "application/json;encoding=utf-16",
want: stripped,
},
{
name: "wildcard match",
content: content,
mime: "application/yaml;encoding=utf-16",
want: wildcard,
},
{
name: "fallback match",
content: content,
mime: "text/plain;encoding=utf-16",
want: fallback,
},
{
name: "invalid mime type",
content: content,
mime: "text;encoding=utf16",
want: nil,
},
{
name: "missing no encoding",
content: contentWithoutWildcards,
mime: "text/plain",
want: nil,
},
{
name: "stripped match no encoding",
content: content,
mime: "application/json",
want: stripped,
},
{
name: "wildcard match no encoding",
content: content,
mime: "application/yaml",
want: wildcard,
},
{
name: "fallback match no encoding",
content: content,
mime: "text/plain",
want: fallback,
},
{
name: "invalid mime type no encoding",
content: content,
mime: "text",
want: nil,
},
{
name: "missing mime type",
content: content,
mime: "",
want: fallback,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.content.Get(tt.mime)
require.Same(t, tt.want, got)
})
}
}
|