Skip to content

Commit

Permalink
add SetNoUrlEncode/NoUrlEncode functions for gclient.Client (#3041)
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn authored Oct 17, 2023
1 parent 972f8c3 commit bb9cd37
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions net/gclient/gclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Client struct {
authUser string // HTTP basic authentication: user.
authPass string // HTTP basic authentication: pass.
retryCount int // Retry count when request fails.
noUrlEncode bool // No url encoding for request parameters.
retryInterval time.Duration // Retry interval when request fails.
middlewareHandler []HandlerFunc // Interceptor handlers
discovery gsvc.Discovery // Discovery for service.
Expand Down
7 changes: 7 additions & 0 deletions net/gclient/gclient_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,10 @@ func (c *Client) RedirectLimit(redirectLimit int) *Client {
newClient.SetRedirectLimit(redirectLimit)
return newClient
}

// NoUrlEncode sets the mark that do not encode the parameters before sending request.
func (c *Client) NoUrlEncode() *Client {
newClient := c.Clone()
newClient.SetNoUrlEncode(true)
return newClient
}
6 changes: 6 additions & 0 deletions net/gclient/gclient_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ func (c *Client) SetRedirectLimit(redirectLimit int) *Client {
return c
}

// SetNoUrlEncode sets the mark that do not encode the parameters before sending request.
func (c *Client) SetNoUrlEncode(noUrlEncode bool) *Client {
c.noUrlEncode = noUrlEncode
return c
}

// SetProxy set proxy for the client.
// This func will do nothing when the parameter `proxyURL` is empty or in wrong pattern.
// The correct pattern is like `http://USER:PASSWORD@IP:PORT` or `socks5://USER:PASSWORD@IP:PORT`.
Expand Down
2 changes: 1 addition & 1 deletion net/gclient/gclient_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (c *Client) prepareRequest(ctx context.Context, method, url string, data ..
}
}
default:
params = httputil.BuildParams(data[0])
params = httputil.BuildParams(data[0], c.noUrlEncode)
}
}
if method == http.MethodGet {
Expand Down
50 changes: 50 additions & 0 deletions net/gclient/gclient_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,53 @@ func TestClient_SetBodyContent(t *testing.T) {
t.Assert(res.ReadAllString(), "world")
})
}

func TestClient_SetNoUrlEncode(t *testing.T) {
s := g.Server(guid.S())
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.Write(r.URL.RawQuery)
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
c := g.Client()
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))

var params = g.Map{
"path": "/data/binlog",
}
t.Assert(c.GetContent(ctx, `/`, params), `path=%2Fdata%2Fbinlog`)

c.SetNoUrlEncode(true)
t.Assert(c.GetContent(ctx, `/`, params), `path=/data/binlog`)

c.SetNoUrlEncode(false)
t.Assert(c.GetContent(ctx, `/`, params), `path=%2Fdata%2Fbinlog`)
})
}

func TestClient_NoUrlEncode(t *testing.T) {
s := g.Server(guid.S())
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.Write(r.URL.RawQuery)
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
c := g.Client()
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))

var params = g.Map{
"path": "/data/binlog",
}
t.Assert(c.GetContent(ctx, `/`, params), `path=%2Fdata%2Fbinlog`)

t.Assert(c.NoUrlEncode().GetContent(ctx, `/`, params), `path=/data/binlog`)
})
}

0 comments on commit bb9cd37

Please sign in to comment.