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

     1  // Copyright 2014 Manu Martinez-Almeida.  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 render
     9  
    10  import (
    11  	"bytes"
    12  	"github.com/hellobchain/newcryptosm/http/httptest"
    13  	"testing"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/ugorji/go/codec"
    17  )
    18  
    19  // TODO unit tests
    20  // test errors
    21  
    22  func TestRenderMsgPack(t *testing.T) {
    23  	w := httptest.NewRecorder()
    24  	data := map[string]interface{}{
    25  		"foo": "bar",
    26  	}
    27  
    28  	(MsgPack{data}).WriteContentType(w)
    29  	assert.Equal(t, "application/msgpack; charset=utf-8", w.Header().Get("Content-Type"))
    30  
    31  	err := (MsgPack{data}).Render(w)
    32  
    33  	assert.NoError(t, err)
    34  
    35  	h := new(codec.MsgpackHandle)
    36  	assert.NotNil(t, h)
    37  	buf := bytes.NewBuffer([]byte{})
    38  	assert.NotNil(t, buf)
    39  	err = codec.NewEncoder(buf, h).Encode(data)
    40  
    41  	assert.NoError(t, err)
    42  	assert.Equal(t, w.Body.String(), string(buf.Bytes()))
    43  	assert.Equal(t, "application/msgpack; charset=utf-8", w.Header().Get("Content-Type"))
    44  }