From 668a7011c409ce390141298bd6dc9ea14fb64246 Mon Sep 17 00:00:00 2001 From: kevin Date: Sat, 31 Oct 2020 20:11:12 +0800 Subject: [PATCH] add more tests --- readme.md | 7 +++-- rest/httpx/requests_test.go | 52 +++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/readme.md b/readme.md index 2524caa5..cb984b0b 100644 --- a/readme.md +++ b/readme.md @@ -178,15 +178,14 @@ GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/tal-tech/ 如果您发现 ***bug*** 请及时提 ***issue***,我们会尽快确认并修改。 -开源中国年度评选,给 **go-zero** 投上一票:[https://www.oschina.net/p/go-zero](https://www.oschina.net/p/go-zero) - 为了防止广告用户、识别技术同行,请 ***star*** 后加我时注明 **github** 当前 ***star*** 数,我再拉进 **go-zero** 群,感谢! 加我之前有劳点一下 ***star***,一个小小的 ***star*** 是作者们回答海量问题的动力🤝 +wechat + 项目地址:[https://github.com/tal-tech/go-zero](https://github.com/tal-tech/go-zero) 码云地址:[https://gitee.com/kevwan/go-zero](https://gitee.com/kevwan/go-zero) (国内用户可访问gitee,每日自动从github同步代码) -wechat - +开源中国年度评选,给 **go-zero** 投上一票:[https://www.oschina.net/p/go-zero](https://www.oschina.net/p/go-zero) \ No newline at end of file diff --git a/rest/httpx/requests_test.go b/rest/httpx/requests_test.go index 68bc9f40..896b0eb1 100644 --- a/rest/httpx/requests_test.go +++ b/rest/httpx/requests_test.go @@ -19,9 +19,7 @@ func TestParseForm(t *testing.T) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello&age=18&percent=3.4", nil) assert.Nil(t, err) - - err = Parse(r, &v) - assert.Nil(t, err) + assert.Nil(t, Parse(r, &v)) assert.Equal(t, "hello", v.Name) assert.Equal(t, 18, v.Age) assert.Equal(t, 3.4, v.Percent) @@ -97,8 +95,44 @@ Content-Disposition: form-data; name="age" r := httptest.NewRequest(http.MethodPost, "http://localhost:3333/", strings.NewReader(body)) r.Header.Set(ContentType, "multipart/form-data; boundary=--------------------------220477612388154780019383") - err := Parse(r, &v) - assert.Nil(t, err) + assert.Nil(t, Parse(r, &v)) + assert.Equal(t, "kevin", v.Name) + assert.Equal(t, 18, v.Age) +} + +func TestParseMultipartFormWrongBoundary(t *testing.T) { + var v struct { + Name string `form:"name"` + Age int `form:"age"` + } + + body := strings.Replace(`----------------------------22047761238815478001938 +Content-Disposition: form-data; name="name" + +kevin +----------------------------22047761238815478001938 +Content-Disposition: form-data; name="age" + +18 +----------------------------22047761238815478001938--`, "\n", "\r\n", -1) + + r := httptest.NewRequest(http.MethodPost, "http://localhost:3333/", strings.NewReader(body)) + r.Header.Set(ContentType, "multipart/form-data; boundary=--------------------------220477612388154780019383") + + assert.NotNil(t, Parse(r, &v)) +} + +func TestParseJsonBody(t *testing.T) { + var v struct { + Name string `json:"name"` + Age int `json:"age"` + } + + body := `{"name":"kevin", "age": 18}` + r := httptest.NewRequest(http.MethodPost, "http://localhost:3333/", strings.NewReader(body)) + r.Header.Set(ContentType, ApplicationJson) + + assert.Nil(t, Parse(r, &v)) assert.Equal(t, "kevin", v.Name) assert.Equal(t, 18, v.Age) } @@ -111,9 +145,7 @@ func TestParseRequired(t *testing.T) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?name=hello", nil) assert.Nil(t, err) - - err = Parse(r, &v) - assert.NotNil(t, err) + assert.NotNil(t, Parse(r, &v)) } func TestParseOptions(t *testing.T) { @@ -123,9 +155,7 @@ func TestParseOptions(t *testing.T) { r, err := http.NewRequest(http.MethodGet, "http://hello.com/a?pos=4", nil) assert.Nil(t, err) - - err = Parse(r, &v) - assert.NotNil(t, err) + assert.NotNil(t, Parse(r, &v)) } func BenchmarkParseRaw(b *testing.B) {