-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(net/ctype): add new util func for check content-type
- ToKind()
- Loading branch information
Showing
4 changed files
with
73 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,9 @@ | ||
package httpctype | ||
|
||
// there commonly base type category list | ||
const ( | ||
KindForm = "form" | ||
KindFormData = "dataForm" | ||
KindJSON = "json" | ||
KindXML = "xml" | ||
) |
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,33 @@ | ||
package httpctype | ||
|
||
import "strings" | ||
|
||
// ToKind name match | ||
func ToKind(cType, defaultType string) string { | ||
if len(cType) == 0 { | ||
return defaultType | ||
} | ||
|
||
// JSON body request: "application/json" | ||
if strings.Contains(cType, "/json") { | ||
return KindJSON | ||
} | ||
|
||
// basic POST form data binding. content type: "application/x-www-form-urlencoded" | ||
if strings.Contains(cType, "/x-www-form-urlencoded") { | ||
return KindForm | ||
} | ||
|
||
// contains file uploaded form: "multipart/form-data" "multipart/mixed" | ||
// strings.HasPrefix(mediaType, "multipart/") | ||
if strings.Contains(cType, "/form-data") { | ||
return KindFormData | ||
} | ||
|
||
// XML body request: "text/xml" | ||
if strings.Contains(cType, "/xml") { | ||
return KindXML | ||
} | ||
|
||
return defaultType | ||
} |
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,28 @@ | ||
package httpctype_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gookit/goutil/netutil/httpctype" | ||
"github.com/gookit/goutil/testutil/assert" | ||
) | ||
|
||
func TestToKind(t *testing.T) { | ||
tests := []struct { | ||
cType string | ||
defaultType string | ||
want string | ||
}{ | ||
{"", "abc", "abc"}, | ||
{"not-match", "", ""}, | ||
{"not-match", "abc", "abc"}, | ||
{httpctype.JSON, "", httpctype.KindJSON}, | ||
{httpctype.Form, "", httpctype.KindForm}, | ||
{httpctype.FormData, "", httpctype.KindFormData}, | ||
{httpctype.XML, "", httpctype.KindXML}, | ||
} | ||
|
||
for _, tt := range tests { | ||
assert.Equal(t, tt.want, httpctype.ToKind(tt.cType, tt.defaultType)) | ||
} | ||
} |