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.
// 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.

@ -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")
}
}

@ -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)
}

Loading…
Cancel
Save