...

Source file src/github.com/gin-gonic/gin/binding/msgpack_test.go

Documentation: github.com/gin-gonic/gin/binding

     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  
     7  package binding
     8  
     9  import (
    10  	"bytes"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  	"github.com/ugorji/go/codec"
    16  )
    17  
    18  func TestMsgpackBindingBindBody(t *testing.T) {
    19  	type teststruct struct {
    20  		Foo string `msgpack:"foo"`
    21  	}
    22  	var s teststruct
    23  	err := msgpackBinding{}.BindBody(msgpackBody(t, teststruct{"FOO"}), &s)
    24  	require.NoError(t, err)
    25  	assert.Equal(t, "FOO", s.Foo)
    26  }
    27  
    28  func msgpackBody(t *testing.T, obj any) []byte {
    29  	var bs bytes.Buffer
    30  	h := &codec.MsgpackHandle{}
    31  	err := codec.NewEncoder(&bs, h).Encode(obj)
    32  	require.NoError(t, err)
    33  	return bs.Bytes()
    34  }
    35  

View as plain text