Skip to content
Myr

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

6 files+105 added−72 removed

deleted:binding/binding_body_test.go-72 removed
1package binding23import (4 "bytes"5 "io/ioutil"6 "testing"78 "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)1314func 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}5354func 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}6162func 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.45package binding67import (8 "testing"910 "github.com/stretchr/testify/assert"11 "github.com/stretchr/testify/require"12)1314func 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.45package binding67import (8 "bytes"9 "testing"1011 "github.com/stretchr/testify/assert"12 "github.com/stretchr/testify/require"13 "github.com/ugorji/go/codec"14)1516func 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}2526func 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.45package binding67import (8 "testing"910 "github.com/stretchr/testify/assert"11 "github.com/stretchr/testify/require"12)1314func 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.45package binding67import (8 "testing"910 "github.com/stretchr/testify/assert"11 "github.com/stretchr/testify/require"12)1314func 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