-
Notifications
You must be signed in to change notification settings - Fork 0
/
unsuback_test.go
44 lines (35 loc) · 1.3 KB
/
unsuback_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
package mqtt
import (
"bytes"
"testing"
"github.com/srishina/mqtt.go/internal/packettype"
"github.com/stretchr/testify/assert"
)
func TestCodecUnsubAckPacket(t *testing.T) {
encoded := []byte{
0xB0, 0x06,
0x00, 0x10, // Packet identifier 16
0x00, // no properties
byte(UnsubAckNoSubscriptionExisted),
byte(UnsubAckReasonCodeNotAuthorized), byte(UnsubAckReasonCodeSuccess),
}
reader := bytes.NewBuffer(encoded)
byte0, remainingLength, err := readFixedHeader(reader)
assert.NoError(t, err, "Decoding UNSUBACK fixed header returned error")
assert.Equal(t, packettype.UNSUBACK, packettype.PacketType(byte0>>4))
assert.Equal(t, uint32(0x06), remainingLength)
us := UnsubAck{}
err = us.decode(reader, remainingLength)
assert.NoError(t, err, "UnsubAck.decode returned an error")
assert.Equal(t, uint16(16), us.packetID)
assert.Equal(t, 3, len(us.ReasonCodes))
assert.Equal(t, UnsubAckNoSubscriptionExisted, us.ReasonCodes[0])
assert.Equal(t, UnsubAckReasonCodeNotAuthorized, us.ReasonCodes[1])
assert.Equal(t, UnsubAckReasonCodeSuccess, us.ReasonCodes[2])
var buf bytes.Buffer
err = us.encode(&buf)
assert.NoError(t, err, "UnsubAck.encode returned an error")
if !bytes.Equal(encoded, buf.Bytes()) {
t.Errorf("UnsubAck.encode did not return expected bytes %v but %v ", encoded, buf.Bytes())
}
}