msgpack_test.go 770 B

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