add test cases for form

This commit is contained in:
thinkerou 2017-11-18 10:34:59 +08:00
parent dc56b1a1bb
commit 87d292482f

View File

@ -57,6 +57,12 @@ type InvalidNameType struct {
TestName string `invalid_name:"test_name"` TestName string `invalid_name:"test_name"`
} }
type InvalidNameMapType struct {
TestName struct {
MapFoo map[string]interface{} `form:"map_foo"`
}
}
type FooStructForSliceType struct { type FooStructForSliceType struct {
SliceFoo []int `form:"slice_foo"` SliceFoo []int `form:"slice_foo"`
} }
@ -223,6 +229,12 @@ func TestBindingFormInvalidName(t *testing.T) {
"test_name=bar", "bar2=foo") "test_name=bar", "bar2=foo")
} }
func TestBindingFormInvalidName2(t *testing.T) {
testFormBindingInvalidName2(t, "POST",
"/", "/",
"map_foo=bar", "bar2=foo")
}
func TestBindingFormForType(t *testing.T) { func TestBindingFormForType(t *testing.T) {
testFormBindingForType(t, "POST", testFormBindingForType(t, "POST",
"/", "/", "/", "/",
@ -649,6 +661,24 @@ func testFormBindingInvalidName(t *testing.T, method, path, badPath, body, badBo
assert.Error(t, err) assert.Error(t, err)
} }
func testFormBindingInvalidName2(t *testing.T, method, path, badPath, body, badBody string) {
b := Form
assert.Equal(t, b.Name(), "form")
obj := InvalidNameMapType{}
req := requestWithBody(method, path, body)
if method == "POST" {
req.Header.Add("Content-Type", MIMEPOSTForm)
}
err := b.Bind(req, &obj)
assert.Error(t, err)
obj = InvalidNameMapType{}
req = requestWithBody(method, badPath, badBody)
err = JSON.Bind(req, &obj)
assert.Error(t, err)
}
func testFormBindingForType(t *testing.T, method, path, badPath, body, badBody string, typ string) { func testFormBindingForType(t *testing.T, method, path, badPath, body, badBody string, typ string) {
b := Form b := Form
assert.Equal(t, b.Name(), "form") assert.Equal(t, b.Name(), "form")