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