From 8c67426f0f9421ef3c5210ea37489bc882eaf300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 24 Jul 2021 21:37:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0URL=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gin/request/util.go | 62 ++++++++++++++++++++++++++++++++++++++++ gin/request/util_test.go | 43 ++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 gin/request/util.go create mode 100644 gin/request/util_test.go 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) + } + }) + } +}