diff --git a/binding/binding_body_test.go b/binding/binding_body_test.go deleted file mode 100644 index 901d429c..00000000 --- a/binding/binding_body_test.go +++ /dev/null @@ -1,72 +0,0 @@ -package binding - -import ( - "bytes" - "io/ioutil" - "testing" - - "github.com/gin-gonic/gin/testdata/protoexample" - "github.com/golang/protobuf/proto" - "github.com/stretchr/testify/assert" - "github.com/ugorji/go/codec" -) - -func TestBindingBody(t *testing.T) { - for _, tt := range []struct { - name string - binding BindingBody - body string - want string - }{ - { - name: "JSON binding", - binding: JSON, - body: `{"foo":"FOO"}`, - }, - { - name: "XML binding", - binding: XML, - body: ` - - FOO -`, - }, - { - name: "MsgPack binding", - binding: MsgPack, - body: msgPackBody(t), - }, - { - name: "YAML binding", - binding: YAML, - body: `foo: FOO`, - }, - } { - t.Logf("testing: %s", tt.name) - req := requestWithBody("POST", "/", tt.body) - form := FooStruct{} - body, _ := ioutil.ReadAll(req.Body) - assert.NoError(t, tt.binding.BindBody(body, &form)) - assert.Equal(t, FooStruct{"FOO"}, form) - } -} - -func msgPackBody(t *testing.T) string { - test := FooStruct{"FOO"} - h := new(codec.MsgpackHandle) - buf := bytes.NewBuffer(nil) - assert.NoError(t, codec.NewEncoder(buf, h).Encode(test)) - return buf.String() -} - -func TestBindingBodyProto(t *testing.T) { - test := protoexample.Test{ - Label: proto.String("FOO"), - } - data, _ := proto.Marshal(&test) - req := requestWithBody("POST", "/", string(data)) - form := protoexample.Test{} - body, _ := ioutil.ReadAll(req.Body) - assert.NoError(t, ProtoBuf.BindBody(body, &form)) - assert.Equal(t, test, form) -} diff --git a/binding/json_test.go b/binding/json_test.go new file mode 100644 index 00000000..cae4cccc --- /dev/null +++ b/binding/json_test.go @@ -0,0 +1,21 @@ +// Copyright 2019 Gin Core Team. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package binding + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestJSONBindingBindBody(t *testing.T) { + var s struct { + Foo string `json:"foo"` + } + err := jsonBinding{}.BindBody([]byte(`{"foo": "FOO"}`), &s) + require.NoError(t, err) + assert.Equal(t, "FOO", s.Foo) +} diff --git a/binding/msgpack_test.go b/binding/msgpack_test.go new file mode 100644 index 00000000..6baa6739 --- /dev/null +++ b/binding/msgpack_test.go @@ -0,0 +1,32 @@ +// Copyright 2019 Gin Core Team. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package binding + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/ugorji/go/codec" +) + +func TestMsgpackBindingBindBody(t *testing.T) { + type teststruct struct { + Foo string `msgpack:"foo"` + } + var s teststruct + err := msgpackBinding{}.BindBody(msgpackBody(t, teststruct{"FOO"}), &s) + require.NoError(t, err) + assert.Equal(t, "FOO", s.Foo) +} + +func msgpackBody(t *testing.T, obj interface{}) []byte { + var bs bytes.Buffer + h := &codec.MsgpackHandle{} + err := codec.NewEncoder(&bs, h).Encode(obj) + require.NoError(t, err) + return bs.Bytes() +} diff --git a/binding/xml_test.go b/binding/xml_test.go new file mode 100644 index 00000000..f9546c1a --- /dev/null +++ b/binding/xml_test.go @@ -0,0 +1,25 @@ +// Copyright 2019 Gin Core Team. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package binding + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestXMLBindingBindBody(t *testing.T) { + var s struct { + Foo string `xml:"foo"` + } + xmlBody := ` + + FOO +` + err := xmlBinding{}.BindBody([]byte(xmlBody), &s) + require.NoError(t, err) + assert.Equal(t, "FOO", s.Foo) +} diff --git a/binding/yaml_test.go b/binding/yaml_test.go new file mode 100644 index 00000000..e66338b7 --- /dev/null +++ b/binding/yaml_test.go @@ -0,0 +1,21 @@ +// Copyright 2019 Gin Core Team. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package binding + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestYAMLBindingBindBody(t *testing.T) { + var s struct { + Foo string `yaml:"foo"` + } + err := yamlBinding{}.BindBody([]byte("foo: FOO"), &s) + require.NoError(t, err) + assert.Equal(t, "FOO", s.Foo) +} diff --git a/vendor/vendor.json b/vendor/vendor.json index d441d4a6..70b2d9eb 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -112,6 +112,12 @@ "version": "v1.2", "versionExact": "v1.2.2" }, + { + "checksumSHA1": "wnEANt4k5X/KGwoFyfSSnpxULm4=", + "path": "github.com/stretchr/testify/require", + "revision": "f35b8ab0b5a2cef36673838d662e249dd9c94686", + "revisionTime": "2018-05-06T18:05:49Z" + }, { "checksumSHA1": "S4ei9eSqVThDio0Jn2sav6yUbvg=", "path": "github.com/ugorji/go/codec",