feat: add httpc.Do & httpc.Service.Do (#1775)

* backup

* backup

* backup

* feat: add httpc.Do & httpc.Service.Do

* fix: not using strings.Cut, it's from Go 1.18

* chore: remove redudant code

* feat: httpc.Do finished

* chore: fix reviewdog

* chore: break loop if found

* add more tests
This commit is contained in:
Kevin Wan
2022-04-11 11:00:28 +08:00
committed by GitHub
parent fabea4c448
commit 50de01fb49
8 changed files with 680 additions and 10 deletions

View File

@@ -1,8 +1,17 @@
package httpc
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
nurl "net/url"
"strings"
"github.com/zeromicro/go-zero/core/lang"
"github.com/zeromicro/go-zero/core/mapping"
"github.com/zeromicro/go-zero/rest/httpc/internal"
)
@@ -10,6 +19,17 @@ var interceptors = []internal.Interceptor{
internal.LogInterceptor,
}
// Do sends an HTTP request with the given arguments and returns an HTTP response.
// data is automatically marshal into a *httpRequest, typically it's defined in an API file.
func Do(ctx context.Context, method, url string, data interface{}) (*http.Response, error) {
req, err := buildRequest(ctx, method, url, data)
if err != nil {
return nil, err
}
return DoRequest(req)
}
// DoRequest sends an HTTP request and returns an HTTP response.
func DoRequest(r *http.Request) (*http.Response, error) {
return request(r, defaultClient{})
@@ -27,6 +47,107 @@ func (c defaultClient) do(r *http.Request) (*http.Response, error) {
return http.DefaultClient.Do(r)
}
func buildFormQuery(u *nurl.URL, val map[string]interface{}) string {
query := u.Query()
for k, v := range val {
query.Add(k, fmt.Sprint(v))
}
return query.Encode()
}
func buildRequest(ctx context.Context, method, url string, data interface{}) (*http.Request, error) {
u, err := nurl.Parse(url)
if err != nil {
return nil, err
}
var val map[string]map[string]interface{}
if data != nil {
val, err = mapping.Marshal(data)
if err != nil {
return nil, err
}
}
if err := fillPath(u, val[pathKey]); err != nil {
return nil, err
}
var reader io.Reader
jsonVars, hasJsonBody := val[jsonKey]
if hasJsonBody {
if method == http.MethodGet {
return nil, ErrGetWithBody
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
if err := enc.Encode(jsonVars); err != nil {
return nil, err
}
reader = &buf
}
req, err := http.NewRequestWithContext(ctx, method, u.String(), reader)
if err != nil {
return nil, err
}
req.URL.RawQuery = buildFormQuery(u, val[formKey])
fillHeader(req, val[headerKey])
if hasJsonBody {
req.Header.Set(contentType, applicationJson)
}
return req, nil
}
func fillHeader(r *http.Request, val map[string]interface{}) {
for k, v := range val {
r.Header.Add(k, fmt.Sprint(v))
}
}
func fillPath(u *nurl.URL, val map[string]interface{}) error {
used := make(map[string]lang.PlaceholderType)
fields := strings.Split(u.Path, slash)
for i := range fields {
field := fields[i]
if len(field) > 0 && field[0] == colon {
name := field[1:]
ival, ok := val[name]
if !ok {
return fmt.Errorf("missing path variable %q", name)
}
value := fmt.Sprint(ival)
if len(value) == 0 {
return fmt.Errorf("empty path variable %q", name)
}
fields[i] = value
used[name] = lang.Placeholder
}
}
if len(val) != len(used) {
for key := range used {
delete(val, key)
}
var unused []string
for key := range val {
unused = append(unused, key)
}
return fmt.Errorf("more path variables are provided: %q", strings.Join(unused, ", "))
}
u.Path = strings.Join(fields, slash)
return nil
}
func request(r *http.Request, cli client) (*http.Response, error) {
var respHandlers []internal.ResponseHandler
for _, interceptor := range interceptors {

View File

@@ -1,14 +1,17 @@
package httpc
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/rest/httpx"
"github.com/zeromicro/go-zero/rest/router"
)
func TestDo(t *testing.T) {
func TestDoRequest(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))
defer svr.Close()
@@ -19,7 +22,7 @@ func TestDo(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
func TestDoNotFound(t *testing.T) {
func TestDoRequest_NotFound(t *testing.T) {
svr := httptest.NewServer(http.NotFoundHandler())
defer svr.Close()
req, err := http.NewRequest(http.MethodPost, svr.URL, nil)
@@ -30,7 +33,7 @@ func TestDoNotFound(t *testing.T) {
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
}
func TestDoMoved(t *testing.T) {
func TestDoRequest_Moved(t *testing.T) {
svr := httptest.NewServer(http.RedirectHandler("/foo", http.StatusMovedPermanently))
defer svr.Close()
req, err := http.NewRequest(http.MethodGet, svr.URL, nil)
@@ -39,3 +42,84 @@ func TestDoMoved(t *testing.T) {
// too many redirects
assert.NotNil(t, err)
}
func TestDo(t *testing.T) {
type Data struct {
Key string `path:"key"`
Value int `form:"value"`
Header string `header:"X-Header"`
Body string `json:"body"`
}
rt := router.NewRouter()
err := rt.Handle(http.MethodPost, "/nodes/:key",
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req Data
assert.Nil(t, httpx.Parse(r, &req))
}))
assert.Nil(t, err)
svr := httptest.NewServer(http.HandlerFunc(rt.ServeHTTP))
defer svr.Close()
data := Data{
Key: "foo",
Value: 10,
Header: "my-header",
Body: "my body",
}
resp, err := Do(context.Background(), http.MethodPost, svr.URL+"/nodes/:key", data)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
func TestDo_BadRequest(t *testing.T) {
_, err := Do(context.Background(), http.MethodPost, ":/nodes/:key", nil)
assert.NotNil(t, err)
val1 := struct {
Value string `json:"value,options=[a,b]"`
}{
Value: "c",
}
_, err = Do(context.Background(), http.MethodPost, "/nodes/:key", val1)
assert.NotNil(t, err)
val2 := struct {
Value string `path:"val"`
}{
Value: "",
}
_, err = Do(context.Background(), http.MethodPost, "/nodes/:key", val2)
assert.NotNil(t, err)
val3 := struct {
Value string `path:"key"`
Body string `json:"body"`
}{
Value: "foo",
}
_, err = Do(context.Background(), http.MethodGet, "/nodes/:key", val3)
assert.NotNil(t, err)
_, err = Do(context.Background(), "\n", "rtmp://nodes", nil)
assert.NotNil(t, err)
val4 := struct {
Value string `path:"val"`
}{
Value: "",
}
_, err = Do(context.Background(), http.MethodPost, "/nodes/:val", val4)
assert.NotNil(t, err)
val5 := struct {
Value string `path:"val"`
Another int `path:"foo"`
}{
Value: "1",
Another: 2,
}
_, err = Do(context.Background(), http.MethodPost, "/nodes/:val", val5)
assert.NotNil(t, err)
}

View File

@@ -1,6 +1,7 @@
package httpc
import (
"context"
"net/http"
"github.com/zeromicro/go-zero/core/breaker"
@@ -12,6 +13,8 @@ type (
// Service represents a remote HTTP service.
Service interface {
// Do sends an HTTP request with the given arguments and returns an HTTP response.
Do(ctx context.Context, method, url string, data interface{}) (*http.Response, error)
// DoRequest sends a HTTP request to the service.
DoRequest(r *http.Request) (*http.Response, error)
}
@@ -39,6 +42,16 @@ func NewServiceWithClient(name string, cli *http.Client, opts ...Option) Service
}
}
// Do sends an HTTP request with the given arguments and returns an HTTP response.
func (s namedService) Do(ctx context.Context, method, url string, data interface{}) (*http.Response, error) {
req, err := buildRequest(ctx, method, url, data)
if err != nil {
return nil, err
}
return s.DoRequest(req)
}
// DoRequest sends an HTTP request to the service.
func (s namedService) DoRequest(r *http.Request) (*http.Response, error) {
return request(r, s)

View File

@@ -1,6 +1,7 @@
package httpc
import (
"context"
"net/http"
"net/http/httptest"
"testing"
@@ -8,7 +9,7 @@ import (
"github.com/stretchr/testify/assert"
)
func TestNamedService_Do(t *testing.T) {
func TestNamedService_DoRequest(t *testing.T) {
svr := httptest.NewServer(http.RedirectHandler("/foo", http.StatusMovedPermanently))
defer svr.Close()
req, err := http.NewRequest(http.MethodGet, svr.URL, nil)
@@ -19,7 +20,7 @@ func TestNamedService_Do(t *testing.T) {
assert.NotNil(t, err)
}
func TestNamedService_Get(t *testing.T) {
func TestNamedService_DoRequestGet(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("foo", r.Header.Get("foo"))
}))
@@ -36,7 +37,7 @@ func TestNamedService_Get(t *testing.T) {
assert.Equal(t, "bar", resp.Header.Get("foo"))
}
func TestNamedService_Post(t *testing.T) {
func TestNamedService_DoRequestPost(t *testing.T) {
svr := httptest.NewServer(http.NotFoundHandler())
defer svr.Close()
service := NewService("foo")
@@ -47,3 +48,38 @@ func TestNamedService_Post(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
}
func TestNamedService_Do(t *testing.T) {
type Data struct {
Key string `path:"key"`
Value int `form:"value"`
Header string `header:"X-Header"`
Body string `json:"body"`
}
svr := httptest.NewServer(http.NotFoundHandler())
defer svr.Close()
service := NewService("foo")
data := Data{
Key: "foo",
Value: 10,
Header: "my-header",
Body: "my body",
}
resp, err := service.Do(context.Background(), http.MethodPost, svr.URL+"/nodes/:key", data)
assert.Nil(t, err)
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
}
func TestNamedService_DoBadRequest(t *testing.T) {
val := struct {
Value string `json:"value,options=[a,b]"`
}{
Value: "c",
}
service := NewService("foo")
_, err := service.Do(context.Background(), http.MethodPost, "/nodes/:key", val)
assert.NotNil(t, err)
}

View File

@@ -1,6 +1,17 @@
package httpc
import "errors"
const (
pathKey = "path"
formKey = "form"
headerKey = "header"
jsonKey = "json"
slash = "/"
colon = ':'
contentType = "Content-Type"
applicationJson = "application/json"
)
// ErrGetWithBody indicates that GET request with body.
var ErrGetWithBody = errors.New("HTTP GET should not have body")