|
|
@ -10,13 +10,12 @@ import ( |
|
|
|
"io" |
|
|
|
"io" |
|
|
|
"log" |
|
|
|
"log" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
nethttp "net/http" |
|
|
|
|
|
|
|
"reflect" |
|
|
|
"reflect" |
|
|
|
"strconv" |
|
|
|
"strconv" |
|
|
|
"testing" |
|
|
|
"testing" |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
kratosErrors "github.com/go-kratos/kratos/v2/errors" |
|
|
|
kratoserrors "github.com/go-kratos/kratos/v2/errors" |
|
|
|
"github.com/go-kratos/kratos/v2/middleware" |
|
|
|
"github.com/go-kratos/kratos/v2/middleware" |
|
|
|
"github.com/go-kratos/kratos/v2/registry" |
|
|
|
"github.com/go-kratos/kratos/v2/registry" |
|
|
|
"github.com/go-kratos/kratos/v2/selector" |
|
|
|
"github.com/go-kratos/kratos/v2/selector" |
|
|
@ -24,7 +23,7 @@ import ( |
|
|
|
|
|
|
|
|
|
|
|
type mockRoundTripper struct{} |
|
|
|
type mockRoundTripper struct{} |
|
|
|
|
|
|
|
|
|
|
|
func (rt *mockRoundTripper) RoundTrip(req *nethttp.Request) (resp *nethttp.Response, err error) { |
|
|
|
func (rt *mockRoundTripper) RoundTrip(req *http.Request) (resp *http.Response, err error) { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -129,7 +128,7 @@ func TestWithRequestEncoder(t *testing.T) { |
|
|
|
|
|
|
|
|
|
|
|
func TestWithResponseDecoder(t *testing.T) { |
|
|
|
func TestWithResponseDecoder(t *testing.T) { |
|
|
|
o := &clientOptions{} |
|
|
|
o := &clientOptions{} |
|
|
|
v := func(ctx context.Context, res *nethttp.Response, out interface{}) error { return nil } |
|
|
|
v := func(ctx context.Context, res *http.Response, out interface{}) error { return nil } |
|
|
|
WithResponseDecoder(v)(o) |
|
|
|
WithResponseDecoder(v)(o) |
|
|
|
if o.decoder == nil { |
|
|
|
if o.decoder == nil { |
|
|
|
t.Errorf("expected encoder to be not nil") |
|
|
|
t.Errorf("expected encoder to be not nil") |
|
|
@ -138,7 +137,7 @@ func TestWithResponseDecoder(t *testing.T) { |
|
|
|
|
|
|
|
|
|
|
|
func TestWithErrorDecoder(t *testing.T) { |
|
|
|
func TestWithErrorDecoder(t *testing.T) { |
|
|
|
o := &clientOptions{} |
|
|
|
o := &clientOptions{} |
|
|
|
v := func(ctx context.Context, res *nethttp.Response) error { return nil } |
|
|
|
v := func(ctx context.Context, res *http.Response) error { return nil } |
|
|
|
WithErrorDecoder(v)(o) |
|
|
|
WithErrorDecoder(v)(o) |
|
|
|
if o.errorDecoder == nil { |
|
|
|
if o.errorDecoder == nil { |
|
|
|
t.Errorf("expected encoder to be not nil") |
|
|
|
t.Errorf("expected encoder to be not nil") |
|
|
@ -199,27 +198,24 @@ func TestWithNodeFilter(t *testing.T) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestDefaultRequestEncoder(t *testing.T) { |
|
|
|
func TestDefaultRequestEncoder(t *testing.T) { |
|
|
|
req1 := &nethttp.Request{ |
|
|
|
r, _ := http.NewRequest(http.MethodPost, "", io.NopCloser(bytes.NewBufferString(`{"a":"1", "b": 2}`))) |
|
|
|
Header: make(nethttp.Header), |
|
|
|
r.Header.Set("Content-Type", "application/xml") |
|
|
|
Body: io.NopCloser(bytes.NewBufferString("{\"a\":\"1\", \"b\": 2}")), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
req1.Header.Set("Content-Type", "application/xml") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
v1 := &struct { |
|
|
|
v1 := &struct { |
|
|
|
A string `json:"a"` |
|
|
|
A string `json:"a"` |
|
|
|
B int64 `json:"b"` |
|
|
|
B int64 `json:"b"` |
|
|
|
}{"a", 1} |
|
|
|
}{"a", 1} |
|
|
|
b, err1 := DefaultRequestEncoder(context.TODO(), "application/json", v1) |
|
|
|
b, err := DefaultRequestEncoder(context.TODO(), "application/json", v1) |
|
|
|
if err1 != nil { |
|
|
|
if err != nil { |
|
|
|
t.Errorf("expected no error, got %v", err1) |
|
|
|
t.Fatal(err) |
|
|
|
} |
|
|
|
} |
|
|
|
v1b := &struct { |
|
|
|
v1b := &struct { |
|
|
|
A string `json:"a"` |
|
|
|
A string `json:"a"` |
|
|
|
B int64 `json:"b"` |
|
|
|
B int64 `json:"b"` |
|
|
|
}{} |
|
|
|
}{} |
|
|
|
err1 = json.Unmarshal(b, v1b) |
|
|
|
err = json.Unmarshal(b, v1b) |
|
|
|
if err1 != nil { |
|
|
|
if err != nil { |
|
|
|
t.Errorf("expected no error, got %v", err1) |
|
|
|
t.Fatal(err) |
|
|
|
} |
|
|
|
} |
|
|
|
if !reflect.DeepEqual(v1b, v1) { |
|
|
|
if !reflect.DeepEqual(v1b, v1) { |
|
|
|
t.Errorf("expected %v, got %v", v1, v1b) |
|
|
|
t.Errorf("expected %v, got %v", v1, v1b) |
|
|
@ -227,8 +223,8 @@ func TestDefaultRequestEncoder(t *testing.T) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestDefaultResponseDecoder(t *testing.T) { |
|
|
|
func TestDefaultResponseDecoder(t *testing.T) { |
|
|
|
resp1 := &nethttp.Response{ |
|
|
|
resp1 := &http.Response{ |
|
|
|
Header: make(nethttp.Header), |
|
|
|
Header: make(http.Header), |
|
|
|
StatusCode: 200, |
|
|
|
StatusCode: 200, |
|
|
|
Body: io.NopCloser(bytes.NewBufferString("{\"a\":\"1\", \"b\": 2}")), |
|
|
|
Body: io.NopCloser(bytes.NewBufferString("{\"a\":\"1\", \"b\": 2}")), |
|
|
|
} |
|
|
|
} |
|
|
@ -236,19 +232,19 @@ func TestDefaultResponseDecoder(t *testing.T) { |
|
|
|
A string `json:"a"` |
|
|
|
A string `json:"a"` |
|
|
|
B int64 `json:"b"` |
|
|
|
B int64 `json:"b"` |
|
|
|
}{} |
|
|
|
}{} |
|
|
|
err1 := DefaultResponseDecoder(context.TODO(), resp1, &v1) |
|
|
|
err := DefaultResponseDecoder(context.TODO(), resp1, &v1) |
|
|
|
if err1 != nil { |
|
|
|
if err != nil { |
|
|
|
t.Errorf("expected no error, got %v", err1) |
|
|
|
t.Fatal(err) |
|
|
|
} |
|
|
|
} |
|
|
|
if !reflect.DeepEqual("1", v1.A) { |
|
|
|
if v1.A != "1" { |
|
|
|
t.Errorf("expected %v, got %v", "1", v1.A) |
|
|
|
t.Errorf("expected %v, got %v", "1", v1.A) |
|
|
|
} |
|
|
|
} |
|
|
|
if !reflect.DeepEqual(int64(2), v1.B) { |
|
|
|
if v1.B != int64(2) { |
|
|
|
t.Errorf("expected %v, got %v", 2, v1.B) |
|
|
|
t.Errorf("expected %v, got %v", 2, v1.B) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
resp2 := &nethttp.Response{ |
|
|
|
resp2 := &http.Response{ |
|
|
|
Header: make(nethttp.Header), |
|
|
|
Header: make(http.Header), |
|
|
|
StatusCode: 200, |
|
|
|
StatusCode: 200, |
|
|
|
Body: io.NopCloser(bytes.NewBufferString("{badjson}")), |
|
|
|
Body: io.NopCloser(bytes.NewBufferString("{badjson}")), |
|
|
|
} |
|
|
|
} |
|
|
@ -256,22 +252,22 @@ func TestDefaultResponseDecoder(t *testing.T) { |
|
|
|
A string `json:"a"` |
|
|
|
A string `json:"a"` |
|
|
|
B int64 `json:"b"` |
|
|
|
B int64 `json:"b"` |
|
|
|
}{} |
|
|
|
}{} |
|
|
|
err2 := DefaultResponseDecoder(context.TODO(), resp2, &v2) |
|
|
|
err = DefaultResponseDecoder(context.TODO(), resp2, &v2) |
|
|
|
terr1 := &json.SyntaxError{} |
|
|
|
syntaxErr := &json.SyntaxError{} |
|
|
|
if !errors.As(err2, &terr1) { |
|
|
|
if !errors.As(err, &syntaxErr) { |
|
|
|
t.Errorf("expected %v, got %v", terr1, err2) |
|
|
|
t.Errorf("expected %v, got %v", syntaxErr, err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestDefaultErrorDecoder(t *testing.T) { |
|
|
|
func TestDefaultErrorDecoder(t *testing.T) { |
|
|
|
for i := 200; i < 300; i++ { |
|
|
|
for i := 200; i < 300; i++ { |
|
|
|
resp := &nethttp.Response{Header: make(nethttp.Header), StatusCode: i} |
|
|
|
resp := &http.Response{Header: make(http.Header), StatusCode: i} |
|
|
|
if DefaultErrorDecoder(context.TODO(), resp) != nil { |
|
|
|
if DefaultErrorDecoder(context.TODO(), resp) != nil { |
|
|
|
t.Errorf("expected no error, got %v", DefaultErrorDecoder(context.TODO(), resp)) |
|
|
|
t.Errorf("expected no error, got %v", DefaultErrorDecoder(context.TODO(), resp)) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
resp1 := &nethttp.Response{ |
|
|
|
resp1 := &http.Response{ |
|
|
|
Header: make(nethttp.Header), |
|
|
|
Header: make(http.Header), |
|
|
|
StatusCode: 300, |
|
|
|
StatusCode: 300, |
|
|
|
Body: io.NopCloser(bytes.NewBufferString("{\"foo\":\"bar\"}")), |
|
|
|
Body: io.NopCloser(bytes.NewBufferString("{\"foo\":\"bar\"}")), |
|
|
|
} |
|
|
|
} |
|
|
@ -279,28 +275,28 @@ func TestDefaultErrorDecoder(t *testing.T) { |
|
|
|
t.Errorf("expected error, got nil") |
|
|
|
t.Errorf("expected error, got nil") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
resp2 := &nethttp.Response{ |
|
|
|
resp2 := &http.Response{ |
|
|
|
Header: make(nethttp.Header), |
|
|
|
Header: make(http.Header), |
|
|
|
StatusCode: 500, |
|
|
|
StatusCode: 500, |
|
|
|
Body: io.NopCloser(bytes.NewBufferString("{\"code\":54321, \"message\": \"hi\", \"reason\": \"FOO\"}")), |
|
|
|
Body: io.NopCloser(bytes.NewBufferString("{\"code\":54321, \"message\": \"hi\", \"reason\": \"FOO\"}")), |
|
|
|
} |
|
|
|
} |
|
|
|
err2 := DefaultErrorDecoder(context.TODO(), resp2) |
|
|
|
err := DefaultErrorDecoder(context.TODO(), resp2) |
|
|
|
if err2 == nil { |
|
|
|
if err == nil { |
|
|
|
t.Errorf("expected error, got nil") |
|
|
|
t.Errorf("expected error, got nil") |
|
|
|
} |
|
|
|
} |
|
|
|
if !reflect.DeepEqual(int32(500), err2.(*kratosErrors.Error).Code) { |
|
|
|
if err.(*kratoserrors.Error).Code != int32(500) { |
|
|
|
t.Errorf("expected %v, got %v", 500, err2.(*kratosErrors.Error).Code) |
|
|
|
t.Errorf("expected %v, got %v", 500, err.(*kratoserrors.Error).Code) |
|
|
|
} |
|
|
|
} |
|
|
|
if !reflect.DeepEqual("hi", err2.(*kratosErrors.Error).Message) { |
|
|
|
if err.(*kratoserrors.Error).Message != "hi" { |
|
|
|
t.Errorf("expected %v, got %v", "hi", err2.(*kratosErrors.Error).Message) |
|
|
|
t.Errorf("expected %v, got %v", "hi", err.(*kratoserrors.Error).Message) |
|
|
|
} |
|
|
|
} |
|
|
|
if !reflect.DeepEqual("FOO", err2.(*kratosErrors.Error).Reason) { |
|
|
|
if err.(*kratoserrors.Error).Reason != "FOO" { |
|
|
|
t.Errorf("expected %v, got %v", "FOO", err2.(*kratosErrors.Error).Reason) |
|
|
|
t.Errorf("expected %v, got %v", "FOO", err.(*kratoserrors.Error).Reason) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestCodecForResponse(t *testing.T) { |
|
|
|
func TestCodecForResponse(t *testing.T) { |
|
|
|
resp := &nethttp.Response{Header: make(nethttp.Header)} |
|
|
|
resp := &http.Response{Header: make(http.Header)} |
|
|
|
resp.Header.Set("Content-Type", "application/xml") |
|
|
|
resp.Header.Set("Content-Type", "application/xml") |
|
|
|
c := CodecForResponse(resp) |
|
|
|
c := CodecForResponse(resp) |
|
|
|
if !reflect.DeepEqual("xml", c.Name()) { |
|
|
|
if !reflect.DeepEqual("xml", c.Name()) { |
|
|
@ -346,7 +342,7 @@ func TestNewClient(t *testing.T) { |
|
|
|
}), |
|
|
|
}), |
|
|
|
) |
|
|
|
) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Error(err) |
|
|
|
t.Fatal(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err = client.Invoke(context.Background(), http.MethodPost, "/go", map[string]string{"name": "kratos"}, nil, EmptyCallOption{}, &mockCallOption{}) |
|
|
|
err = client.Invoke(context.Background(), http.MethodPost, "/go", map[string]string{"name": "kratos"}, nil, EmptyCallOption{}, &mockCallOption{}) |
|
|
|