main
1package serde
2
3import (
4 "fmt"
5 "testing"
6
7 "github.com/google/jsonapi"
8 "github.com/stretchr/testify/assert"
9)
10
11func TestMediaTypeFor(t *testing.T) {
12 tt := []struct {
13 input string
14 expected MediaType
15 }{
16 {input: jsonapi.MediaType, expected: JSONAPI},
17 {input: "application/json", expected: JSON},
18 {input: "text/plain", expected: Text},
19 {input: "text/html", expected: Default},
20 {input: "*/*", expected: Default},
21 {input: "text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8", expected: Default},
22 {input: "application/vnd.api+json, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8", expected: JSONAPI},
23 {input: "application/vnd.api+json;q=0.1, application/json;q=0.2, text/plain;q=0.3, unknown/thing;q=0.4", expected: Text},
24 {input: "text/html; charset=UTF-8", expected: Default},
25 {input: "application/json; charset=UTF-8", expected: JSON},
26 {input: "application/vnd.api+json; charset=UTF-8", expected: JSONAPI},
27 {input: "text/plain; charset=UTF-8", expected: Text},
28 {input: "application/json, text/plain, */*", expected: JSON},
29 }
30 for _, row := range tt {
31 t.Run(fmt.Sprintf("%v returns %v", row.input, row.expected), func(t *testing.T) {
32 assert.Equal(t, row.expected, MediaTypeFor(row.input))
33 })
34 }
35}