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.

202 lines
4.1 KiB

  1. package rpc
  2. import (
  3. "net/http"
  4. "testing"
  5. "time"
  6. "github.com/tendermint/go-rpc/client"
  7. "github.com/tendermint/go-rpc/server"
  8. "github.com/tendermint/go-rpc/types"
  9. "github.com/tendermint/go-wire"
  10. )
  11. // Client and Server should work over tcp or unix sockets
  12. var (
  13. tcpAddr = "tcp://0.0.0.0:46657"
  14. unixAddr = "unix:///tmp/go-rpc.sock" // NOTE: must remove file for test to run again
  15. websocketEndpoint = "/websocket/endpoint"
  16. )
  17. // Define a type for results and register concrete versions
  18. type Result interface{}
  19. type ResultStatus struct {
  20. Value string
  21. }
  22. var _ = wire.RegisterInterface(
  23. struct{ Result }{},
  24. wire.ConcreteType{&ResultStatus{}, 0x1},
  25. )
  26. // Define some routes
  27. var Routes = map[string]*rpcserver.RPCFunc{
  28. "status": rpcserver.NewRPCFunc(StatusResult, "arg"),
  29. }
  30. // an rpc function
  31. func StatusResult(v string) (Result, error) {
  32. return &ResultStatus{v}, nil
  33. }
  34. // launch unix and tcp servers
  35. func init() {
  36. mux := http.NewServeMux()
  37. rpcserver.RegisterRPCFuncs(mux, Routes)
  38. wm := rpcserver.NewWebsocketManager(Routes, nil)
  39. mux.HandleFunc(websocketEndpoint, wm.WebsocketHandler)
  40. go func() {
  41. _, err := rpcserver.StartHTTPServer(tcpAddr, mux)
  42. if err != nil {
  43. panic(err)
  44. }
  45. }()
  46. mux2 := http.NewServeMux()
  47. rpcserver.RegisterRPCFuncs(mux2, Routes)
  48. wm = rpcserver.NewWebsocketManager(Routes, nil)
  49. mux2.HandleFunc(websocketEndpoint, wm.WebsocketHandler)
  50. go func() {
  51. _, err := rpcserver.StartHTTPServer(unixAddr, mux2)
  52. if err != nil {
  53. panic(err)
  54. }
  55. }()
  56. // wait for servers to start
  57. time.Sleep(time.Second * 2)
  58. }
  59. func testURI(t *testing.T, cl *rpcclient.ClientURI) {
  60. val := "acbd"
  61. params := map[string]interface{}{
  62. "arg": val,
  63. }
  64. var result Result
  65. _, err := cl.Call("status", params, &result)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. got := result.(*ResultStatus).Value
  70. if got != val {
  71. t.Fatalf("Got: %v .... Expected: %v \n", got, val)
  72. }
  73. }
  74. func testJSONRPC(t *testing.T, cl *rpcclient.ClientJSONRPC) {
  75. val := "acbd"
  76. params := []interface{}{val}
  77. var result Result
  78. _, err := cl.Call("status", params, &result)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. got := result.(*ResultStatus).Value
  83. if got != val {
  84. t.Fatalf("Got: %v .... Expected: %v \n", got, val)
  85. }
  86. }
  87. func testWS(t *testing.T, cl *rpcclient.WSClient) {
  88. val := "acbd"
  89. params := []interface{}{val}
  90. err := cl.WriteJSON(rpctypes.RPCRequest{
  91. JSONRPC: "2.0",
  92. ID: "",
  93. Method: "status",
  94. Params: params,
  95. })
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. msg := <-cl.ResultsCh
  100. result := new(Result)
  101. wire.ReadJSONPtr(result, msg, &err)
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. got := (*result).(*ResultStatus).Value
  106. if got != val {
  107. t.Fatalf("Got: %v .... Expected: %v \n", got, val)
  108. }
  109. }
  110. //-------------
  111. func TestURI_TCP(t *testing.T) {
  112. cl := rpcclient.NewClientURI(tcpAddr)
  113. testURI(t, cl)
  114. }
  115. func TestURI_UNIX(t *testing.T) {
  116. cl := rpcclient.NewClientURI(unixAddr)
  117. testURI(t, cl)
  118. }
  119. func TestJSONRPC_TCP(t *testing.T) {
  120. cl := rpcclient.NewClientJSONRPC(tcpAddr)
  121. testJSONRPC(t, cl)
  122. }
  123. func TestJSONRPC_UNIX(t *testing.T) {
  124. cl := rpcclient.NewClientJSONRPC(unixAddr)
  125. testJSONRPC(t, cl)
  126. }
  127. func TestWS_TCP(t *testing.T) {
  128. cl := rpcclient.NewWSClient(tcpAddr, websocketEndpoint)
  129. _, err := cl.Start()
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. testWS(t, cl)
  134. }
  135. func TestWS_UNIX(t *testing.T) {
  136. cl := rpcclient.NewWSClient(unixAddr, websocketEndpoint)
  137. _, err := cl.Start()
  138. if err != nil {
  139. t.Fatal(err)
  140. }
  141. testWS(t, cl)
  142. }
  143. func TestHexStringArg(t *testing.T) {
  144. cl := rpcclient.NewClientURI(tcpAddr)
  145. // should NOT be handled as hex
  146. val := "0xabc"
  147. params := map[string]interface{}{
  148. "arg": val,
  149. }
  150. var result Result
  151. _, err := cl.Call("status", params, &result)
  152. if err != nil {
  153. t.Fatal(err)
  154. }
  155. got := result.(*ResultStatus).Value
  156. if got != val {
  157. t.Fatalf("Got: %v .... Expected: %v \n", got, val)
  158. }
  159. }
  160. func TestQuotedStringArg(t *testing.T) {
  161. cl := rpcclient.NewClientURI(tcpAddr)
  162. // should NOT be unquoted
  163. val := "\"abc\""
  164. params := map[string]interface{}{
  165. "arg": val,
  166. }
  167. var result Result
  168. _, err := cl.Call("status", params, &result)
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. got := result.(*ResultStatus).Value
  173. if got != val {
  174. t.Fatalf("Got: %v .... Expected: %v \n", got, val)
  175. }
  176. }