Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pkg/util/isjson.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package util

import "strings"
import (
"mime"
"strings"
)

func IsMediaTypeJson(mediaType string) bool {
return mediaType == "application/json" || strings.HasSuffix(mediaType, "+json")
parsed, _, err := mime.ParseMediaType(mediaType)
if err != nil {
return false
}
return parsed == "application/json" || strings.HasSuffix(parsed, "+json")
}
12 changes: 12 additions & 0 deletions pkg/util/isjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ func TestIsMediaTypeJson(t *testing.T) {
mediaType: "application/vnd.api+json",
want: true,
},
{
// NOTE that this _technically_ isn't a standard extension to JSON https://www.iana.org/assignments/media-types/application/json but due to the fact that several APIs do use it, we should support it
name: "When MediaType is application/json;v=1, returns true",
mediaType: "application/json;v=1",
want: true,
},
{
// NOTE that this _technically_ isn't a standard extension to JSON https://www.iana.org/assignments/media-types/application/json but due to the fact that several APIs do use it, we should support it
name: "When MediaType is application/json;version=1, returns true",
mediaType: "application/json;version=1",
want: true,
},
}
for _, test := range suite {
t.Run(test.name, func(t *testing.T) {
Expand Down