ws/ws/message_test.go

80 lines
1.7 KiB
Go
Raw Normal View History

package ws
import (
"bytes"
"testing"
)
func TestSimpleMessageReading(t *testing.T) {
cases := []struct{
ReadBuffer []byte
Expected Message
Err error
}{
{ // FIN ; TEXT ; Unmasked -> error
[]byte{0x81,0x05,0x68,0x65,0x6c,0x6c,0x6f},
Message{},
UnmaskedFrameErr,
},
{ // FIN ; TEXT ; hello
[]byte{0x81,0x85,0x00,0x00,0x00,0x00,0x68,0x65,0x6c,0x6c,0x6f},
Message{ true, TEXT, 5, []byte("hello") },
nil,
},
{ // FIN ; BINARY ; hello
[]byte{0x82,0x85,0x00,0x00,0x00,0x00,0x68,0x65,0x6c,0x6c,0x6f},
Message{ true, BINARY, 5, []byte("hello") },
nil,
},
{ // FIN ; BINARY ; test unmasking
[]byte{0x82,0x88,0x01,0x02,0x03,0x04,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80},
Message{ true, BINARY, 8, []byte{0x11,0x22,0x33,0x44,0x51,0x62,0x73,0x84} },
nil,
},
{ // FIN=0 ; TEXT ;
[]byte{0x01,0x82,0x00,0x00,0x00,0x00,0x01,0x02},
Message{ false, TEXT, 2, []byte{0x01,0x02} },
nil,
},
}
for _, tc := range cases{
reader := bytes.NewBuffer(tc.ReadBuffer)
got, err := readMessage(reader)
if err != tc.Err {
t.Errorf("Expected %v error, got %v", tc.Err, err)
}
// do not check message if error expected
if tc.Err != nil {
continue
}
// check FIN
if got.Final != tc.Expected.Final {
t.Errorf("Expected FIN=%t, got %t", tc.Expected.Final, got.Final)
}
// check OpCode
if got.Type != tc.Expected.Type {
t.Errorf("Expected TYPE=%x, got %x", tc.Expected.Type, got.Type)
}
// check Size
if got.Size != tc.Expected.Size {
t.Errorf("Expected SIZE=%d, got %d", tc.Expected.Size, got.Size)
}
// check Data
if string(got.Data) != string(tc.Expected.Data) {
t.Errorf("Expected Data='%s', got '%d'", tc.Expected.Data, got.Data)
}
}
}