Ejemplo n.º 1
0
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)
}
Ejemplo n.º 2
0
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)
}
Ejemplo n.º 3
0
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)
}
Ejemplo n.º 4
0
// 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
}
Ejemplo n.º 5
0
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)
}
Ejemplo n.º 6
0
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())
}
Ejemplo n.º 7
0
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)
}
Ejemplo n.º 8
0
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())
}
Ejemplo n.º 9
0
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)
}
Ejemplo n.º 10
0
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())
}
Ejemplo n.º 11
0
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)
}