Cloak/internal/server/websocketAux_test.go

59 lines
1.2 KiB
Go
Raw Normal View History

2020-03-16 11:37:09 +00:00
package server
import (
"bytes"
"github.com/cbeuw/connutil"
2020-03-16 11:37:09 +00:00
"testing"
)
func TestFirstBuffedConn_Read(t *testing.T) {
mockConn, writingEnd := connutil.AsyncPipe()
2020-03-16 11:37:09 +00:00
expectedFirstPacket := []byte{1, 2, 3}
firstBuffedConn := &firstBuffedConn{
Conn: mockConn,
firstRead: false,
firstPacket: expectedFirstPacket,
}
buf := make([]byte, 1024)
n, err := firstBuffedConn.Read(buf)
2020-03-16 11:37:09 +00:00
if err != nil {
t.Error(err)
return
}
if !bytes.Equal(expectedFirstPacket, buf[:n]) {
2020-03-16 11:37:09 +00:00
t.Error("first read doesn't produce given packet")
return
}
expectedSecondPacket := []byte{4, 5, 6, 7}
writingEnd.Write(expectedSecondPacket)
n, err = firstBuffedConn.Read(buf)
2020-03-16 11:37:09 +00:00
if err != nil {
t.Error(err)
return
}
if !bytes.Equal(expectedSecondPacket, buf[:n]) {
2020-03-16 11:37:09 +00:00
t.Error("second read doesn't produce subsequently written packet")
return
}
}
func TestWsAcceptor(t *testing.T) {
mockConn := connutil.Discard()
2020-03-16 11:37:09 +00:00
expectedFirstPacket := []byte{1, 2, 3}
wsAcceptor := newWsAcceptor(mockConn, expectedFirstPacket)
_, err := wsAcceptor.Accept()
2020-03-16 11:37:09 +00:00
if err != nil {
t.Error(err)
return
}
_, err = wsAcceptor.Accept()
if err == nil {
2020-03-16 11:37:09 +00:00
t.Error("accepting second time doesn't return error")
}
}