...
1
2
3
4
5
6
7 package binding
8
9 import (
10 "bytes"
11 "testing"
12
13 "github.com/stretchr/testify/assert"
14 "github.com/ugorji/go/codec"
15 )
16
17 func TestBindingMsgPack(t *testing.T) {
18 test := FooStruct{
19 Foo: "bar",
20 }
21
22 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)
28
29 data := buf.Bytes()
30
31 testMsgPackBodyBinding(t,
32 MsgPack, "msgpack",
33 "/", "/",
34 string(data), string(data[1:]))
35 }
36
37 func testMsgPackBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) {
38 assert.Equal(t, name, b.Name())
39
40 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)
46
47 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 }
53
54 func TestBindingDefaultMsgPack(t *testing.T) {
55 assert.Equal(t, MsgPack, Default("POST", MIMEMSGPACK))
56 assert.Equal(t, MsgPack, Default("PUT", MIMEMSGPACK2))
57 }
58
View as plain text