示例#1
0
func CreateStaticBackend(connectionsArgument string) (*StaticBackend, error) {
	connections, err := backends.ParseConnectionsParameter(connectionsArgument)

	if err != nil {
		return nil, err
	} else {
		return &StaticBackend{connectionsConfig: connections}, nil
	}
}
示例#2
0
func TestParseConnectionsParameter(t *testing.T) {
	fmt.Println("Testing TestParseConnectionsParameter")
	connectionsConfig, err := backends.ParseConnectionsParameter("1234:localhost:4567")
	if err != nil {
		t.Error(err)
	}

	name := connectionsConfig[0].Name
	localAddr := connectionsConfig[0].LocalAddress
	remoteAddr := connectionsConfig[0].RemoteAddress

	assert.Equal(t, localAddr, ":1234", "LocalAddress is not the expected one")
	assert.Equal(t, name, "localhost", "Name is not the expected one")
	assert.Equal(t, remoteAddr, "localhost:4567", "RemoteAddress is not the expected one")
}
示例#3
0
func TestRunProxy(t *testing.T) {
	fmt.Println("Testing TestRunProxy")

	quit := make(chan bool)
	create := make(chan []Connection)
	kill := make(chan []Connection)

	go echoServer(t, quit)

	connectionsConfig, err := backends.ParseConnectionsParameter("11112:localhost:11113")
	if err != nil {
		t.Error(err)
	}

	connections := make([]Connection, len(connectionsConfig))
	for i := range connectionsConfig {
		connections[i] = CreateConnection(connectionsConfig[i])
	}

	go RunTcpProxy(1, create, kill, func() {
		create <- connections

		conn, err := net.Dial("tcp", "localhost:11112")
		if err != nil {
			t.Fatal(err)
		}
		defer conn.Close()

		var cmd []byte
		fmt.Fscan(conn, &cmd)
		t.Log("Message:", string(cmd))

		quit <- true
	})

}