2022-05-30 10:16:10 +03:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
2020-01-07 06:31:10 +03:00
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-03-27 04:09:44 +03:00
|
|
|
//go:build !nomsgpack
|
2020-01-07 06:31:10 +03:00
|
|
|
|
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/ugorji/go/codec"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO unit tests
|
|
|
|
// test errors
|
|
|
|
|
|
|
|
func TestRenderMsgPack(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
2022-03-21 04:43:17 +03:00
|
|
|
data := map[string]any{
|
2020-01-07 06:31:10 +03:00
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
(MsgPack{data}).WriteContentType(w)
|
|
|
|
assert.Equal(t, "application/msgpack; charset=utf-8", w.Header().Get("Content-Type"))
|
|
|
|
|
|
|
|
err := (MsgPack{data}).Render(w)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
h := new(codec.MsgpackHandle)
|
|
|
|
assert.NotNil(t, h)
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
assert.NotNil(t, buf)
|
|
|
|
err = codec.NewEncoder(buf, h).Encode(data)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2021-06-25 07:14:06 +03:00
|
|
|
assert.Equal(t, w.Body.String(), buf.String())
|
2020-01-07 06:31:10 +03:00
|
|
|
assert.Equal(t, "application/msgpack; charset=utf-8", w.Header().Get("Content-Type"))
|
|
|
|
}
|