...

Source file src/github.com/gin-gonic/gin/render/render_msgpack_test.go

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

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

View as plain text