|
|
|
@ -5,9 +5,11 @@ import ( |
|
|
|
|
"context" |
|
|
|
|
"crypto/tls" |
|
|
|
|
"encoding/json" |
|
|
|
|
"errors" |
|
|
|
|
"fmt" |
|
|
|
|
"io" |
|
|
|
|
nethttp "net/http" |
|
|
|
|
"reflect" |
|
|
|
|
"strconv" |
|
|
|
|
"testing" |
|
|
|
|
"time" |
|
|
|
@ -15,7 +17,6 @@ import ( |
|
|
|
|
kratosErrors "github.com/go-kratos/kratos/v2/errors" |
|
|
|
|
"github.com/go-kratos/kratos/v2/middleware" |
|
|
|
|
"github.com/go-kratos/kratos/v2/registry" |
|
|
|
|
"github.com/stretchr/testify/assert" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
type mockRoundTripper struct{} |
|
|
|
@ -29,7 +30,9 @@ func TestWithTransport(t *testing.T) { |
|
|
|
|
o := WithTransport(ov) |
|
|
|
|
co := &clientOptions{} |
|
|
|
|
o(co) |
|
|
|
|
assert.Equal(t, co.transport, ov) |
|
|
|
|
if !reflect.DeepEqual(co.transport, ov) { |
|
|
|
|
t.Errorf("expected transport to be %v, got %v", ov, co.transport) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestWithTimeout(t *testing.T) { |
|
|
|
@ -37,14 +40,18 @@ func TestWithTimeout(t *testing.T) { |
|
|
|
|
o := WithTimeout(ov) |
|
|
|
|
co := &clientOptions{} |
|
|
|
|
o(co) |
|
|
|
|
assert.Equal(t, co.timeout, ov) |
|
|
|
|
if !reflect.DeepEqual(co.timeout, ov) { |
|
|
|
|
t.Errorf("expected timeout to be %v, got %v", ov, co.timeout) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestWithBlock(t *testing.T) { |
|
|
|
|
o := WithBlock() |
|
|
|
|
co := &clientOptions{} |
|
|
|
|
o(co) |
|
|
|
|
assert.True(t, co.block) |
|
|
|
|
if !co.block { |
|
|
|
|
t.Errorf("expected block to be true, got %v", co.block) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestWithBalancer(t *testing.T) { |
|
|
|
@ -55,7 +62,9 @@ func TestWithTLSConfig(t *testing.T) { |
|
|
|
|
o := WithTLSConfig(ov) |
|
|
|
|
co := &clientOptions{} |
|
|
|
|
o(co) |
|
|
|
|
assert.Same(t, ov, co.tlsConf) |
|
|
|
|
if !reflect.DeepEqual(co.tlsConf, ov) { |
|
|
|
|
t.Errorf("expected tls config to be %v, got %v", ov, co.tlsConf) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestWithUserAgent(t *testing.T) { |
|
|
|
@ -63,7 +72,9 @@ func TestWithUserAgent(t *testing.T) { |
|
|
|
|
o := WithUserAgent(ov) |
|
|
|
|
co := &clientOptions{} |
|
|
|
|
o(co) |
|
|
|
|
assert.Equal(t, co.userAgent, ov) |
|
|
|
|
if !reflect.DeepEqual(co.userAgent, ov) { |
|
|
|
|
t.Errorf("expected user agent to be %v, got %v", ov, co.userAgent) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestWithMiddleware(t *testing.T) { |
|
|
|
@ -72,7 +83,9 @@ func TestWithMiddleware(t *testing.T) { |
|
|
|
|
func(middleware.Handler) middleware.Handler { return nil }, |
|
|
|
|
} |
|
|
|
|
WithMiddleware(v...)(o) |
|
|
|
|
assert.Equal(t, v, o.middleware) |
|
|
|
|
if !reflect.DeepEqual(o.middleware, v) { |
|
|
|
|
t.Errorf("expected middleware to be %v, got %v", v, o.middleware) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestWithEndpoint(t *testing.T) { |
|
|
|
@ -80,7 +93,9 @@ func TestWithEndpoint(t *testing.T) { |
|
|
|
|
o := WithEndpoint(ov) |
|
|
|
|
co := &clientOptions{} |
|
|
|
|
o(co) |
|
|
|
|
assert.Equal(t, co.endpoint, ov) |
|
|
|
|
if !reflect.DeepEqual(co.endpoint, ov) { |
|
|
|
|
t.Errorf("expected endpoint to be %v, got %v", ov, co.endpoint) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestWithRequestEncoder(t *testing.T) { |
|
|
|
@ -89,21 +104,27 @@ func TestWithRequestEncoder(t *testing.T) { |
|
|
|
|
return nil, nil |
|
|
|
|
} |
|
|
|
|
WithRequestEncoder(v)(o) |
|
|
|
|
assert.NotNil(t, o.encoder) |
|
|
|
|
if o.encoder == nil { |
|
|
|
|
t.Errorf("expected encoder to be not nil") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestWithResponseDecoder(t *testing.T) { |
|
|
|
|
o := &clientOptions{} |
|
|
|
|
v := func(ctx context.Context, res *nethttp.Response, out interface{}) error { return nil } |
|
|
|
|
WithResponseDecoder(v)(o) |
|
|
|
|
assert.NotNil(t, o.decoder) |
|
|
|
|
if o.decoder == nil { |
|
|
|
|
t.Errorf("expected encoder to be not nil") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestWithErrorDecoder(t *testing.T) { |
|
|
|
|
o := &clientOptions{} |
|
|
|
|
v := func(ctx context.Context, res *nethttp.Response) error { return nil } |
|
|
|
|
WithErrorDecoder(v)(o) |
|
|
|
|
assert.NotNil(t, o.errorDecoder) |
|
|
|
|
if o.errorDecoder == nil { |
|
|
|
|
t.Errorf("expected encoder to be not nil") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type mockDiscovery struct{} |
|
|
|
@ -139,7 +160,9 @@ func TestWithDiscovery(t *testing.T) { |
|
|
|
|
o := WithDiscovery(ov) |
|
|
|
|
co := &clientOptions{} |
|
|
|
|
o(co) |
|
|
|
|
assert.Equal(t, co.discovery, ov) |
|
|
|
|
if !reflect.DeepEqual(co.discovery, ov) { |
|
|
|
|
t.Errorf("expected discovery to be %v, got %v", ov, co.discovery) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestDefaultRequestEncoder(t *testing.T) { |
|
|
|
@ -154,14 +177,20 @@ func TestDefaultRequestEncoder(t *testing.T) { |
|
|
|
|
B int64 `json:"b"` |
|
|
|
|
}{"a", 1} |
|
|
|
|
b, err1 := DefaultRequestEncoder(context.TODO(), "application/json", v1) |
|
|
|
|
assert.Nil(t, err1) |
|
|
|
|
if err1 != nil { |
|
|
|
|
t.Errorf("expected no error, got %v", err1) |
|
|
|
|
} |
|
|
|
|
v1b := &struct { |
|
|
|
|
A string `json:"a"` |
|
|
|
|
B int64 `json:"b"` |
|
|
|
|
}{} |
|
|
|
|
err1 = json.Unmarshal(b, v1b) |
|
|
|
|
assert.Nil(t, err1) |
|
|
|
|
assert.Equal(t, v1, v1b) |
|
|
|
|
if err1 != nil { |
|
|
|
|
t.Errorf("expected no error, got %v", err1) |
|
|
|
|
} |
|
|
|
|
if !reflect.DeepEqual(v1b, v1) { |
|
|
|
|
t.Errorf("expected %v, got %v", v1, v1b) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestDefaultResponseDecoder(t *testing.T) { |
|
|
|
@ -175,9 +204,15 @@ func TestDefaultResponseDecoder(t *testing.T) { |
|
|
|
|
B int64 `json:"b"` |
|
|
|
|
}{} |
|
|
|
|
err1 := DefaultResponseDecoder(context.TODO(), resp1, &v1) |
|
|
|
|
assert.Nil(t, err1) |
|
|
|
|
assert.Equal(t, "1", v1.A) |
|
|
|
|
assert.Equal(t, int64(2), v1.B) |
|
|
|
|
if err1 != nil { |
|
|
|
|
t.Errorf("expected no error, got %v", err1) |
|
|
|
|
} |
|
|
|
|
if !reflect.DeepEqual("1", v1.A) { |
|
|
|
|
t.Errorf("expected %v, got %v", "1", v1.A) |
|
|
|
|
} |
|
|
|
|
if !reflect.DeepEqual(int64(2), v1.B) { |
|
|
|
|
t.Errorf("expected %v, got %v", 2, v1.B) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
resp2 := &nethttp.Response{ |
|
|
|
|
Header: make(nethttp.Header), |
|
|
|
@ -190,20 +225,26 @@ func TestDefaultResponseDecoder(t *testing.T) { |
|
|
|
|
}{} |
|
|
|
|
err2 := DefaultResponseDecoder(context.TODO(), resp2, &v2) |
|
|
|
|
terr1 := &json.SyntaxError{} |
|
|
|
|
assert.ErrorAs(t, err2, &terr1) |
|
|
|
|
if !errors.As(err2, &terr1) { |
|
|
|
|
t.Errorf("expected %v, got %v", terr1, err2) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestDefaultErrorDecoder(t *testing.T) { |
|
|
|
|
for i := 200; i < 300; i++ { |
|
|
|
|
resp := &nethttp.Response{Header: make(nethttp.Header), StatusCode: i} |
|
|
|
|
assert.Nil(t, DefaultErrorDecoder(context.TODO(), resp)) |
|
|
|
|
if DefaultErrorDecoder(context.TODO(), resp) != nil { |
|
|
|
|
t.Errorf("expected no error, got %v", DefaultErrorDecoder(context.TODO(), resp)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
resp1 := &nethttp.Response{ |
|
|
|
|
Header: make(nethttp.Header), |
|
|
|
|
StatusCode: 300, |
|
|
|
|
Body: io.NopCloser(bytes.NewBufferString("{\"foo\":\"bar\"}")), |
|
|
|
|
} |
|
|
|
|
assert.Error(t, DefaultErrorDecoder(context.TODO(), resp1)) |
|
|
|
|
if DefaultErrorDecoder(context.TODO(), resp1) == nil { |
|
|
|
|
t.Errorf("expected error, got nil") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
resp2 := &nethttp.Response{ |
|
|
|
|
Header: make(nethttp.Header), |
|
|
|
@ -211,17 +252,27 @@ func TestDefaultErrorDecoder(t *testing.T) { |
|
|
|
|
Body: io.NopCloser(bytes.NewBufferString("{\"code\":54321, \"message\": \"hi\", \"reason\": \"FOO\"}")), |
|
|
|
|
} |
|
|
|
|
err2 := DefaultErrorDecoder(context.TODO(), resp2) |
|
|
|
|
assert.Error(t, err2) |
|
|
|
|
assert.Equal(t, int32(500), err2.(*kratosErrors.Error).GetCode()) |
|
|
|
|
assert.Equal(t, "hi", err2.(*kratosErrors.Error).GetMessage()) |
|
|
|
|
assert.Equal(t, "FOO", err2.(*kratosErrors.Error).GetReason()) |
|
|
|
|
if err2 == nil { |
|
|
|
|
t.Errorf("expected error, got nil") |
|
|
|
|
} |
|
|
|
|
if !reflect.DeepEqual(int32(500), err2.(*kratosErrors.Error).GetCode()) { |
|
|
|
|
t.Errorf("expected %v, got %v", 500, err2.(*kratosErrors.Error).GetCode()) |
|
|
|
|
} |
|
|
|
|
if !reflect.DeepEqual("hi", err2.(*kratosErrors.Error).GetMessage()) { |
|
|
|
|
t.Errorf("expected %v, got %v", "hi", err2.(*kratosErrors.Error).GetMessage()) |
|
|
|
|
} |
|
|
|
|
if !reflect.DeepEqual("FOO", err2.(*kratosErrors.Error).GetReason()) { |
|
|
|
|
t.Errorf("expected %v, got %v", "FOO", err2.(*kratosErrors.Error).GetReason()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestCodecForResponse(t *testing.T) { |
|
|
|
|
resp := &nethttp.Response{Header: make(nethttp.Header)} |
|
|
|
|
resp.Header.Set("Content-Type", "application/xml") |
|
|
|
|
c := CodecForResponse(resp) |
|
|
|
|
assert.Equal(t, "xml", c.Name()) |
|
|
|
|
if !reflect.DeepEqual("xml", c.Name()) { |
|
|
|
|
t.Errorf("expected %v, got %v", "xml", c.Name()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestNewClient(t *testing.T) { |
|
|
|
|