Skip to content
Myr

binding_msgpack_test.go

This file

Path

binding/binding_msgpack_test.go

59 lines · 1.4 KiB · Go

Last changed by

Not known.

History of this file

Take it

1// Copyright 2020 Gin Core Team. 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 binding89import (10	"bytes"11	"net/http"12	"testing"1314	"github.com/stretchr/testify/assert"15	"github.com/stretchr/testify/require"16	"github.com/ugorji/go/codec"17)1819func TestBindingMsgPack(t *testing.T) {20	test := FooStruct{21		Foo: "bar",22	}2324	h := new(codec.MsgpackHandle)25	assert.NotNil(t, h)26	buf := bytes.NewBuffer([]byte{})27	assert.NotNil(t, buf)28	err := codec.NewEncoder(buf, h).Encode(test)29	require.NoError(t, err)3031	data := buf.Bytes()3233	testMsgPackBodyBinding(t,34		MsgPack, "msgpack",35		"/", "/",36		string(data), string(data[1:]))37}3839func testMsgPackBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {40	assert.Equal(t, name, b.Name())4142	obj := FooStruct{}43	req := requestWithBody(http.MethodPost, path, body)44	req.Header.Add("Content-Type", MIMEMSGPACK)45	err := b.Bind(req, &obj)46	require.NoError(t, err)47	assert.Equal(t, "bar", obj.Foo)4849	obj = FooStruct{}50	req = requestWithBody(http.MethodPost, badPath, badBody)51	req.Header.Add("Content-Type", MIMEMSGPACK)52	err = MsgPack.Bind(req, &obj)53	require.Error(t, err)54}5556func TestBindingDefaultMsgPack(t *testing.T) {57	assert.Equal(t, MsgPack, Default(http.MethodPost, MIMEMSGPACK))58	assert.Equal(t, MsgPack, Default(http.MethodPut, MIMEMSGPACK2))59}