add test file

This commit is contained in:
guonaihong 2019-10-15 19:45:41 +08:00
parent 48e871a76d
commit 5c8421f8e8
2 changed files with 62 additions and 3 deletions

View File

@ -35,12 +35,12 @@ type QueryTest struct {
} }
type FooStruct struct { type FooStruct struct {
Foo string `msgpack:"foo" json:"foo" form:"foo" xml:"foo" binding:"required"` Foo string `msgpack:"foo" json:"foo" form:"foo" xml:"foo" query:"foo" binding:"required"`
} }
type FooBarStruct struct { type FooBarStruct struct {
FooStruct FooStruct
Bar string `msgpack:"bar" json:"bar" form:"bar" xml:"bar" binding:"required"` Bar string `msgpack:"bar" json:"bar" form:"bar" xml:"bar" query:"bar" binding:"required"`
} }
type FooBarFileStruct struct { type FooBarFileStruct struct {
@ -93,7 +93,7 @@ type FooStructForTimeTypeFailLocation struct {
} }
type FooStructForMapType struct { type FooStructForMapType struct {
MapFoo map[string]interface{} `form:"map_foo"` MapFoo map[string]interface{} `form:"map_foo" query:"map_foo"`
} }
type FooStructForIgnoreFormTag struct { type FooStructForIgnoreFormTag struct {
@ -340,6 +340,64 @@ func TestBindingFormForType(t *testing.T) {
"", "", "StructPointer") "", "", "StructPointer")
} }
type testBindQuery2 struct {
method string
path string
badPath string
body string
badBody string
}
func TestBindingQuery22(t *testing.T) {
td := []testBindQuery2{
{method: "POST", path: "/?foo=bar&bar=foo", badPath: "/", body: "foo=unused", badBody: "bar2=foo"},
{method: "GET", path: "/?foo=bar&bar=foo", badPath: "/?bar2=foo", body: "foo=unused", badBody: ""},
}
testQueryBinding2 := func(method, path, badPath, body, badBody string) {
b := Query2
assert.Equal(t, "query", b.Name())
obj := FooBarStruct{}
req := requestWithBody(method, path, body)
if method == "POST" {
req.Header.Add("Content-Type", MIMEPOSTForm)
}
err := b.Bind(req, &obj)
assert.NoError(t, err)
assert.Equal(t, "bar", obj.Foo)
assert.Equal(t, "foo", obj.Bar)
}
for _, v := range td {
testQueryBinding2(v.method, v.path, v.badPath, v.body, v.badBody)
}
}
func TestBindingQuery2Fail(t *testing.T) {
td := []testBindQuery2{
{method: "POST", path: "/?map_foo=", badPath: "/", body: "map_foo=unused", badBody: "bar2=foo"},
{method: "GET", path: "/?map_foo=o", badPath: "/?bar2=foo", body: "map_foo=unused", badBody: ""},
}
testQueryBindingFail2 := func(method, path, badPath, body, badBody string) {
b := Query2
assert.Equal(t, "query", b.Name())
obj := FooStructForMapType{}
req := requestWithBody(method, path, body)
if method == "POST" {
req.Header.Add("Content-Type", MIMEPOSTForm)
}
err := b.Bind(req, &obj)
assert.Error(t, err)
}
for _, v := range td {
testQueryBindingFail2(v.method, v.path, v.badPath, v.body, v.badBody)
}
}
func TestBindingQuery(t *testing.T) { func TestBindingQuery(t *testing.T) {
testQueryBinding(t, "POST", testQueryBinding(t, "POST",
"/?foo=bar&bar=foo", "/", "/?foo=bar&bar=foo", "/",

1
go.sum
View File

@ -1,4 +1,5 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=