Skip to content

Commit

Permalink
fix: improve regex expression for json and xml content type validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Sep 24, 2023
1 parent 7c1134f commit 8d8419f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
4 changes: 2 additions & 2 deletions client.go
Expand Up @@ -62,8 +62,8 @@ var (
jsonContentType = "application/json"
formContentType = "application/x-www-form-urlencoded"

jsonCheck = regexp.MustCompile(`(?i:(application|text)/(json|.*\+json|json\-.*)(;|$))`)
xmlCheck = regexp.MustCompile(`(?i:(application|text)/(xml|.*\+xml)(;|$))`)
jsonCheck = regexp.MustCompile(`(?i:(application|text)/(.*json.*)(;|$))`)
xmlCheck = regexp.MustCompile(`(?i:(application|text)/(.*xml.*)(;|$))`)

hdrUserAgentValue = "go-resty/" + Version + " (https://github.com/go-resty/resty)"
bufPool = &sync.Pool{New: func() interface{} { return &bytes.Buffer{} }}
Expand Down
44 changes: 19 additions & 25 deletions util_test.go
Expand Up @@ -23,20 +23,18 @@ func TestIsJSONType(t *testing.T) {
{"application/vnd.foo+json; charset=utf-8", true},

{"text/json", true},
{"text/xml+json", true},
{"text/vnd.foo+json", true},

{"application/foo-json", false},
{"application/foo.json", false},
{"application/vnd.foo-json", false},
{"application/vnd.foo.json", false},
{"application/json+xml", false},

{"text/foo-json", false},
{"text/foo.json", false},
{"text/vnd.foo-json", false},
{"text/vnd.foo.json", false},
{"text/json+xml", false},
{"application/foo-json", true},
{"application/foo.json", true},
{"application/vnd.foo-json", true},
{"application/vnd.foo.json", true},
{"application/x-amz-json-1.1", true},

{"text/foo-json", true},
{"text/foo.json", true},
{"text/vnd.foo-json", true},
{"text/vnd.foo.json", true},
} {
result := IsJSONType(test.input)

Expand All @@ -52,27 +50,23 @@ func TestIsXMLType(t *testing.T) {
expect bool
}{
{"application/xml", true},
{"application/json+xml", true},
{"application/vnd.foo+xml", true},

{"application/xml; charset=utf-8", true},
{"application/vnd.foo+xml; charset=utf-8", true},

{"text/xml", true},
{"text/json+xml", true},
{"text/vnd.foo+xml", true},

{"application/foo-xml", false},
{"application/foo.xml", false},
{"application/vnd.foo-xml", false},
{"application/vnd.foo.xml", false},
{"application/xml+json", false},

{"text/foo-xml", false},
{"text/foo.xml", false},
{"text/vnd.foo-xml", false},
{"text/vnd.foo.xml", false},
{"text/xml+json", false},
{"application/foo-xml", true},
{"application/foo.xml", true},
{"application/vnd.foo-xml", true},
{"application/vnd.foo.xml", true},

{"text/foo-xml", true},
{"text/foo.xml", true},
{"text/vnd.foo-xml", true},
{"text/vnd.foo.xml", true},
} {
result := IsXMLType(test.input)

Expand Down

0 comments on commit 8d8419f

Please sign in to comment.