Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make IsJSONType and IsXMLType more robust #174

Merged
merged 2 commits into from
Aug 10, 2018
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
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ var (
jsonContentType = "application/json; charset=utf-8"
formContentType = "application/x-www-form-urlencoded"

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

hdrUserAgentValue = "go-resty v%s - https://github.com/go-resty/resty"
bufPool = &sync.Pool{New: func() interface{} { return &bytes.Buffer{} }}
Expand Down
81 changes: 81 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) 2015-2018 Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
// resty source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.

package resty

import (
"testing"
)

func TestIsJSONType(t *testing.T) {
for _, test := range []struct {
input string
expect bool
}{
{"application/json", true},
{"application/xml+json", true},
{"application/vnd.foo+json", true},

{"application/json; charset=utf-8", true},
{"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},
} {
result := IsJSONType(test.input)

if result != test.expect {
t.Errorf("failed on %q: want %v, got %v", test.input, test.expect, result)
}
}
}

func TestIsXMLType(t *testing.T) {
for _, test := range []struct {
input string
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},
} {
result := IsXMLType(test.input)

if result != test.expect {
t.Errorf("failed on %q: want %v, got %v", test.input, test.expect, result)
}
}
}