From 245df74b1effee8bee7cfedc4470758239ad5730 Mon Sep 17 00:00:00 2001 From: fgy Date: Thu, 10 Oct 2024 16:15:57 +0800 Subject: [PATCH] fix: json checker --- cmd/hz/generator/json_checker_test.go | 18 ++++++++++++++++++ cmd/hz/generator/package_tpl.go | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 cmd/hz/generator/json_checker_test.go diff --git a/cmd/hz/generator/json_checker_test.go b/cmd/hz/generator/json_checker_test.go new file mode 100644 index 000000000..54b96b4c8 --- /dev/null +++ b/cmd/hz/generator/json_checker_test.go @@ -0,0 +1,18 @@ +package generator + +import ( + "regexp" + "testing" +) + +var jsonCheck = regexp.MustCompile(`(?i:(application|text)/(json|.*\+json|json\-.*)(;\s*|$))`) + +func Test_JSONChecker(t *testing.T) { + ct := []string{"application/json", "application/json;", "application/json; ", "application/json;charset=UTF-8", "application/json; charset=UTF-8"} + + for _, c := range ct { + if jsonCheck.MatchString(c) != true { + t.Errorf("%s is json content-type, but get false", c) + } + } +} diff --git a/cmd/hz/generator/package_tpl.go b/cmd/hz/generator/package_tpl.go index 0efa0ccfe..1c7495b5c 100644 --- a/cmd/hz/generator/package_tpl.go +++ b/cmd/hz/generator/package_tpl.go @@ -312,8 +312,8 @@ var ( jsonContentType = "application/json; charset=utf-8" formContentType = "multipart/form-data" - jsonCheck = regexp.MustCompile(` + "`(?i:(application|text)/(json|.*\\+json|json\\-.*)(; |$))`)\n" + - `xmlCheck = regexp.MustCompile(` + "`(?i:(application|text)/(xml|.*\\+xml)(; |$))`)\n" + + jsonCheck = regexp.MustCompile(` + "`(?i:(application|text)/(json|.*\\+json|json\\-.*)(;\\s*|$))`)\n" + + `xmlCheck = regexp.MustCompile(` + "`(?i:(application|text)/(xml|.*\\+xml)(;\\s*|$))`)\n" + ` )