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.

206 lines
4.2 KiB

  1. package rpc
  2. import (
  3. "net/http"
  4. "testing"
  5. "time"
  6. rpcclient "github.com/tendermint/go-rpc/client"
  7. rpcserver "github.com/tendermint/go-rpc/server"
  8. rpctypes "github.com/tendermint/go-rpc/types"
  9. wire "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 := map[string]interface{}{
  77. "arg": val,
  78. }
  79. var result Result
  80. _, err := cl.Call("status", params, &result)
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. got := result.(*ResultStatus).Value
  85. if got != val {
  86. t.Fatalf("Got: %v .... Expected: %v \n", got, val)
  87. }
  88. }
  89. func testWS(t *testing.T, cl *rpcclient.WSClient) {
  90. val := "acbd"
  91. params := map[string]interface{}{
  92. "arg": val,
  93. }
  94. err := cl.WriteJSON(rpctypes.RPCRequest{
  95. JSONRPC: "2.0",
  96. ID: "",
  97. Method: "status",
  98. Params: params,
  99. })
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. msg := <-cl.ResultsCh
  104. result := new(Result)
  105. wire.ReadJSONPtr(result, msg, &err)
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. got := (*result).(*ResultStatus).Value
  110. if got != val {
  111. t.Fatalf("Got: %v .... Expected: %v \n", got, val)
  112. }
  113. }
  114. //-------------
  115. func TestURI_TCP(t *testing.T) {
  116. cl := rpcclient.NewClientURI(tcpAddr)
  117. testURI(t, cl)
  118. }
  119. func TestURI_UNIX(t *testing.T) {
  120. cl := rpcclient.NewClientURI(unixAddr)
  121. testURI(t, cl)
  122. }
  123. func TestJSONRPC_TCP(t *testing.T) {
  124. cl := rpcclient.NewClientJSONRPC(tcpAddr)
  125. testJSONRPC(t, cl)
  126. }
  127. func TestJSONRPC_UNIX(t *testing.T) {
  128. cl := rpcclient.NewClientJSONRPC(unixAddr)
  129. testJSONRPC(t, cl)
  130. }
  131. func TestWS_TCP(t *testing.T) {
  132. cl := rpcclient.NewWSClient(tcpAddr, websocketEndpoint)
  133. _, err := cl.Start()
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. testWS(t, cl)
  138. }
  139. func TestWS_UNIX(t *testing.T) {
  140. cl := rpcclient.NewWSClient(unixAddr, websocketEndpoint)
  141. _, err := cl.Start()
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. testWS(t, cl)
  146. }
  147. func TestHexStringArg(t *testing.T) {
  148. cl := rpcclient.NewClientURI(tcpAddr)
  149. // should NOT be handled as hex
  150. val := "0xabc"
  151. params := map[string]interface{}{
  152. "arg": val,
  153. }
  154. var result Result
  155. _, err := cl.Call("status", params, &result)
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. got := result.(*ResultStatus).Value
  160. if got != val {
  161. t.Fatalf("Got: %v .... Expected: %v \n", got, val)
  162. }
  163. }
  164. func TestQuotedStringArg(t *testing.T) {
  165. cl := rpcclient.NewClientURI(tcpAddr)
  166. // should NOT be unquoted
  167. val := "\"abc\""
  168. params := map[string]interface{}{
  169. "arg": val,
  170. }
  171. var result Result
  172. _, err := cl.Call("status", params, &result)
  173. if err != nil {
  174. t.Fatal(err)
  175. }
  176. got := result.(*ResultStatus).Value
  177. if got != val {
  178. t.Fatalf("Got: %v .... Expected: %v \n", got, val)
  179. }
  180. }