diff --git a/ws/message_test.go b/ws/message_test.go index 5aba272..2c4f7bd 100644 --- a/ws/message_test.go +++ b/ws/message_test.go @@ -40,14 +40,14 @@ func TestSimpleMessageReading(t *testing.T) { }, } - for _, tc := range cases{ + for i, 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) + t.Errorf("[%d] Expected %v error, got %v", i, tc.Err, err) } // do not check message if error expected @@ -57,22 +57,68 @@ func TestSimpleMessageReading(t *testing.T) { // check FIN if got.Final != tc.Expected.Final { - t.Errorf("Expected FIN=%t, got %t", tc.Expected.Final, got.Final) + t.Errorf("[%d] Expected FIN=%t, got %t", i, 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) + t.Errorf("[%d] Expected TYPE=%x, got %x", i, 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) + t.Errorf("[%d] Expected SIZE=%d, got %d", i, 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) + t.Errorf("[%d] Expected Data='%s', got '%d'", i, tc.Expected.Data, got.Data) + } + + } + +} + + + +func TestSimpleMessageSending(t *testing.T) { + + cases := []struct{ + Base Message + Expected []byte + }{ + { // FIN ; TEXT ; hello + Message{ true, TEXT, 5, []byte("hello") }, + []byte{0x81,0x05,0x68,0x65,0x6c,0x6c,0x6f}, + }, + { // FIN ; BINARY ; hello + Message{ true, BINARY, 5, []byte("hello") }, + []byte{0x82,0x05,0x68,0x65,0x6c,0x6c,0x6f}, + }, + { // FIN ; BINARY ; test unmasking + Message{ true, BINARY, 8, []byte{0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80} }, + []byte{0x82,0x08,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80}, + }, + { // FIN=0 ; TEXT ; + Message{ false, TEXT, 2, []byte{0x01,0x02} }, + []byte{0x01,0x02,0x01,0x02}, + }, + } + + for i, tc := range cases{ + + writer := new(bytes.Buffer) + + err := tc.Base.Send(writer) + + if err != nil { + t.Errorf("[%d] expected no error, got %v", i, err) + continue + } + + // check buffer + if writer.String() != string(tc.Expected) { + t.Errorf("[%d] expected '%x', got '%x'", i, tc.Expected, writer.String()) } }