Skip to content

Commit

Permalink
add chaining function Discovery to disable/enable discovery feature f…
Browse files Browse the repository at this point in the history
…or package gclient; fix issue #2737 (#2738)
  • Loading branch information
gqcn authored Jul 4, 2023
1 parent 740dfa5 commit ba2a7e4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
37 changes: 37 additions & 0 deletions contrib/registry/file/file_z_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"time"

"github.com/gogf/gf/contrib/registry/file/v2"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/net/gsvc"
Expand Down Expand Up @@ -48,3 +50,38 @@ func Test_HTTP_Registry(t *testing.T) {
t.Assert(client.GetContent(ctx, "/http-registry"), svcName)
})
}

func Test_HTTP_Discovery_Disable(t *testing.T) {
var (
svcName = guid.S()
dirPath = gfile.Temp(svcName)
)
defer gfile.Remove(dirPath)
gsvc.SetRegistry(file.New(dirPath))

s := g.Server()
s.BindHandler("/http-registry", func(r *ghttp.Request) {
r.Response.Write(svcName)
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)

gtest.C(t, func(t *gtest.T) {
client := g.Client()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
result, err := client.Get(ctx, "/http-registry")
defer result.Close()
t.Assert(gerror.Code(err), gcode.CodeNotFound)
})
gtest.C(t, func(t *gtest.T) {
client := g.Client()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
result, err := client.Discovery(nil).Get(ctx, "/http-registry")
defer result.Close()
t.AssertNil(err)
t.Assert(result.ReadAllString(), svcName)
})
}
10 changes: 10 additions & 0 deletions net/gclient/gclient_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package gclient

import (
"time"

"github.com/gogf/gf/v2/net/gsvc"
)

// Prefix is a chaining function,
Expand Down Expand Up @@ -37,6 +39,14 @@ func (c *Client) HeaderRaw(headers string) *Client {
return newClient
}

// Discovery is a chaining function, which sets the discovery for client.
// You can use `Discovery(nil)` to disable discovery feature for current client.
func (c *Client) Discovery(discovery gsvc.Discovery) *Client {
newClient := c.Clone()
newClient.SetDiscovery(discovery)
return newClient
}

// Cookie is a chaining function,
// which sets cookie items with map for next request.
func (c *Client) Cookie(m map[string]string) *Client {
Expand Down

0 comments on commit ba2a7e4

Please sign in to comment.