Mirror of github.com/gissleh/irc
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.1 KiB

  1. package irctest_test
  2. import (
  3. "net"
  4. "testing"
  5. "github.com/gissleh/irc/internal/irctest"
  6. )
  7. func TestInteraction(t *testing.T) {
  8. interaction := irctest.Interaction{
  9. Lines: []irctest.InteractionLine{
  10. {Client: "FIRST MESSAGE"},
  11. {Server: "SERVER MESSAGE"},
  12. {Client: "SECOND MESSAGE"},
  13. },
  14. }
  15. addr, err := interaction.Listen()
  16. if err != nil {
  17. t.Fatal("Listen:", err)
  18. }
  19. conn, err := net.Dial("tcp", addr)
  20. if err != nil {
  21. t.Fatal("Dial:", err)
  22. }
  23. _, err = conn.Write([]byte("FIRST MESSAGE\r\n"))
  24. if err != nil {
  25. t.Fatal("Write:", err)
  26. }
  27. buffer := make([]byte, 64)
  28. n, err := conn.Read(buffer)
  29. if err != nil {
  30. t.Fatal("Read:", err)
  31. }
  32. if string(buffer[:n]) != "SERVER MESSAGE\r\n" {
  33. t.Fatal("Read not correct:", string(buffer[:n]))
  34. }
  35. _, err = conn.Write([]byte("SECOND MESSAGE\r\n"))
  36. if err != nil {
  37. t.Fatal("Write 2:", err)
  38. }
  39. interaction.Wait()
  40. if interaction.Failure != nil {
  41. t.Error("Index:", interaction.Failure.Index)
  42. t.Error("Result:", interaction.Failure.Result)
  43. t.Error("NetErr:", interaction.Failure.NetErr)
  44. t.FailNow()
  45. }
  46. }