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:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user