| 1 | package utils |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | msgpack "github.com/shamaton/msgpack/v3" |
| 7 | "github.com/stretchr/testify/require" |
| 8 | ) |
| 9 | |
| 10 | type sampleMsgPackStructure struct { |
| 11 | ImportantString string `msgpack:"important_string"` |
| 12 | } |
| 13 | |
| 14 | func Test_MsgpackEncoder(t *testing.T) { |
| 15 | t.Parallel() |
| 16 | |
| 17 | var ( |
| 18 | ss = &sampleStructure{ |
| 19 | ImportantString: "Hello World", |
| 20 | } |
| 21 | msgpackEncoder = msgpack.Marshal |
| 22 | ) |
| 23 | |
| 24 | raw, err := msgpackEncoder(ss) |
| 25 | require.NoError(t, err) |
| 26 | |
| 27 | var decoded sampleStructure |
| 28 | err = msgpack.Unmarshal(raw, &decoded) |
| 29 | require.NoError(t, err) |
| 30 | require.Equal(t, ss.ImportantString, decoded.ImportantString) |
| 31 | } |
| 32 | |
| 33 | func Test_MsgpackDecoder(t *testing.T) { |
| 34 | t.Parallel() |
| 35 | |
| 36 | var ( |
| 37 | ss = &sampleMsgPackStructure{ |
| 38 | ImportantString: "Hello World", |
| 39 | } |
| 40 | msgpackEncoder = msgpack.Marshal |
| 41 | msgpackDecoder = msgpack.Unmarshal |
| 42 | ) |
| 43 | |
| 44 | raw, err := msgpackEncoder(ss) |
| 45 | require.NoError(t, err) |
| 46 | |
| 47 | var decoded sampleMsgPackStructure |
| 48 | err = msgpackDecoder(raw, &decoded) |
| 49 | require.NoError(t, err) |
| 50 | require.Equal(t, "Hello World", decoded.ImportantString) |
| 51 | } |
| 52 | |
| 53 | func Test_MsgpackDecodeInvalid(t *testing.T) { |
| 54 | t.Parallel() |
| 55 | |
| 56 | var decoded sampleMsgPackStructure |
| 57 | err := msgpack.Unmarshal([]byte{0xff, 0xff}, &decoded) |
| 58 | require.Error(t, err) |
| 59 | } |