wsrpc_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package wsrpc_test
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/http/httptest"
  6. "net/url"
  7. "testing"
  8. "time"
  9. "github.com/gohxs/prettylog"
  10. "golang.org/x/net/websocket"
  11. "dev.hexasoftware.com/stdio/wsrpc"
  12. )
  13. func init() {
  14. prettylog.Global()
  15. }
  16. var (
  17. ready = make(chan struct{})
  18. )
  19. ///////////////////////////
  20. // TESTS
  21. //////////////
  22. func TestCall(t *testing.T) {
  23. ws := prepareClient(t) // Prepare a temp server and a client
  24. // Sending
  25. callObj := wsrpc.CallObj{
  26. OP: "call",
  27. ID: "123",
  28. Method: "hello",
  29. Params: []interface{}{1, 2},
  30. }
  31. websocket.JSON.Send(ws, callObj)
  32. //Receive
  33. res := wsrpc.CallObj{}
  34. err := websocket.JSON.Receive(ws, &res)
  35. if err != nil {
  36. t.Fatal("Read error", err)
  37. }
  38. if res.OP != "response" || res.Response != "Hello world" {
  39. t.Fatal("Failed, invalid response")
  40. }
  41. if res.Error != "" {
  42. t.Fatal("Remote error", res.Error)
  43. }
  44. }
  45. func TestParams(t *testing.T) {
  46. ws := prepareClient(t)
  47. cli := wsrpc.NewClient(ws)
  48. go cli.Process()
  49. res, err := cli.Call("sum", 1, 2)
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. if res != float64(3) {
  54. t.Fatal("Message error")
  55. }
  56. }
  57. func TestError(t *testing.T) {
  58. ws := prepareClient(t)
  59. cli := wsrpc.NewClient(ws)
  60. go cli.Process()
  61. _, err := cli.Call("nomethod", 1, 2, 3, 4)
  62. if err == nil {
  63. t.Fatal("It should return an error but didn't")
  64. }
  65. _, err = cli.Call("hello", 1, 2, 3)
  66. if err != nil {
  67. t.Fatal("Ups")
  68. }
  69. }
  70. //////////////////////
  71. // HELPERS
  72. ////////
  73. func cliTest(ws *wsrpc.ClientCtx) {
  74. ws.Define("hello", func(params ...interface{}) (interface{}, error) {
  75. return "Hello world", nil
  76. })
  77. ws.Define("done", func(params ...interface{}) (interface{}, error) {
  78. return nil, nil
  79. })
  80. ws.Define("sum", func(params ...interface{}) (interface{}, error) {
  81. return params[0].(float64) + params[1].(float64), nil
  82. })
  83. ready <- struct{}{}
  84. <-time.After(10 * time.Second)
  85. ws.WS.Close()
  86. }
  87. func prepareClient(t *testing.T) *websocket.Conn {
  88. mux := http.NewServeMux()
  89. mux.Handle("/", wsrpc.New(cliTest))
  90. serv := httptest.NewServer(mux)
  91. u, err := url.Parse(serv.URL)
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. wsURL := fmt.Sprintf("ws://%s:%s/wsrpc", u.Hostname(), u.Port())
  96. ws, err := websocket.Dial(wsURL, "", "http://localhost/")
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. <-ready // wait until define ready
  101. return ws
  102. }