test: add more tests (#1856)
This commit is contained in:
@@ -158,3 +158,32 @@ func TestDo_BadRequest(t *testing.T) {
|
||||
_, err = Do(context.Background(), http.MethodPost, "/nodes/:val", val5)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestDo_Json(t *testing.T) {
|
||||
type Data struct {
|
||||
Key string `path:"key"`
|
||||
Value int `form:"value"`
|
||||
Header string `header:"X-Header"`
|
||||
Body chan int `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: make(chan int),
|
||||
}
|
||||
_, err = Do(context.Background(), http.MethodPost, svr.URL+"/nodes/:key", data)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
@@ -129,7 +129,18 @@ func TestWriteJsonTimeout(t *testing.T) {
|
||||
// only log it and ignore
|
||||
w := tracedResponseWriter{
|
||||
headers: make(map[string][]string),
|
||||
timeout: true,
|
||||
err: http.ErrHandlerTimeout,
|
||||
}
|
||||
msg := message{Name: "anyone"}
|
||||
WriteJson(&w, http.StatusOK, msg)
|
||||
assert.Equal(t, http.StatusOK, w.code)
|
||||
}
|
||||
|
||||
func TestWriteJsonError(t *testing.T) {
|
||||
// only log it and ignore
|
||||
w := tracedResponseWriter{
|
||||
headers: make(map[string][]string),
|
||||
err: errors.New("foo"),
|
||||
}
|
||||
msg := message{Name: "anyone"}
|
||||
WriteJson(&w, http.StatusOK, msg)
|
||||
@@ -162,8 +173,8 @@ type tracedResponseWriter struct {
|
||||
hasBody bool
|
||||
code int
|
||||
lessWritten bool
|
||||
timeout bool
|
||||
wroteHeader bool
|
||||
err error
|
||||
}
|
||||
|
||||
func (w *tracedResponseWriter) Header() http.Header {
|
||||
@@ -171,8 +182,8 @@ func (w *tracedResponseWriter) Header() http.Header {
|
||||
}
|
||||
|
||||
func (w *tracedResponseWriter) Write(bytes []byte) (n int, err error) {
|
||||
if w.timeout {
|
||||
return 0, http.ErrHandlerTimeout
|
||||
if w.err != nil {
|
||||
return 0, w.err
|
||||
}
|
||||
|
||||
n, err = w.builder.Write(bytes)
|
||||
|
||||
Reference in New Issue
Block a user