diff --git a/gin/request/util.go b/gin/request/util.go new file mode 100644 index 0000000..f978366 --- /dev/null +++ b/gin/request/util.go @@ -0,0 +1,62 @@ +// Package request... +// +// Description : 解析请求 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2021-07-24 8:53 下午 +package request + +import ( + "net/url" + "strings" + + "github.com/gin-gonic/gin" +) + +// ParseGetRequestURI 解析URL请求参数 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 9:03 下午 2021/7/24 +func ParseGetRequestURI(url *url.URL) map[string][]string { + url.RawQuery = strings.TrimSpace(url.RawQuery) + parseResult := make(map[string][]string) + if len(url.RawQuery) == 0 { + return parseResult + } + requestURIArr := strings.Split(url.RawQuery, "&") + for _, kvPair := range requestURIArr { + kvPair = strings.TrimSpace(kvPair) + if len(kvPair) == 0 { + continue + } + kvArr := strings.Split(kvPair, "=") + _, keyExist := parseResult[kvArr[0]] + if len(kvArr) == 1 { + // 识别到 ?a=1&b=2&c 这种格式,默认补齐成 ?a=1&b=2&c= + if keyExist { + parseResult[kvPair] = append(parseResult[kvPair], "") + } else { + parseResult[kvPair] = []string{""} + } + continue + } + // 兼容参数值中包含=的情况 + if keyExist { + parseResult[kvArr[0]] = append(parseResult[kvArr[0]], strings.Join(kvArr[1:], "=")) + } else { + parseResult[kvArr[0]] = []string{strings.Join(kvArr[1:], "=")} + } + } + return parseResult +} + +// ParseGetRequestBody 解析get请求的body +// +// Author : zhangdeman001@ke.com<张德满> +// +// Date : 9:07 下午 2021/7/24 +func ParseGetRequestBody(ctx *gin.Context) { + +} diff --git a/gin/request/util_test.go b/gin/request/util_test.go new file mode 100644 index 0000000..5b34c17 --- /dev/null +++ b/gin/request/util_test.go @@ -0,0 +1,43 @@ +// Package request... +// +// Description : request... +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-07-24 9:21 下午 +package request + +import ( + "net/url" + "reflect" + "testing" +) + +// TestParseGetRequestURI 测试解析请求uri +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 9:28 下午 2021/7/24 +func TestParseGetRequestURI(t *testing.T) { + type args struct { + url *url.URL + } + tests := []struct { + name string + args args + want map[string][]string + }{ + {"不存在任何参数", args{url: &url.URL{RawQuery: ""}}, make(map[string][]string)}, + {"正常参数", args{url: &url.URL{RawQuery: "name=zhang&age=10"}}, map[string][]string{"name": {"zhang"}, "age": {"10"}}}, + {"多个相同参数", args{url: &url.URL{RawQuery: "name=zhang&age=10&name=wang"}}, map[string][]string{"name": {"zhang", "wang"}, "age": {"10"}}}, + {"末尾存在&符", args{url: &url.URL{RawQuery: "name=zhang&age=10&name=wang&"}}, map[string][]string{"name": {"zhang", "wang"}, "age": {"10"}}}, + {"缺失=号", args{url: &url.URL{RawQuery: "name=zhang&age=10&name=wang&test"}}, map[string][]string{"name": {"zhang", "wang"}, "age": {"10"}, "test": {""}}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := ParseGetRequestURI(tt.args.url); !reflect.DeepEqual(got, tt.want) { + t.Errorf("ParseGetRequestURI() = %v, want %v", got, tt.want) + } + }) + } +}