-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodec_test.go
89 lines (79 loc) · 2.14 KB
/
codec_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
package eiop
import (
"bytes"
"fmt"
"reflect"
"testing"
)
func TestDecodePacket(t *testing.T) {
cases := []string{"", "a123"}
for _, tc := range cases {
t.Run(fmt.Sprintf("should fail decode \"%s\"", tc), func(t *testing.T) {
_, got := DecodePacket(tc)
if got == nil {
t.Fail()
}
})
}
wantType := Message
wantData := "test"
arg := wantType.Encode() + wantData
t.Run(fmt.Sprintf("%s=%s%s", arg, wantType.Encode(), wantData), func(t *testing.T) {
pkt, err := DecodePacket(arg)
if err != nil {
t.Errorf("Expected decode, but got: '%v'", err)
}
if pkt.Type != wantType || pkt.Data.(string) != wantData {
t.Errorf("Expected '%s%s', but got '%s%s'", wantType.String(), wantData, pkt.Type.String(), pkt.Data)
}
})
}
func TestEncodePacket(t *testing.T) {
type testCase struct {
arg Packet
want any
}
cases := []testCase{
{Packet{Type: Message, Data: "test"}, "4test"},
{Packet{Type: Message, Data: []byte("test")}, []byte("test")},
}
for _, tc := range cases {
t.Run(fmt.Sprintf("%v", tc.want), func(t *testing.T) {
got := EncodePacket(tc.arg, true)
if !reflect.DeepEqual(tc.want, got) {
t.Errorf("Expected '%v', but got '%v'", tc.want, got)
}
})
}
}
func TestEncodePayload(t *testing.T) {
payload := Payload{
{Type: Open},
{Type: Close},
{Type: Ping, Data: "probe"},
{Type: Pong, Data: "probe"},
{Type: Message, Data: "test"},
}
got := EncodePayload(payload)
want := []byte{48, 30, 49, 30, 50, 112, 114, 111, 98, 101, 30, 51, 112, 114, 111, 98, 101, 30, 52, 116, 101, 115, 116}
if bytes.Compare(want, []byte(got)) != 0 {
t.Errorf("Expected '%s', but got '%s'", want, got)
}
}
func TestDecodePayload(t *testing.T) {
data := string([]byte{48, 30, 49, 30, 50, 112, 114, 111, 98, 101, 30, 51, 112, 114, 111, 98, 101, 30, 52, 116, 101, 115, 116})
got, err := DecodePayload(data)
if err != nil {
t.Errorf("Expected decode, but got: '%v'", err)
}
want := Payload{
{Type: Open},
{Type: Close},
{Type: Ping, Data: "probe"},
{Type: Pong, Data: "probe"},
{Type: Message, Data: "test"},
}
if !reflect.DeepEqual(want, got) {
t.Errorf("Expected '%v', but got '%v'", want, got)
}
}