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.
This commit is contained in:
AmirHossein Fallah 2026-02-17 16:12:36 +03:30
parent fd02a3a487
commit 93e3206653

View File

@ -7,7 +7,6 @@
package render package render
import ( import (
"bytes"
"errors" "errors"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -30,14 +29,12 @@ func TestRenderMsgPack(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
h := new(codec.MsgpackHandle) var decoded map[string]any
assert.NotNil(t, h) var mh codec.MsgpackHandle
buf := bytes.NewBuffer([]byte{}) mh.RawToString = true
assert.NotNil(t, buf) err = codec.NewDecoderBytes(w.Body.Bytes(), &mh).Decode(&decoded)
err = codec.NewEncoder(buf, h).Encode(data)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, w.Body.String(), buf.String()) assert.Equal(t, data, decoded)
assert.Equal(t, "application/msgpack; charset=utf-8", w.Header().Get("Content-Type")) assert.Equal(t, "application/msgpack; charset=utf-8", w.Header().Get("Content-Type"))
} }
@ -53,13 +50,14 @@ func TestWriteMsgPack(t *testing.T) {
assert.Equal(t, "application/msgpack; charset=utf-8", w.Header().Get("Content-Type")) assert.Equal(t, "application/msgpack; charset=utf-8", w.Header().Get("Content-Type"))
// Verify the encoded data is correct var decoded map[string]any
h := new(codec.MsgpackHandle) var mh codec.MsgpackHandle
buf := bytes.NewBuffer([]byte{}) mh.RawToString = true
err = codec.NewEncoder(buf, h).Encode(data) err = codec.NewDecoderBytes(w.Body.Bytes(), &mh).Decode(&decoded)
require.NoError(t, err) require.NoError(t, err)
assert.Len(t, decoded, 2)
assert.Equal(t, buf.String(), w.Body.String()) assert.Equal(t, "bar", decoded["foo"])
assert.EqualValues(t, 42, decoded["num"])
} }
type failWriter struct { type failWriter struct {