mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 17:20:24 +08:00
78ea0769fd
* feat: simplify httpc * chore: fix lint errors * chore: fix log url issue * chore: fix log url issue * refactor: handle resp & err in ResponseHandler * chore: remove unnecessary var names in return clause
42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
package httpc
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestDo(t *testing.T) {
|
|
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
}))
|
|
defer svr.Close()
|
|
req, err := http.NewRequest(http.MethodGet, svr.URL, nil)
|
|
assert.Nil(t, err)
|
|
resp, err := DoRequest(req)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
}
|
|
|
|
func TestDoNotFound(t *testing.T) {
|
|
svr := httptest.NewServer(http.NotFoundHandler())
|
|
defer svr.Close()
|
|
req, err := http.NewRequest(http.MethodPost, svr.URL, nil)
|
|
assert.Nil(t, err)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := DoRequest(req)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
|
}
|
|
|
|
func TestDoMoved(t *testing.T) {
|
|
svr := httptest.NewServer(http.RedirectHandler("/foo", http.StatusMovedPermanently))
|
|
defer svr.Close()
|
|
req, err := http.NewRequest(http.MethodGet, svr.URL, nil)
|
|
assert.Nil(t, err)
|
|
_, err = DoRequest(req)
|
|
// too many redirects
|
|
assert.NotNil(t, err)
|
|
}
|