-
Notifications
You must be signed in to change notification settings - Fork 41
/
messagepackhubprotocol_test.go
82 lines (79 loc) · 2.92 KB
/
messagepackhubprotocol_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
package signalr
import (
"bytes"
"reflect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("MessagePackHubProtocol", func() {
protocol := messagePackHubProtocol{}
protocol.setDebugLogger(testLogger())
Context("ParseMessages", func() {
It("should encode/decode an InvocationMessage", func() {
message := invocationMessage{
Type: 4,
Target: "target",
InvocationID: "1",
// because DecodeSlice below decodes ints to the smallest type and arrays always to []interface{}, we need to be very specific
Arguments: []interface{}{"1", int8(1), []interface{}{int8(7), int8(3)}},
StreamIds: []string{"0"},
}
buf := bytes.Buffer{}
err := protocol.WriteMessage(message, &buf)
Expect(err).NotTo(HaveOccurred())
remainBuf := bytes.Buffer{}
got, err := protocol.ParseMessages(&buf, &remainBuf)
Expect(err).NotTo(HaveOccurred())
Expect(remainBuf.Len()).To(Equal(0))
Expect(len(got)).To(Equal(1))
Expect(got[0]).To(BeAssignableToTypeOf(invocationMessage{}))
gotMsg := got[0].(invocationMessage)
Expect(gotMsg.Type).To(Equal(message.Type))
Expect(gotMsg.Target).To(Equal(message.Target))
Expect(gotMsg.InvocationID).To(Equal(message.InvocationID))
Expect(gotMsg.StreamIds).To(Equal(message.StreamIds))
for i, gotArg := range gotMsg.Arguments {
// We can not directly compare gotArg and want.Arguments[i]
// because msgpack serializes numbers to the shortest possible type
t := reflect.TypeOf(message.Arguments[i])
value := reflect.New(t)
Expect(protocol.UnmarshalArgument(gotArg, value.Interface())).NotTo(HaveOccurred())
Expect(reflect.Indirect(value).Interface()).To(Equal(message.Arguments[i]))
}
})
It("should encode/decode an CancelInvocationMessage", func() {
message := cancelInvocationMessage{
Type: 5,
InvocationID: "1",
}
buf := bytes.Buffer{}
err := protocol.WriteMessage(message, &buf)
Expect(err).NotTo(HaveOccurred())
remainBuf := bytes.Buffer{}
got, err := protocol.ParseMessages(&buf, &remainBuf)
Expect(err).NotTo(HaveOccurred())
Expect(remainBuf.Len()).To(Equal(0))
Expect(len(got)).To(Equal(1))
Expect(got[0]).To(BeAssignableToTypeOf(cancelInvocationMessage{}))
gotMsg := got[0].(cancelInvocationMessage)
Expect(gotMsg.Type).To(Equal(message.Type))
Expect(gotMsg.InvocationID).To(Equal(message.InvocationID))
})
It("should encode/decode an CloseMessage", func() {
message := closeMessage{
Type: 7,
}
buf := bytes.Buffer{}
err := protocol.WriteMessage(message, &buf)
Expect(err).NotTo(HaveOccurred())
remainBuf := bytes.Buffer{}
got, err := protocol.ParseMessages(&buf, &remainBuf)
Expect(err).NotTo(HaveOccurred())
Expect(remainBuf.Len()).To(Equal(0))
Expect(len(got)).To(Equal(1))
Expect(got[0]).To(BeAssignableToTypeOf(closeMessage{}))
gotMsg := got[0].(closeMessage)
Expect(gotMsg.Type).To(Equal(message.Type))
})
})
})