render_msgpack_test.go
This file
Path
render/render_msgpack_test.go
80 lines · 1.9 KiB · Go
Last changed by
test(render): add comprehensive tests for MsgPack render (#4537)Amirhfbefore adoption216a4a7
History of this fileTake it
The walk stopped after 200 commits: lines older than cf32d2d point at it.
chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 1–9why, one step back1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45//go:build !nomsgpack67package render89import (test(render): add comprehensive tests for MsgPack render (#4537)Amirhfbefore adoption216a4a7
line 10why, one step back10 "errors"chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 11–31why, one step back11 "net/http/httptest"12 "testing"1314 "github.com/stretchr/testify/assert"15 "github.com/stretchr/testify/require"16 "github.com/ugorji/go/codec"17)1819func TestRenderMsgPack(t *testing.T) {20 w := httptest.NewRecorder()21 data := map[string]any{22 "foo": "bar",23 }2425 (MsgPack{data}).WriteContentType(w)26 assert.Equal(t, "application/msgpack; charset=utf-8", w.Header().Get("Content-Type"))2728 err := (MsgPack{data}).Render(w)2930 require.NoError(t, err)31test(render): add comprehensive tests for MsgPack render (#4537)Amirhfbefore adoption216a4a7
lines 32–39why, one step back32 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}chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 40why, one step back40test(render): add comprehensive tests for MsgPack render (#4537)Amirhfbefore adoption216a4a7
lines 41–48why, one step back41func 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)chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 49why, one step back49 require.NoError(t, err)test(render): add comprehensive tests for MsgPack render (#4537)Amirhfbefore adoption216a4a7
line 50why, one step back50chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 51why, one step back51 assert.Equal(t, "application/msgpack; charset=utf-8", w.Header().Get("Content-Type"))test(render): add comprehensive tests for MsgPack render (#4537)Amirhfbefore adoption216a4a7
lines 52–79why, one step back5253 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")chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 80why, one step back80}