github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/gin/binding/msgpack_test.go (about)

     1  // Copyright 2019 Gin Core Team. All rights reserved.
     2  // Use of this source code is governed by a MIT style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !nomsgpack
     6  // +build !nomsgpack
     7  
     8  package binding
     9  
    10  import (
    11  	"bytes"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  	"github.com/ugorji/go/codec"
    17  )
    18  
    19  func TestMsgpackBindingBindBody(t *testing.T) {
    20  	type teststruct struct {
    21  		Foo string `msgpack:"foo"`
    22  	}
    23  	var s teststruct
    24  	err := msgpackBinding{}.BindBody(msgpackBody(t, teststruct{"FOO"}), &s)
    25  	require.NoError(t, err)
    26  	assert.Equal(t, "FOO", s.Foo)
    27  }
    28  
    29  func msgpackBody(t *testing.T, obj interface{}) []byte {
    30  	var bs bytes.Buffer
    31  	h := &codec.MsgpackHandle{}
    32  	err := codec.NewEncoder(&bs, h).Encode(obj)
    33  	require.NoError(t, err)
    34  	return bs.Bytes()
    35  }