Skip to content

Commit

Permalink
test: add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
welkeyever committed Sep 7, 2023
1 parent 1144ccf commit 2adc039
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
38 changes: 38 additions & 0 deletions pkg/app/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2151,6 +2151,44 @@ func TestClientRetry(t *testing.T) {
}
}

func TestClientHostClientConfigHookError(t *testing.T) {
client, _ := NewClient(WithHostClientConfigHook(func(hc interface{}) error {
hct, ok := hc.(*http1.HostClient)
assert.True(t, ok)
assert.DeepEqual(t, "foo.bar:80", hct.Addr)
return errors.New("hook return")
}))

req := protocol.AcquireRequest()
req.SetMethod(consts.MethodGet)
req.SetRequestURI("http://foo.bar/")
resp := protocol.AcquireResponse()
err := client.do(nil, req, resp)
assert.DeepEqual(t, "hook return", err.Error())
}

func TestClientHostClientConfigHook(t *testing.T) {
client, _ := NewClient(WithHostClientConfigHook(func(hc interface{}) error {
hct, ok := hc.(*http1.HostClient)
assert.True(t, ok)
assert.DeepEqual(t, "foo.bar:80", hct.Addr)
hct.Addr = "FOO.BAR:443"
return nil
}))

req := protocol.AcquireRequest()
req.SetMethod(consts.MethodGet)
req.SetRequestURI("http://foo.bar/")
resp := protocol.AcquireResponse()
client.do(context.Background(), req, resp)
client.mLock.Lock()
hc := client.m["foo.bar"]
client.mLock.Unlock()
hcr, ok := hc.(*http1.HostClient)
assert.True(t, ok)
assert.DeepEqual(t, "FOO.BAR:443", hcr.Addr)
}

func TestClientDialerName(t *testing.T) {
client, _ := NewClient()
dName, err := client.GetDialerName()
Expand Down
3 changes: 1 addition & 2 deletions pkg/app/client/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/cloudwego/hertz/pkg/network"
"github.com/cloudwego/hertz/pkg/network/dialer"
"github.com/cloudwego/hertz/pkg/network/standard"
"github.com/cloudwego/hertz/pkg/protocol/client"
"github.com/cloudwego/hertz/pkg/protocol/consts"
)

Expand Down Expand Up @@ -101,7 +100,7 @@ func WithResponseBodyStream(b bool) config.ClientOption {
}

// WithHostClientConfigHook is used to set the function hook for re-configure the host client.
func WithHostClientConfigHook(h func(hc client.HostClient) error) config.ClientOption {
func WithHostClientConfigHook(h func(hc interface{}) error) config.ClientOption {
return config.ClientOption{F: func(o *config.ClientOptions) {
o.HostClientConfigHook = h
}}
Expand Down

0 comments on commit 2adc039

Please sign in to comment.