-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make IsJSONType and IsXMLType more robust (#174)
- The existing IsJSONType and IsXMLType regular expressions are here generalized to work with any properly formatted media type. - Update media type detection to work with parameters
- Loading branch information
1 parent
2620251
commit 8f9bf7b
Showing
2 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |