github.com/safing/portbase@v0.19.5/formats/dsd/http_test.go (about)

     1  package dsd
     2  
     3  import (
     4  	"mime"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestMimeTypes(t *testing.T) {
    11  	t.Parallel()
    12  
    13  	// Test static maps.
    14  	for _, mimeType := range FormatToMimeType {
    15  		cleaned, _, err := mime.ParseMediaType(mimeType)
    16  		assert.NoError(t, err, "mime type must be parse-able")
    17  		assert.Equal(t, mimeType, cleaned, "mime type should be clean in map already")
    18  	}
    19  	for mimeType := range MimeTypeToFormat {
    20  		cleaned, _, err := mime.ParseMediaType(mimeType)
    21  		assert.NoError(t, err, "mime type must be parse-able")
    22  		assert.Equal(t, mimeType, cleaned, "mime type should be clean in map already")
    23  	}
    24  
    25  	// Test assumptions.
    26  	for accept, format := range map[string]uint8{
    27  		"application/json, image/webp":       JSON,
    28  		"image/webp, application/json":       JSON,
    29  		"application/json;q=0.9, image/webp": JSON,
    30  		"*":                                  DefaultSerializationFormat,
    31  		"*/*":                                DefaultSerializationFormat,
    32  		"text/yAMl":                          YAML,
    33  		" * , yaml ":                         YAML,
    34  		"yaml;charset ,*":                    YAML,
    35  		"xml,*":                              DefaultSerializationFormat,
    36  		"text/xml, text/other":               AUTO,
    37  		"text/*":                             DefaultSerializationFormat,
    38  		"yaml ;charset":                      AUTO, // Invalid mimetype format.
    39  		"":                                   DefaultSerializationFormat,
    40  		"x":                                  AUTO,
    41  	} {
    42  		derivedFormat := FormatFromAccept(accept)
    43  		assert.Equal(t, format, derivedFormat, "assumption for %q should hold", accept)
    44  	}
    45  }