示例#1
0
func TestCanConnectAndDisconnect(t *testing.T) {
	var wait chan bool
	var err error
	ircServer := mockirc.New("mockirc.tenyks.io", 26661)
	wait, err = ircServer.Start()
	if err != nil {
		t.Fatal("Expected nil", "got", err)
	}
	<-wait

	conn := NewConnection("mockirc", MakeConnConfig())
	wait = conn.Connect()
	<-wait

	if conn.GetRetries() > 0 {
		t.Error("Expected", 0, "got", conn.GetRetries())
	}

	if !conn.IsConnected() {
		t.Error("Expected", true, "got", false)
	}

	conn.Disconnect()

	if conn.IsConnected() {
		t.Error("Expected", false, "got", true)
	}

	err = ircServer.Stop()
	if err != nil {
		t.Fatal("Error stopping mockirc server")
	}
}
示例#2
0
func ExampleIRCInteraction() {
	var client net.Conn
	var err error
	var wait chan bool

	ircServer := mockirc.New("mockirc.tenyks.io", 6661) // servername and port
	// When I recieve "PING mockirc.tenyks.io" on the server, respond back with PONG...
	ircServer.When("PING mockirc.tenyks.io").Respond(":PONG mockirc.tenyks.io")
	ircServer.When("NICK kyle").Respond("... response to NICK")
	wait, err = ircServer.Start()
	if err != nil {
		log.Fatal(err)
	}
	defer ircServer.Stop()
	<-wait // wait for start to fire up channel

	client, err = net.Dial("tcp", "localhost:6661")

	io := bufio.NewReadWriter(
		bufio.NewReader(client),
		bufio.NewWriter(client))

	_, err = io.WriteString("PING mockirc.tenyks.io\r\n")
	if err != nil {
		log.Fatal(err)
	}

	msg_string, err := io.ReadString('\n')

	if msg_string != ":PONG mockirc.tenyks.io" {
		log.Fatal("Invalid response")
	}
}
示例#3
0
func TestCanHandshakeAndWorkWithIRC(t *testing.T) {
	var wait chan bool
	var err error
	ircServer := mockirc.New("mockirc.tenyks.io", 26661)
	ircServer.When("USER tenyks localhost something :tenyks").Respond(":101 :Welcome")
	ircServer.When("PING ").Respond(":PONG")
	wait, err = ircServer.Start()
	if err != nil {
		t.Fatal("Expected nil", "got", err)
	}
	<-wait

	conn := NewConnection("mockirc", MakeConnConfig())
	wait = conn.Connect()
	<-wait

	conn.BootstrapHandler(nil)
	<-conn.ConnectWait

	msg := string(<-conn.In)
	if msg != ":101 :Welcome\r\n" {
		t.Error("Expected :101 :Welcome", "got", msg)
	}

	conn.SendPing(nil)
	select {
	case msg := <-conn.In:
		if msg != ":PONG\r\n" {
			t.Error("Expected", ":PONG mockirc", "got", msg)
		}
	case <-time.After(time.Second * 5):
		t.Error("Timed out before getting a response back from mockirc")
	}

	conn.Disconnect()

	err = ircServer.Stop()
	if err != nil {
		t.Fatal("Error stopping mockirc server")
	}
}