From 6c33cd6074181035aecda5d08534383e8eb4e695 Mon Sep 17 00:00:00 2001 From: wangtingshun Date: Mon, 29 Aug 2022 10:17:25 +0800 Subject: [PATCH] feat:add http client do With middleware change lint and add client test --- transport/http/client.go | 22 +++++++++------------- transport/http/client_test.go | 6 ++++++ transport/http/server_test.go | 18 +++--------------- 3 files changed, 18 insertions(+), 28 deletions(-) diff --git a/transport/http/client.go b/transport/http/client.go index 82425a7b9..ac198ed47 100644 --- a/transport/http/client.go +++ b/transport/http/client.go @@ -266,11 +266,11 @@ func (client *Client) invoke(ctx context.Context, req *http.Request, args interf // DoWithMiddleware send an HTTP request WithMiddleware and decodes the body of response into target. // returns an error (of type *Error) if the response status code is not 2xx. -func (client *Client) DoWithMiddleware(req *http.Request, opts ...CallOption) (*http.Response, error) { +func (client *Client) DoWithMiddleware(req *http.Request, reply interface{}, opts ...CallOption) error { c := defaultCallInfo(req.URL.Path) for _, o := range opts { if err := o.before(&c); err != nil { - return nil, err + return err } } h := func(ctx context.Context, in interface{}) (interface{}, error) { @@ -284,21 +284,17 @@ func (client *Client) DoWithMiddleware(req *http.Request, opts ...CallOption) (* if err != nil { return nil, err } - return res, nil + defer res.Body.Close() + if err := client.opts.decoder(ctx, res, reply); err != nil { + return nil, err + } + return reply, nil } if len(client.opts.middleware) > 0 { h = middleware.Chain(client.opts.middleware...)(h) } - resp, err := h(req.Context(), req) - if err != nil { - return nil, err - } - - response, ok := resp.(*http.Response) - if ok { - return response, nil - } - return nil, errors.New(500, "Client DoWithMiddleware Failed", "response convert failed ") + _, err := h(req.Context(), req) + return err } // Do send an HTTP request and decodes the body of response into target. diff --git a/transport/http/client_test.go b/transport/http/client_test.go index 4ee00456f..b9d3631b0 100644 --- a/transport/http/client_test.go +++ b/transport/http/client_test.go @@ -363,4 +363,10 @@ func TestNewClient(t *testing.T) { if err == nil { t.Error("err should be equal to encoder error") } + reqURL := fmt.Sprintf(client.target.Endpoint + "/go") + req, err := nethttp.NewRequest("POST", reqURL, nil) + err = client.DoWithMiddleware(req, nil, EmptyCallOption{}) + if err == nil { + t.Error("err should not be equal to nil") + } } diff --git a/transport/http/server_test.go b/transport/http/server_test.go index f00f4cb25..c3f6dc2f4 100644 --- a/transport/http/server_test.go +++ b/transport/http/server_test.go @@ -5,7 +5,6 @@ import ( "crypto/tls" "encoding/json" "fmt" - "github.com/go-kratos/kratos/v2/middleware" "io" "net" "net/http" @@ -14,6 +13,8 @@ import ( "testing" "time" + "github.com/go-kratos/kratos/v2/middleware" + "github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/internal/host" @@ -241,26 +242,13 @@ func testClient(t *testing.T, srv *Server) { if err != nil { t.Fatal(err) } - resp, err := client.DoWithMiddleware(req) + err = client.DoWithMiddleware(req, &res) if errors.Code(err) != test.code { t.Fatalf("want %v, but got %v", test, err) } if err != nil { continue } - if resp.StatusCode != 200 { - _ = resp.Body.Close() - t.Fatalf("http status got %d", resp.StatusCode) - } - content, err := io.ReadAll(resp.Body) - _ = resp.Body.Close() - if err != nil { - t.Fatalf("read resp error %v", err) - } - err = json.Unmarshal(content, &res) - if err != nil { - t.Fatalf("unmarshal resp error %v", err) - } if res.Path != test.path { t.Errorf("expected %s got %s", test.path, res.Path) }