test: add more tests (#1856)

This commit is contained in:
Kevin Wan
2022-05-02 21:24:20 +08:00
committed by GitHub
parent d0a58d1f2d
commit f21970c117
6 changed files with 194 additions and 5 deletions

View File

@@ -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)
}