initial commits
commit
76045c518f
|
@ -0,0 +1,196 @@
|
|||
package mediarange
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
errBadMediaRange = errors.New("bad media-range")
|
||||
)
|
||||
|
||||
// MediaRange represents a "media-range" as defined by IETF RFC-2616 (Hypertext Transfer Protocol — HTTP/1.1) in section 14.1.
|
||||
//
|
||||
// Here are some sample media-ranges:
|
||||
//
|
||||
// */*
|
||||
// audio/*
|
||||
// image/*
|
||||
// text/*
|
||||
// video/*
|
||||
// application/x-tar
|
||||
// audio/wav
|
||||
// image/gif
|
||||
// image/png
|
||||
// text/html
|
||||
// text/plain
|
||||
// video/mp4
|
||||
//
|
||||
// Note that some of these sample media-ranges look like media-types / mime-types:
|
||||
//
|
||||
// application/x-tar
|
||||
// audio/wav
|
||||
// image/gif
|
||||
// image/png
|
||||
// text/html
|
||||
// text/plain
|
||||
// video/mp4
|
||||
//
|
||||
// But some of these sample media-ranges are not media-types / mime-types:
|
||||
//
|
||||
// */*
|
||||
// audio/*
|
||||
// image/*
|
||||
// text/*
|
||||
// video/*
|
||||
//
|
||||
// These later kinds of media-ranges (that do not look like media-types / mime-types) use the asterisk ("*") as a wild-card symbol.
|
||||
// So "*/*" represents any media-type / mime-type.
|
||||
// ANd "image/*" represents "image/gif", "image/png", etc.
|
||||
type MediaRange struct {
|
||||
typ string
|
||||
subtyp string
|
||||
}
|
||||
|
||||
// Create creates a MediaRange.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// // audio/ogg
|
||||
// mediarange.Create("audio", "ogg")
|
||||
//
|
||||
// // audio/*
|
||||
// mediarange.Create("audio", "*")
|
||||
//
|
||||
// // */*
|
||||
// mediarange.Create("*", "*")
|
||||
//
|
||||
// Note that an empty string for mediaRangeType or mediaRangeSubType will be treated as a wild-card (i.e., "*").
|
||||
func Create(mediaRangeType string, mediaRangeSubType string) MediaRange {
|
||||
mediaRangeType = strings.TrimSpace(mediaRangeType)
|
||||
mediaRangeSubType = strings.TrimSpace(mediaRangeSubType)
|
||||
|
||||
if "*" == mediaRangeType {
|
||||
mediaRangeType = ""
|
||||
}
|
||||
if "*" == mediaRangeSubType {
|
||||
mediaRangeSubType = ""
|
||||
}
|
||||
|
||||
return MediaRange{
|
||||
typ: strings.ToLower(mediaRangeType),
|
||||
subtyp: strings.ToLower(mediaRangeSubType),
|
||||
}
|
||||
}
|
||||
|
||||
// Parse parses a string and returns a MediaRange.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// mr, err := mediarange.Parse("application/activity+json")
|
||||
func Parse(value string) (MediaRange, error) {
|
||||
|
||||
value = strings.TrimSpace(value)
|
||||
|
||||
parts := strings.SplitN(value, "/", 2)
|
||||
if 2 != len(parts) {
|
||||
return MediaRange{}, errBadMediaRange
|
||||
}
|
||||
|
||||
var typ string = parts[0]
|
||||
|
||||
var subtyp string
|
||||
{
|
||||
a := strings.SplitN(parts[1], ";", 2)
|
||||
{
|
||||
length := len(a)
|
||||
if 2 != length && 1 != length {
|
||||
return MediaRange{}, errBadMediaRange
|
||||
}
|
||||
}
|
||||
|
||||
subtyp = a[0]
|
||||
}
|
||||
|
||||
return Create(typ, subtyp), nil
|
||||
}
|
||||
|
||||
// SubType returns the subtype of the media-range in its canonical form — i.e. lower-case and without spacing.
|
||||
func (receiver MediaRange) SubType() string {
|
||||
var typ string = receiver.typ
|
||||
var subtyp string = receiver.subtyp
|
||||
|
||||
if "" == typ || "*" == typ {
|
||||
return "*"
|
||||
}
|
||||
|
||||
if "" == subtyp {
|
||||
return "*"
|
||||
}
|
||||
|
||||
return subtyp
|
||||
}
|
||||
|
||||
// Type returns the type of the media-range in its canonical form — i.e. lower-case and without spacing.
|
||||
func (receiver MediaRange) Type() string {
|
||||
var typ string = receiver.typ
|
||||
|
||||
if "" == typ {
|
||||
return "*"
|
||||
}
|
||||
|
||||
return typ
|
||||
}
|
||||
|
||||
func (receiver MediaRange) Match(value string) bool {
|
||||
|
||||
var typ string = receiver.Type()
|
||||
if "*" == typ {
|
||||
return true
|
||||
}
|
||||
|
||||
// Since media-types are a sub-set of media-ranges, we can do this.
|
||||
var mediatype MediaRange
|
||||
{
|
||||
var err error
|
||||
|
||||
mediatype, err = Parse(value)
|
||||
if nil != err {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
if mediatype.typ != typ {
|
||||
return false
|
||||
}
|
||||
|
||||
var subtyp string = receiver.SubType()
|
||||
if "*" == subtyp {
|
||||
return true
|
||||
}
|
||||
|
||||
if mediatype.subtyp != subtyp {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// String returns the serialed media-range.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// "text/html" == mediarange.Create("text", "html").String()
|
||||
func (receiver MediaRange) String() string {
|
||||
var p []byte
|
||||
|
||||
var typ string = receiver.Type()
|
||||
p = append(p, typ...)
|
||||
|
||||
p = append(p, '/')
|
||||
|
||||
var subtyp string = receiver.SubType()
|
||||
p = append(p, subtyp...)
|
||||
|
||||
return string(p)
|
||||
}
|
|
@ -0,0 +1,594 @@
|
|||
package mediarange_test
|
||||
|
||||
import (
|
||||
"sourcecode.social/reiver/go-httpaccept/mediarange"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMediaRange_Match(t *testing.T) {
|
||||
|
||||
tests := []struct{
|
||||
MediaType string
|
||||
MediaRange mediarange.MediaRange
|
||||
Expected bool
|
||||
}{
|
||||
{
|
||||
MediaType: "application/activity+json",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/epub+zip",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/gzip",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/json",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/pdf",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/msword",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/octet-stream",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/ogg",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/rtf",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/xhtml+xml",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/x-httpd-php",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/X-httpd-PHP",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "image/gif",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "image/png",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "Image/png",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "image/PnG",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "IMAGE/PNG",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "text/html",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "TEXT/html",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "text/plain",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "TEXT/PLAIN",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "xyz/banana",
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: true,
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaType: "application/activity+json",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/epub+zip",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/gzip",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/json",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/pdf",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/msword",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/octet-stream",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/ogg",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/rtf",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/xhtml+xml",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/x-httpd-php",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/X-httpd-PHP",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "image/gif",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "image/png",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "Image/png",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "image/PnG",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "IMAGE/PNG",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "text/html",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "TEXT/html",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "text/plain",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "TEXT/PLAIN",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "xyz/banana",
|
||||
MediaRange: mediarange.Create("*", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaType: "application/activity+json",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/epub+zip",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/gzip",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/json",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/pdf",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/msword",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/octet-stream",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/ogg",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/rtf",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/xhtml+xml",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/x-httpd-php",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "application/X-httpd-PHP",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "image/gif",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "image/png",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "Image/png",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "image/PnG",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "IMAGE/PNG",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "text/html",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "TEXT/html",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "text/plain",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "TEXT/PLAIN",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "xyz/banana",
|
||||
MediaRange: mediarange.Create("application", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaType: "application/activity+json",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/epub+zip",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/gzip",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/json",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/pdf",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/msword",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/octet-stream",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/ogg",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/rtf",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/xhtml+xml",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/x-httpd-php",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/X-httpd-PHP",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "image/gif",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "image/png",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "Image/png",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "image/PnG",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "IMAGE/PNG",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "text/html",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "TEXT/html",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "text/plain",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "TEXT/PLAIN",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "xyz/banana",
|
||||
MediaRange: mediarange.Create("image", "*"),
|
||||
Expected: false,
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaType: "application/activity+json",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/epub+zip",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/gzip",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/json",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/pdf",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/msword",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/octet-stream",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/ogg",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/rtf",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/xhtml+xml",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/x-httpd-php",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "application/X-httpd-PHP",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "image/gif",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "image/png",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "Image/png",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "image/PnG",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "IMAGE/PNG",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: true,
|
||||
},
|
||||
{
|
||||
MediaType: "text/html",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "TEXT/html",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "text/plain",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "TEXT/PLAIN",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
{
|
||||
MediaType: "xyz/banana",
|
||||
MediaRange: mediarange.Create("image", "png"),
|
||||
Expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for testNumber, test := range tests {
|
||||
|
||||
actual := test.MediaRange.Match(test.MediaType)
|
||||
expected := test.Expected
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("For test #%d, the actual result from trying to match the media-type against the media-range is not what was expected.", testNumber)
|
||||
t.Logf("MEDIA-TYPE: %q", test.MediaType)
|
||||
t.Logf("MEDIA-RANGE: %q", test.MediaRange)
|
||||
t.Logf("EXPECTED: %t", expected)
|
||||
t.Logf("ACTUAL: %t", actual)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,286 @@
|
|||
package mediarange_test
|
||||
|
||||
import (
|
||||
"sourcecode.social/reiver/go-httpaccept/mediarange"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
|
||||
tests := []struct{
|
||||
MediaRange string
|
||||
Expected mediarange.MediaRange
|
||||
}{
|
||||
{
|
||||
MediaRange: "*/*",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: "*/*;q=0",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/*;q=0.0",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/*;q=0.5",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/*;q=1",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/*;q=1.0",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: "*/* ;q=0",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/* ;q=0.0",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/* ;q=0.5",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/* ;q=1",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/* ;q=1.0",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: "*/*; q=0",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/*; q=0.0",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/*; q=0.5",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/*; q=1",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/*; q=1.0",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: "*/* ; q=0",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/* ; q=0.0",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/* ; q=0.5",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/* ; q=1",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/* ; q=1.0",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: "*/*;q=0;once=1;twice=22;thrice=333;fource=4444",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/*;q=0.0;once=1;twice=22;thrice=333;fource=4444",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/*;q=0.5;once=1;twice=22;thrice=333;fource=4444",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/*;q=1;once=1;twice=22;thrice=333;fource=4444",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "*/*;q=1.0;once=1;twice=22;thrice=333;fource=4444",
|
||||
Expected: mediarange.Create("*", "*"),
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: "application/*",
|
||||
Expected: mediarange.Create("application", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "audio/*",
|
||||
Expected: mediarange.Create("audio", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "font/*",
|
||||
Expected: mediarange.Create("font", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "image/*",
|
||||
Expected: mediarange.Create("image", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "text/*",
|
||||
Expected: mediarange.Create("text", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "video/*",
|
||||
Expected: mediarange.Create("video", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "xyz/*",
|
||||
Expected: mediarange.Create("xyz", "*"),
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: "Application/*",
|
||||
Expected: mediarange.Create("application", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "audIO/*",
|
||||
Expected: mediarange.Create("audio", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "FonT/*",
|
||||
Expected: mediarange.Create("font", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "IMAGE/*",
|
||||
Expected: mediarange.Create("image", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "tEXT/*",
|
||||
Expected: mediarange.Create("text", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "vIDeo/*",
|
||||
Expected: mediarange.Create("video", "*"),
|
||||
},
|
||||
{
|
||||
MediaRange: "Xyz/*",
|
||||
Expected: mediarange.Create("xyz", "*"),
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: "application/XHTML+XML",
|
||||
Expected: mediarange.Create("application", "xhtml+xml"),
|
||||
},
|
||||
{
|
||||
MediaRange: "IMAGE/png",
|
||||
Expected: mediarange.Create("image", "png"),
|
||||
},
|
||||
{
|
||||
MediaRange: "text/html",
|
||||
Expected: mediarange.Create("text", "html"),
|
||||
},
|
||||
{
|
||||
MediaRange: "Text/html",
|
||||
Expected: mediarange.Create("text", "html"),
|
||||
},
|
||||
{
|
||||
MediaRange: "TexT/html",
|
||||
Expected: mediarange.Create("text", "html"),
|
||||
},
|
||||
{
|
||||
MediaRange: "tExt/html",
|
||||
Expected: mediarange.Create("text", "html"),
|
||||
},
|
||||
{
|
||||
MediaRange: "teXt/html",
|
||||
Expected: mediarange.Create("text", "html"),
|
||||
},
|
||||
{
|
||||
MediaRange: "texT/html",
|
||||
Expected: mediarange.Create("text", "html"),
|
||||
},
|
||||
{
|
||||
MediaRange: "text/Html",
|
||||
Expected: mediarange.Create("text", "html"),
|
||||
},
|
||||
{
|
||||
MediaRange: "text/hTml",
|
||||
Expected: mediarange.Create("text", "html"),
|
||||
},
|
||||
{
|
||||
MediaRange: "text/htMl",
|
||||
Expected: mediarange.Create("text", "html"),
|
||||
},
|
||||
{
|
||||
MediaRange: "text/htmL",
|
||||
Expected: mediarange.Create("text", "html"),
|
||||
},
|
||||
{
|
||||
MediaRange: "TeXT/HTml",
|
||||
Expected: mediarange.Create("text", "html"),
|
||||
},
|
||||
{
|
||||
MediaRange: "TEXT/html",
|
||||
Expected: mediarange.Create("text", "html"),
|
||||
},
|
||||
{
|
||||
MediaRange: "text/HTML",
|
||||
Expected: mediarange.Create("text", "html"),
|
||||
},
|
||||
{
|
||||
MediaRange: "TEXT/HTML",
|
||||
Expected: mediarange.Create("text", "html"),
|
||||
},
|
||||
}
|
||||
|
||||
for testNumber, test := range tests {
|
||||
|
||||
actual, err := mediarange.Parse(test.MediaRange)
|
||||
if nil != err {
|
||||
t.Errorf("For test #%d, did not expect an error but actually got one.", testNumber)
|
||||
t.Logf("MEDIA-RANGE: %q", test.MediaRange)
|
||||
t.Logf("ERROR: (%T) %s", err, err)
|
||||
continue
|
||||
}
|
||||
|
||||
expected := test.Expected
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("For tset #%d, the actual resulting media-range is not what was expected.", testNumber)
|
||||
t.Logf("MEDIA-RANGE: %q", test.MediaRange)
|
||||
t.Logf("EXPECTED: %q %#v", expected, expected)
|
||||
t.Logf("ACTUAL: %q %#v", actual, actual)
|
||||
continue
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,434 @@
|
|||
package mediarange_test
|
||||
|
||||
import (
|
||||
"sourcecode.social/reiver/go-httpaccept/mediarange"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMediaRange_String(t *testing.T) {
|
||||
|
||||
tests := []struct{
|
||||
MediaRange mediarange.MediaRange
|
||||
Expected string
|
||||
}{
|
||||
{
|
||||
MediaRange: mediarange.Create("", ""),
|
||||
Expected: "*"+"/"+"*",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("*" , "*"),
|
||||
Expected: "*"+"/"+"*",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , ""),
|
||||
Expected: "application"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , ""),
|
||||
Expected: "audio"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("font" , ""),
|
||||
Expected: "font"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , ""),
|
||||
Expected: "image"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , ""),
|
||||
Expected: "text"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , ""),
|
||||
Expected: "video"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("xyz" , ""),
|
||||
Expected: "xyz"+"/"+"*",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "*"),
|
||||
Expected: "application"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "*"),
|
||||
Expected: "audio"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("font" , "*"),
|
||||
Expected: "font"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "*"),
|
||||
Expected: "image"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , ""),
|
||||
Expected: "text"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , ""),
|
||||
Expected: "video"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("xyz" , ""),
|
||||
Expected: "xyz"+"/"+"*",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("Application" , "*"),
|
||||
Expected: "application"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Audio" , "*"),
|
||||
Expected: "audio"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Font" , "*"),
|
||||
Expected: "font"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Image" , "*"),
|
||||
Expected: "image"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Text" , ""),
|
||||
Expected: "text"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Video" , ""),
|
||||
Expected: "video"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Xyz" , ""),
|
||||
Expected: "xyz"+"/"+"*",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("APPLICATION" , "*"),
|
||||
Expected: "application"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("AUDIO" , "*"),
|
||||
Expected: "audio"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("FONT" , "*"),
|
||||
Expected: "font"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("IMAGE" , "*"),
|
||||
Expected: "image"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("TEXT" , ""),
|
||||
Expected: "text"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("VIDEO" , ""),
|
||||
Expected: "video"+"/"+"*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("XYZ" , ""),
|
||||
Expected: "xyz"+"/"+"*",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "activity+json"),
|
||||
Expected: "application"+"/"+"activity+json",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "epub+zip"),
|
||||
Expected: "application"+"/"+"epub+zip",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "gzip"),
|
||||
Expected: "application"+"/"+"gzip",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "java-archive"),
|
||||
Expected: "application"+"/"+"java-archive",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "json"),
|
||||
Expected: "application"+"/"+"json",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "ld+json"),
|
||||
Expected: "application"+"/"+"ld+json",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "pdf"),
|
||||
Expected: "application"+"/"+"pdf",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "msword"),
|
||||
Expected: "application"+"/"+"msword",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "octet-stream"),
|
||||
Expected: "application"+"/"+"octet-stream",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "ogg"),
|
||||
Expected: "application"+"/"+"ogg",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "rtf"),
|
||||
Expected: "application"+"/"+"rtf",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "vnd.oasis.opendocument.spreadsheet"),
|
||||
Expected: "application"+"/"+"vnd.oasis.opendocument.spreadsheet",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "vnd.openxmlformats-officedocument.wordprocessingml.document"),
|
||||
Expected: "application"+"/"+"vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "vnd.mozilla.xul+xml"),
|
||||
Expected: "application"+"/"+"vnd.mozilla.xul+xml",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "vnd.ms-excel"),
|
||||
Expected: "application"+"/"+"vnd.ms-excel",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-7z-compressed"),
|
||||
Expected: "application"+"/"+"x-7z-compressed",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-bzip"),
|
||||
Expected: "application"+"/"+"x-bzip",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-bzip2"),
|
||||
Expected: "application"+"/"+"x-bzip2",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-cdf"),
|
||||
Expected: "application"+"/"+"x-cdf",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-csh"),
|
||||
Expected: "application"+"/"+"x-csh",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-freearc"),
|
||||
Expected: "application"+"/"+"x-freearc",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-httpd-php"),
|
||||
Expected: "application"+"/"+"x-httpd-php",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-sh"),
|
||||
Expected: "application"+"/"+"x-sh",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-tar"),
|
||||
Expected: "application"+"/"+"x-tar",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "xhtml+xml"),
|
||||
Expected: "application"+"/"+"xhtml+xml",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "aac"),
|
||||
Expected: "audio"+"/"+"aac",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "midi"),
|
||||
Expected: "audio"+"/"+"midi",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "mpeg"),
|
||||
Expected: "audio"+"/"+"mpeg",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "ogg"),
|
||||
Expected: "audio"+"/"+"ogg",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "opus"),
|
||||
Expected: "audio"+"/"+"opus",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "wav"),
|
||||
Expected: "audio"+"/"+"wav",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "webm"),
|
||||
Expected: "audio"+"/"+"webm",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "3gpp"),
|
||||
Expected: "audio"+"/"+"3gpp",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "3gpp2"),
|
||||
Expected: "audio"+"/"+"3gpp2",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "x-midi"),
|
||||
Expected: "audio"+"/"+"x-midi",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("font" , "ttf"),
|
||||
Expected: "font"+"/"+"ttf",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "avif"),
|
||||
Expected: "image"+"/"+"avif",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "bmp"),
|
||||
Expected: "image"+"/"+"bmp",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "gif"),
|
||||
Expected: "image"+"/"+"gif",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "png"),
|
||||
Expected: "image"+"/"+"png",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "tiff"),
|
||||
Expected: "image"+"/"+"tiff",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "css"),
|
||||
Expected: "text"+"/"+"css",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "csv"),
|
||||
Expected: "text"+"/"+"csv",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "html"),
|
||||
Expected: "text"+"/"+"html",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "javascript"),
|
||||
Expected: "text"+"/"+"javascript",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "markdown"),
|
||||
Expected: "text"+"/"+"markdown",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "plain"),
|
||||
Expected: "text"+"/"+"plain",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "x-markdown"),
|
||||
Expected: "text"+"/"+"x-markdown",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "3gpp2"),
|
||||
Expected: "video"+"/"+"3gpp2",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "ogg"),
|
||||
Expected: "video"+"/"+"ogg",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "mp2t"),
|
||||
Expected: "video"+"/"+"mp2t",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "mp4"),
|
||||
Expected: "video"+"/"+"mp4",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "mpeg"),
|
||||
Expected: "video"+"/"+"mpeg",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "webm"),
|
||||
Expected: "video"+"/"+"webm",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "x-msvideo"),
|
||||
Expected: "video"+"/"+"x-msvideo",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "ACTIVITY+JSON"),
|
||||
Expected: "application"+"/"+"activity+json",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "MPEG"),
|
||||
Expected: "audio"+"/"+"mpeg",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("font" , "TTF"),
|
||||
Expected: "font"+"/"+"ttf",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "PNG"),
|
||||
Expected: "image"+"/"+"png",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "HTML"),
|
||||
Expected: "text"+"/"+"html",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "OGG"),
|
||||
Expected: "video"+"/"+"ogg",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("xyz" , "BANANA"),
|
||||
Expected: "xyz"+"/"+"banana",
|
||||
},
|
||||
}
|
||||
|
||||
for testNumber, test := range tests {
|
||||
|
||||
actual := test.MediaRange.String()
|
||||
expected := test.Expected
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("For test #%d, the actual (serialized) stringer value is not what was expected.", testNumber)
|
||||
t.Logf("MEDIA-RANGE: %#v", test.MediaRange)
|
||||
t.Logf("EXPECTED: %q", expected)
|
||||
t.Logf("ACTUAL: %q", actual)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,481 @@
|
|||
package mediarange_test
|
||||
|
||||
import (
|
||||
"sourcecode.social/reiver/go-httpaccept/mediarange"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMediaRange_SubType(t *testing.T) {
|
||||
|
||||
tests := []struct{
|
||||
MediaRange mediarange.MediaRange
|
||||
Expected string
|
||||
}{
|
||||
{
|
||||
MediaRange: mediarange.Create("" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("*" , "*"),
|
||||
Expected: "*",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("font" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("xyz" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "*"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "*"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("font" , "*"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "*"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("xyz" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("Application" , "*"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Audio" , "*"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Font" , "*"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Image" , "*"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Text" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Video" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Xyz" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("APPLICATION" , "*"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("AUDIO" , "*"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("FONT" , "*"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("IMAGE" , "*"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("TEXT" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("VIDEO" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("XYZ" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "activity+json"),
|
||||
Expected: "activity+json",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "epub+zip"),
|
||||
Expected: "epub+zip",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "gzip"),
|
||||
Expected: "gzip",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "java-archive"),
|
||||
Expected: "java-archive",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "json"),
|
||||
Expected: "json",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "ld+json"),
|
||||
Expected: "ld+json",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "pdf"),
|
||||
Expected: "pdf",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "msword"),
|
||||
Expected: "msword",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "octet-stream"),
|
||||
Expected: "octet-stream",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "ogg"),
|
||||
Expected: "ogg",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "rtf"),
|
||||
Expected: "rtf",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "vnd.oasis.opendocument.spreadsheet"),
|
||||
Expected: "vnd.oasis.opendocument.spreadsheet",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "vnd.openxmlformats-officedocument.wordprocessingml.document"),
|
||||
Expected: "vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "vnd.mozilla.xul+xml"),
|
||||
Expected: "vnd.mozilla.xul+xml",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "vnd.ms-excel"),
|
||||
Expected: "vnd.ms-excel",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-7z-compressed"),
|
||||
Expected: "x-7z-compressed",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-bzip"),
|
||||
Expected: "x-bzip",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-bzip2"),
|
||||
Expected: "x-bzip2",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-cdf"),
|
||||
Expected: "x-cdf",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-csh"),
|
||||
Expected: "x-csh",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-freearc"),
|
||||
Expected: "x-freearc",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-httpd-php"),
|
||||
Expected: "x-httpd-php",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-sh"),
|
||||
Expected: "x-sh",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-tar"),
|
||||
Expected: "x-tar",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "xhtml+xml"),
|
||||
Expected: "xhtml+xml",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "aac"),
|
||||
Expected: "aac",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "midi"),
|
||||
Expected: "midi",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "mpeg"),
|
||||
Expected: "mpeg",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "ogg"),
|
||||
Expected: "ogg",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "opus"),
|
||||
Expected: "opus",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "wav"),
|
||||
Expected: "wav",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "webm"),
|
||||
Expected: "webm",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "3gpp"),
|
||||
Expected: "3gpp",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "3gpp2"),
|
||||
Expected: "3gpp2",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "x-midi"),
|
||||
Expected: "x-midi",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("font" , "ttf"),
|
||||
Expected: "ttf",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "avif"),
|
||||
Expected: "avif",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "bmp"),
|
||||
Expected: "bmp",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "gif"),
|
||||
Expected: "gif",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "png"),
|
||||
Expected: "png",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "tiff"),
|
||||
Expected: "tiff",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "css"),
|
||||
Expected: "css",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "csv"),
|
||||
Expected: "csv",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "html"),
|
||||
Expected: "html",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "javascript"),
|
||||
Expected: "javascript",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "markdown"),
|
||||
Expected: "markdown",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "plain"),
|
||||
Expected: "plain",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "x-markdown"),
|
||||
Expected: "x-markdown",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "3gpp2"),
|
||||
Expected: "3gpp2",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "ogg"),
|
||||
Expected: "ogg",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "mp2t"),
|
||||
Expected: "mp2t",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "mp4"),
|
||||
Expected: "mp4",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "mpeg"),
|
||||
Expected: "mpeg",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "webm"),
|
||||
Expected: "webm",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "x-msvideo"),
|
||||
Expected: "x-msvideo",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "ACTIVITY+JSON"),
|
||||
Expected: "activity+json",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "MPEG"),
|
||||
Expected: "mpeg",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("font" , "TTF"),
|
||||
Expected: "ttf",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "PNG"),
|
||||
Expected: "png",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "HTML"),
|
||||
Expected: "html",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "OGG"),
|
||||
Expected: "ogg",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("xyz" , "BANANA"),
|
||||
Expected: "banana",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("*" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("*" , "apple"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("*" , "BANANA"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("*" , "Cherry"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("*" , "gif"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("*" , "html"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("*" , "javascript"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("*" , "json"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("*" , "png"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("*" , "webm"),
|
||||
Expected: "*",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("*" , "xml"),
|
||||
Expected: "*",
|
||||
},
|
||||
}
|
||||
|
||||
for testNumber, test := range tests {
|
||||
|
||||
actual := test.MediaRange.SubType()
|
||||
expected := test.Expected
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("For test #%d, the actual canonical-subtype is not what was expected.", testNumber)
|
||||
t.Logf("MEDIA-RANGE: %#v", test.MediaRange)
|
||||
t.Logf("EXPECTED: %q", expected)
|
||||
t.Logf("ACTUAL: %q", actual)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,434 @@
|
|||
package mediarange_test
|
||||
|
||||
import (
|
||||
"sourcecode.social/reiver/go-httpaccept/mediarange"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMediaRange_Type(t *testing.T) {
|
||||
|
||||
tests := []struct{
|
||||
MediaRange mediarange.MediaRange
|
||||
Expected string
|
||||
}{
|
||||
{
|
||||
MediaRange: mediarange.Create("" , ""),
|
||||
Expected: "*",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("*" , "*"),
|
||||
Expected: "*",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , ""),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , ""),
|
||||
Expected: "audio",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("font" , ""),
|
||||
Expected: "font",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , ""),
|
||||
Expected: "image",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , ""),
|
||||
Expected: "text",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , ""),
|
||||
Expected: "video",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("xyz" , ""),
|
||||
Expected: "xyz",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "*"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "*"),
|
||||
Expected: "audio",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("font" , "*"),
|
||||
Expected: "font",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "*"),
|
||||
Expected: "image",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , ""),
|
||||
Expected: "text",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , ""),
|
||||
Expected: "video",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("xyz" , ""),
|
||||
Expected: "xyz",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("Application" , "*"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Audio" , "*"),
|
||||
Expected: "audio",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Font" , "*"),
|
||||
Expected: "font",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Image" , "*"),
|
||||
Expected: "image",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Text" , ""),
|
||||
Expected: "text",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Video" , ""),
|
||||
Expected: "video",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("Xyz" , ""),
|
||||
Expected: "xyz",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("APPLICATION" , "*"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("AUDIO" , "*"),
|
||||
Expected: "audio",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("FONT" , "*"),
|
||||
Expected: "font",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("IMAGE" , "*"),
|
||||
Expected: "image",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("TEXT" , ""),
|
||||
Expected: "text",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("VIDEO" , ""),
|
||||
Expected: "video",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("XYZ" , ""),
|
||||
Expected: "xyz",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "activity+json"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "epub+zip"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "gzip"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "java-archive"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "json"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "ld+json"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "pdf"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "msword"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "octet-stream"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "ogg"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "rtf"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "vnd.oasis.opendocument.spreadsheet"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "vnd.openxmlformats-officedocument.wordprocessingml.document"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "vnd.mozilla.xul+xml"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "vnd.ms-excel"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-7z-compressed"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-bzip"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-bzip2"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-cdf"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-csh"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-freearc"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-httpd-php"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-sh"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "x-tar"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "xhtml+xml"),
|
||||
Expected: "application",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "aac"),
|
||||
Expected: "audio",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "midi"),
|
||||
Expected: "audio",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "mpeg"),
|
||||
Expected: "audio",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "ogg"),
|
||||
Expected: "audio",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "opus"),
|
||||
Expected: "audio",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "wav"),
|
||||
Expected: "audio",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "webm"),
|
||||
Expected: "audio",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "3gpp"),
|
||||
Expected: "audio",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "3gpp2"),
|
||||
Expected: "audio",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "x-midi"),
|
||||
Expected: "audio",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("font" , "ttf"),
|
||||
Expected: "font",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "avif"),
|
||||
Expected: "image",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "bmp"),
|
||||
Expected: "image",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "gif"),
|
||||
Expected: "image",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "png"),
|
||||
Expected: "image",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "tiff"),
|
||||
Expected: "image",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "css"),
|
||||
Expected: "text",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "csv"),
|
||||
Expected: "text",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "html"),
|
||||
Expected: "text",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "javascript"),
|
||||
Expected: "text",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "markdown"),
|
||||
Expected: "text",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "plain"),
|
||||
Expected: "text",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "x-markdown"),
|
||||
Expected: "text",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "3gpp2"),
|
||||
Expected: "video",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "ogg"),
|
||||
Expected: "video",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "mp2t"),
|
||||
Expected: "video",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "mp4"),
|
||||
Expected: "video",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "mpeg"),
|
||||
Expected: "video",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "webm"),
|
||||
Expected: "video",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "x-msvideo"),
|
||||
Expected: "video",
|
||||
},
|
||||
|
||||
|
||||
|
||||
{
|
||||
MediaRange: mediarange.Create("application" , "ACTIVITY+JSON"),
|
||||
Expected: "application",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("audio" , "MPEG"),
|
||||
Expected: "audio",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("font" , "TTF"),
|
||||
Expected: "font",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("image" , "PNG"),
|
||||
Expected: "image",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("text" , "HTML"),
|
||||
Expected: "text",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("video" , "OGG"),
|
||||
Expected: "video",
|
||||
},
|
||||
{
|
||||
MediaRange: mediarange.Create("xyz" , "BANANA"),
|
||||
Expected: "xyz",
|
||||
},
|
||||
}
|
||||
|
||||
for testNumber, test := range tests {
|
||||
|
||||
actual := test.MediaRange.Type()
|
||||
expected := test.Expected
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("For test #%d, the actual canonical-type is not what was expected.", testNumber)
|
||||
t.Logf("MEDIA-RANGEE: %#v", test.MediaRange)
|
||||
t.Logf("EXPECTED: %q", expected)
|
||||
t.Logf("ACTUAL: %q", actual)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue