func TestGlobalDial(t *testing.T) { port := tools.NewPort() server, err := testLauncher.Launch(port.URL("tcp")) require.NoError(t, err) wait := make(chan struct{}) go func() { conn, err := server.Accept() require.NoError(t, err) pkt, err := conn.Receive() assert.Nil(t, pkt) assert.Equal(t, ConnectionClose, toError(err).Code()) close(wait) }() conn, err := Dial(port.URL("tcp")) require.NoError(t, err) err = conn.Close() assert.NoError(t, err) <-wait err = server.Close() assert.NoError(t, err) }
func abstractDefaultPortTest(t *testing.T, protocol string) { port := tools.NewPort() server, err := testLauncher.Launch(port.URL(protocol)) require.NoError(t, err) go func() { conn, err := server.Accept() require.NoError(t, err) pkt, err := conn.Receive() assert.Nil(t, pkt) assert.Equal(t, ConnectionClose, toError(err).Code()) }() dialer := NewDialer() dialer.TLSConfig = clientTLSConfig dialer.DefaultTCPPort = port.Port() dialer.DefaultTLSPort = port.Port() dialer.DefaultWSPort = port.Port() dialer.DefaultWSSPort = port.Port() conn, err := dialer.Dial(protocol + "://localhost") require.NoError(t, err) err = conn.Close() assert.NoError(t, err) err = server.Close() assert.NoError(t, err) }
func TestWebSocketFallback(t *testing.T) { port := tools.NewPort() server, err := testLauncher.Launch(port.URL("ws")) require.NoError(t, err) ws := server.(*WebSocketServer) mux := http.NewServeMux() mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello world!")) }) ws.SetFallback(mux) resp, err := http.Get(port.URL("http") + "/test") assert.NoError(t, err) assert.Equal(t, "200 OK", resp.Status) bytes, err := ioutil.ReadAll(resp.Body) assert.NoError(t, err) assert.Equal(t, []byte("Hello world!"), bytes) err = server.Close() assert.NoError(t, err) }
// returns a client-ish and server-ish pair of connections func connectionPair(protocol string, handler func(Conn)) (Conn, chan struct{}) { done := make(chan struct{}) port := tools.NewPort() server, err := testLauncher.Launch(port.URL(protocol)) if err != nil { panic(err) } go func() { conn, err := server.Accept() if err != nil { panic(err) } handler(conn) server.Close() close(done) }() conn, err := testDialer.Dial(port.URL(protocol)) if err != nil { panic(err) } return conn, done }
func TestGlobalLaunch(t *testing.T) { port := tools.NewPort() server, err := Launch(port.URL("tcp")) require.NoError(t, err) err = server.Close() assert.NoError(t, err) }
func abstractServerCloseAfterClose(t *testing.T, protocol string) { port := tools.NewPort() server, err := testLauncher.Launch(port.URL(protocol)) require.NoError(t, err) err = server.Close() assert.NoError(t, err) err = server.Close() assert.Equal(t, NetworkError, toError(err).Code()) }
func TestWebSocketServerInvalidUpgrade(t *testing.T) { port := tools.NewPort() server, err := testLauncher.Launch(port.URL("ws")) require.NoError(t, err) resp, err := http.PostForm(port.URL("http"), url.Values{"foo": {"bar"}}) assert.Equal(t, "405 Method Not Allowed", resp.Status) assert.NoError(t, err) err = server.Close() assert.NoError(t, err) }
func TestWebSocketServerAcceptAfterError(t *testing.T) { port := tools.NewPort() server, err := testLauncher.Launch(port.URL("ws")) require.NoError(t, err) webSocketServer := server.(*WebSocketServer) err = webSocketServer.listener.Close() assert.NoError(t, err) conn, err := server.Accept() require.Nil(t, conn) assert.Equal(t, NetworkError, toError(err).Code()) }
func TestWebSocketOriginChecker(t *testing.T) { port := tools.NewPort() server, err := testLauncher.Launch(port.URL("ws")) require.NoError(t, err) ws := server.(*WebSocketServer) ws.SetOriginChecker(func(r *http.Request) bool { return false }) conn, err := testDialer.Dial(port.URL("ws")) require.Error(t, err) require.Nil(t, conn) }
func TestWebSocketServerConnectionCancelOnClose(t *testing.T) { port := tools.NewPort() server, err := testLauncher.Launch(port.URL("ws")) require.NoError(t, err) conn, err := testDialer.Dial(port.URL("ws")) require.NoError(t, err) err = server.Close() assert.NoError(t, err) pkt, err := conn.Receive() assert.Nil(t, pkt) assert.Equal(t, ConnectionClose, toError(err).Code()) }
func abstractServerTest(t *testing.T, protocol string) { port := tools.NewPort() server, err := testLauncher.Launch(port.URL(protocol)) require.NoError(t, err) go func() { conn1, err := server.Accept() require.NoError(t, err) pkt, err := conn1.Receive() assert.Equal(t, pkt.Type(), packet.CONNECT) assert.NoError(t, err) err = conn1.Send(packet.NewConnackPacket()) assert.NoError(t, err) pkt, err = conn1.Receive() assert.Nil(t, pkt) assert.Equal(t, ConnectionClose, toError(err).Code()) }() conn2, err := testDialer.Dial(port.URL(protocol)) require.NoError(t, err) err = conn2.Send(packet.NewConnectPacket()) assert.NoError(t, err) pkt, err := conn2.Receive() assert.Equal(t, pkt.Type(), packet.CONNACK) assert.NoError(t, err) err = conn2.Close() assert.NoError(t, err) err = server.Close() assert.NoError(t, err) }