This repository has been archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteapack_test.go
105 lines (93 loc) · 1.96 KB
/
teapack_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package teapack
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestPacketRequest(t *testing.T) {
var data map[string]string
var ctx map[string]string
a := assert.New(t)
req := &PacketRequest{
Method: 12,
ID: 12345,
Context: map[string]string{
"foo": "bar",
},
Data: map[string]string{
"hello": "world",
},
}
b, err := Marshal(req)
a.NoError(err)
p, err := Load(b)
a.NoError(err)
err = Unmarshal(p, &data)
a.NoError(err)
err = UnmarshalContext(p, &ctx)
a.NoError(err)
a.Equal("world", data["hello"])
a.Equal("bar", ctx["foo"])
a.Equal(uint16(12345), ID(req))
a.Equal(uint8(12), Method(p))
}
func TestPacketEvent(t *testing.T) {
var data map[string]string
var ctx map[string]string
a := assert.New(t)
req := &PacketEvent{
Method: 12,
Context: map[string]string{
"foo": "bar",
},
Data: map[string]string{
"hello": "world",
},
}
b, err := Marshal(req)
a.NoError(err)
p, err := Load(b)
a.NoError(err)
err = Unmarshal(p, &data)
a.NoError(err)
err = UnmarshalContext(p, &ctx)
a.NoError(err)
a.Equal("world", data["hello"])
a.Equal("bar", ctx["foo"])
a.Equal(uint8(12), Method(p))
}
func TestPacketResponse(t *testing.T) {
var data map[string]string
var ctx map[string]string
a := assert.New(t)
req := &PacketResponse{
StatusCode: StatusCodeOK,
ID: 12345,
Context: map[string]string{
"foo": "bar",
},
Data: map[string]string{
"hello": "world",
},
}
b, err := Marshal(req)
a.NoError(err)
p, err := Load(b)
a.NoError(err)
err = Unmarshal(p, &data)
a.NoError(err)
err = UnmarshalContext(p, &ctx)
a.NoError(err)
a.Equal("world", data["hello"])
a.Equal("bar", ctx["foo"])
a.Equal(StatusCodeOK, Status(req))
a.Equal(uint16(12345), ID(p))
}
func TestPacketError(t *testing.T) {
a := assert.New(t)
_, err := Load([]byte{})
a.Equal(ErrUnknownType, err)
_, err = Load([]byte{1})
a.Equal(ErrTooShort, err)
_, err = Load([]byte{1, 2, 3, 4, 5, 6})
a.Equal(ErrIncorrectRange, err)
}