feat:add http client do With middleware

change lint and add client test
pull/2324/head
wangtingshun 2 years ago
parent 493013e087
commit 6c33cd6074
  1. 22
      transport/http/client.go
  2. 6
      transport/http/client_test.go
  3. 18
      transport/http/server_test.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. // 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. // 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) c := defaultCallInfo(req.URL.Path)
for _, o := range opts { for _, o := range opts {
if err := o.before(&c); err != nil { if err := o.before(&c); err != nil {
return nil, err return err
} }
} }
h := func(ctx context.Context, in interface{}) (interface{}, error) { 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 { if err != nil {
return nil, err 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 { if len(client.opts.middleware) > 0 {
h = middleware.Chain(client.opts.middleware...)(h) h = middleware.Chain(client.opts.middleware...)(h)
} }
resp, err := h(req.Context(), req) _, err := h(req.Context(), req)
if err != nil { return err
return nil, err
}
response, ok := resp.(*http.Response)
if ok {
return response, nil
}
return nil, errors.New(500, "Client DoWithMiddleware Failed", "response convert failed ")
} }
// Do send an HTTP request and decodes the body of response into target. // Do send an HTTP request and decodes the body of response into target.

@ -363,4 +363,10 @@ func TestNewClient(t *testing.T) {
if err == nil { if err == nil {
t.Error("err should be equal to encoder error") 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")
}
} }

@ -5,7 +5,6 @@ import (
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/go-kratos/kratos/v2/middleware"
"io" "io"
"net" "net"
"net/http" "net/http"
@ -14,6 +13,8 @@ import (
"testing" "testing"
"time" "time"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/internal/host" "github.com/go-kratos/kratos/v2/internal/host"
@ -241,26 +242,13 @@ func testClient(t *testing.T, srv *Server) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
resp, err := client.DoWithMiddleware(req) err = client.DoWithMiddleware(req, &res)
if errors.Code(err) != test.code { if errors.Code(err) != test.code {
t.Fatalf("want %v, but got %v", test, err) t.Fatalf("want %v, but got %v", test, err)
} }
if err != nil { if err != nil {
continue 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 { if res.Path != test.path {
t.Errorf("expected %s got %s", test.path, res.Path) t.Errorf("expected %s got %s", test.path, res.Path)
} }

Loading…
Cancel
Save