Skip to content
Myr

test(render): add comprehensive tests for MsgPack render (#4537)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* test(render): add comprehensive tests for MsgPack render * test(render): make msgpack tests deterministic Decode the rendered msgpack output and assert values instead of comparing raw bytes (which can vary with map iteration order). Enable MsgpackHandle.RawToString so msgpack strings decode as Go strings. ---------

authored by Amirhf · committed by GitHub · parent f5c267d · Browse files · 1 file+46 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: AmirHossein Fallah <amirhossein.fallah@arvancloud.ir>

Diff stats

1 file+46 added−10 removed

  • other:+46 added−10 removed

1 file+46 added−10 removed

modified:render/render_msgpack_test.go+46 added-10 removed
5 unmodified lines
67package render89import (10 "bytes"10 "errors"11 "net/http/httptest"12 "testing"1314 "github.com/stretchr/testify/assert"15 "github.com/stretchr/testify/require"16 "github.com/ugorji/go/codec"17)1819// TODO unit tests20// test errors2119func TestRenderMsgPack(t *testing.T) {20 w := httptest.NewRecorder()21 data := map[string]any{22 "foo": "bar",
5 unmodified lines
28 err := (MsgPack{data}).Render(w)2930 require.NoError(t, err)3135 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)32 var decoded map[string]any33 var mh codec.MsgpackHandle34 mh.RawToString = true35 err = codec.NewDecoderBytes(w.Body.Bytes(), &mh).Decode(&decoded)36 require.NoError(t, err)37 assert.Equal(t, data, decoded)38 assert.Equal(t, "application/msgpack; charset=utf-8", w.Header().Get("Content-Type"))39}4041func TestWriteMsgPack(t *testing.T) {42 w := httptest.NewRecorder()43 data := map[string]any{44 "foo": "bar",45 "num": 42,46 }4748 err := WriteMsgPack(w, data)49 require.NoError(t, err)42 assert.Equal(t, w.Body.String(), buf.String())5051 assert.Equal(t, "application/msgpack; charset=utf-8", w.Header().Get("Content-Type"))5253 var decoded map[string]any54 var mh codec.MsgpackHandle55 mh.RawToString = true56 err = codec.NewDecoderBytes(w.Body.Bytes(), &mh).Decode(&decoded)57 require.NoError(t, err)58 assert.Len(t, decoded, 2)59 assert.Equal(t, "bar", decoded["foo"])60 assert.EqualValues(t, 42, decoded["num"])61}6263type failWriter struct {64 *httptest.ResponseRecorder65}6667func (w *failWriter) Write(data []byte) (int, error) {68 return 0, errors.New("write error")69}7071func TestRenderMsgPackError(t *testing.T) {72 w := httptest.NewRecorder()73 data := map[string]any{74 "foo": "bar",75 }7677 err := (MsgPack{data}).Render(&failWriter{w})78 require.Error(t, err)79 assert.Contains(t, err.Error(), "write error")80}