1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 03:05:05 +08:00

fix BuildParams with urlEncode when len(v) <= 6 (#2308)

* fix: check urlEncode when len(v) <= 6

* fix BuildParams with urlEncode when len(v) <= 6

* fix BuildParams with urlEncode when len(v) <= 6

Co-authored-by: Prime Xiao <primexiao.dev@gmail.com>
This commit is contained in:
Prime.X 2022-12-23 10:33:28 +08:00 committed by GitHub
parent e007bf35b2
commit 85c4794ceb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 2 deletions

View File

@ -59,8 +59,12 @@ func BuildParams(params interface{}, noUrlEncode ...bool) (encodedParamStr strin
encodedParamStr += "&"
}
s = gconv.String(v)
if urlEncode && len(s) > 6 && strings.Compare(s[0:6], fileUploadingKey) != 0 {
s = gurl.Encode(s)
if urlEncode {
if strings.HasPrefix(s, fileUploadingKey) && len(s) > len(fileUploadingKey) {
// No url encoding if uploading file.
} else {
s = gurl.Encode(s)
}
}
encodedParamStr += k + "=" + s
}

View File

@ -10,11 +10,13 @@ import (
"context"
"fmt"
"net/http"
"net/url"
"testing"
"time"
"github.com/gogf/gf/v2/encoding/gurl"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/httputil"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/genv"
"github.com/gogf/gf/v2/test/gtest"
@ -139,3 +141,30 @@ func Test_RoutePathParams(t *testing.T) {
)
})
}
func Test_BuildParams(t *testing.T) {
// normal && special cases
params := map[string]string{
"val": "12345678",
"code1": "x&a=1", // for fix
"code2": "x&a=111",
"id": "1+- ", // for fix
"f": "1#a=+- ",
"v": "",
"n": "null",
}
gtest.C(t, func(t *gtest.T) {
res1 := httputil.BuildParams(params)
vs, _ := url.ParseQuery(res1)
t.Assert(len(params), len(vs))
for k := range vs {
vv := vs.Get(k)
_, ok := params[k]
// check no additional param
t.Assert(ok, true)
// check equal
t.AssertEQ(params[k], vv)
}
})
}