Relocate binding body tests (#2086)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Relocate binding body tests Every test file should be related to a tested file. Remove useless tests. * Add github.com/stretchr/testify/require package
authored by Dmitry Kutakov · committed by thinkerou · parent 4fd3234 · Browse files · 6 files·+105 added−72 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+105 added−72 removed
- other:+105 added−72 removed
Provenance
- Commit
f7becac- Tree
8b93eb2- Author
Dmitry Kutakov <vkd.castle@gmail.com>- Committer
thinkerou <thinkerou@gmail.com>- Parent
4fd3234
6 files changed+105 added−72 removed
deleted:binding/binding_body_test.go-72 removed
1-package binding2-3-import (4- "bytes"5- "io/ioutil"6- "testing"7-8- "github.com/gin-gonic/gin/testdata/protoexample"9- "github.com/golang/protobuf/proto"10- "github.com/stretchr/testify/assert"11- "github.com/ugorji/go/codec"12-)13-14-func TestBindingBody(t *testing.T) {15- for _, tt := range []struct {16- name string17- binding BindingBody18- body string19- want string20- }{21- {22- name: "JSON binding",23- binding: JSON,24- body: `{"foo":"FOO"}`,25- },26- {27- name: "XML binding",28- binding: XML,29- body: `<?xml version="1.0" encoding="UTF-8"?>30-<root>31- <foo>FOO</foo>32-</root>`,33- },34- {35- name: "MsgPack binding",36- binding: MsgPack,37- body: msgPackBody(t),38- },39- {40- name: "YAML binding",41- binding: YAML,42- body: `foo: FOO`,43- },44- } {45- t.Logf("testing: %s", tt.name)46- req := requestWithBody("POST", "/", tt.body)47- form := FooStruct{}48- body, _ := ioutil.ReadAll(req.Body)49- assert.NoError(t, tt.binding.BindBody(body, &form))50- assert.Equal(t, FooStruct{"FOO"}, form)51- }52-}53-54-func msgPackBody(t *testing.T) string {55- test := FooStruct{"FOO"}56- h := new(codec.MsgpackHandle)57- buf := bytes.NewBuffer(nil)58- assert.NoError(t, codec.NewEncoder(buf, h).Encode(test))59- return buf.String()60-}61-62-func TestBindingBodyProto(t *testing.T) {63- test := protoexample.Test{64- Label: proto.String("FOO"),65- }66- data, _ := proto.Marshal(&test)67- req := requestWithBody("POST", "/", string(data))68- form := protoexample.Test{}69- body, _ := ioutil.ReadAll(req.Body)70- assert.NoError(t, ProtoBuf.BindBody(body, &form))71- assert.Equal(t, test, form)72-}added:binding/json_test.go+21 added
1+// Copyright 2019 Gin Core Team. All rights reserved.2+// Use of this source code is governed by a MIT style3+// license that can be found in the LICENSE file.4+5+package binding6+7+import (8+ "testing"9+10+ "github.com/stretchr/testify/assert"11+ "github.com/stretchr/testify/require"12+)13+14+func TestJSONBindingBindBody(t *testing.T) {15+ var s struct {16+ Foo string `json:"foo"`17+ }18+ err := jsonBinding{}.BindBody([]byte(`{"foo": "FOO"}`), &s)19+ require.NoError(t, err)20+ assert.Equal(t, "FOO", s.Foo)21+}added:binding/msgpack_test.go+32 added
1+// Copyright 2019 Gin Core Team. All rights reserved.2+// Use of this source code is governed by a MIT style3+// license that can be found in the LICENSE file.4+5+package binding6+7+import (8+ "bytes"9+ "testing"10+11+ "github.com/stretchr/testify/assert"12+ "github.com/stretchr/testify/require"13+ "github.com/ugorji/go/codec"14+)15+16+func TestMsgpackBindingBindBody(t *testing.T) {17+ type teststruct struct {18+ Foo string `msgpack:"foo"`19+ }20+ var s teststruct21+ err := msgpackBinding{}.BindBody(msgpackBody(t, teststruct{"FOO"}), &s)22+ require.NoError(t, err)23+ assert.Equal(t, "FOO", s.Foo)24+}25+26+func msgpackBody(t *testing.T, obj interface{}) []byte {27+ var bs bytes.Buffer28+ h := &codec.MsgpackHandle{}29+ err := codec.NewEncoder(&bs, h).Encode(obj)30+ require.NoError(t, err)31+ return bs.Bytes()32+}added:binding/xml_test.go+25 added
1+// Copyright 2019 Gin Core Team. All rights reserved.2+// Use of this source code is governed by a MIT style3+// license that can be found in the LICENSE file.4+5+package binding6+7+import (8+ "testing"9+10+ "github.com/stretchr/testify/assert"11+ "github.com/stretchr/testify/require"12+)13+14+func TestXMLBindingBindBody(t *testing.T) {15+ var s struct {16+ Foo string `xml:"foo"`17+ }18+ xmlBody := `<?xml version="1.0" encoding="UTF-8"?>19+<root>20+ <foo>FOO</foo>21+</root>`22+ err := xmlBinding{}.BindBody([]byte(xmlBody), &s)23+ require.NoError(t, err)24+ assert.Equal(t, "FOO", s.Foo)25+}added:binding/yaml_test.go+21 added
1+// Copyright 2019 Gin Core Team. All rights reserved.2+// Use of this source code is governed by a MIT style3+// license that can be found in the LICENSE file.4+5+package binding6+7+import (8+ "testing"9+10+ "github.com/stretchr/testify/assert"11+ "github.com/stretchr/testify/require"12+)13+14+func TestYAMLBindingBindBody(t *testing.T) {15+ var s struct {16+ Foo string `yaml:"foo"`17+ }18+ err := yamlBinding{}.BindBody([]byte("foo: FOO"), &s)19+ require.NoError(t, err)20+ assert.Equal(t, "FOO", s.Foo)21+}modified:vendor/vendor.json+6 added
111 unmodified linesExpand all11211311411511611711811912012112212312412528 unmodified lines111 unmodified linesExpand all "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", "revision": "82dbfaf494e3b01d2d481376f11f6a5c8cf9599f", "revisionTime": "2019-07-02T14:15:27Z",28 unmodified lines