Skip to content
Myr

binding_msgpack_test.go

This file

Path

binding/binding_msgpack_test.go

57 lines · 1.3 KiB · Go

Last changed by

chore: support min go version 1.18 (#3511)thinkeroubefore adoption1e1f0b1

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	"testing"1213	"github.com/stretchr/testify/assert"14	"github.com/ugorji/go/codec"15)1617func TestBindingMsgPack(t *testing.T) {18	test := FooStruct{19		Foo: "bar",20	}2122	h := new(codec.MsgpackHandle)23	assert.NotNil(t, h)24	buf := bytes.NewBuffer([]byte{})25	assert.NotNil(t, buf)26	err := codec.NewEncoder(buf, h).Encode(test)27	assert.NoError(t, err)2829	data := buf.Bytes()3031	testMsgPackBodyBinding(t,32		MsgPack, "msgpack",33		"/", "/",34		string(data), string(data[1:]))35}3637func testMsgPackBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {38	assert.Equal(t, name, b.Name())3940	obj := FooStruct{}41	req := requestWithBody("POST", path, body)42	req.Header.Add("Content-Type", MIMEMSGPACK)43	err := b.Bind(req, &obj)44	assert.NoError(t, err)45	assert.Equal(t, "bar", obj.Foo)4647	obj = FooStruct{}48	req = requestWithBody("POST", badPath, badBody)49	req.Header.Add("Content-Type", MIMEMSGPACK)50	err = MsgPack.Bind(req, &obj)51	assert.Error(t, err)52}5354func TestBindingDefaultMsgPack(t *testing.T) {55	assert.Equal(t, MsgPack, Default("POST", MIMEMSGPACK))56	assert.Equal(t, MsgPack, Default("PUT", MIMEMSGPACK2))57}