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.

216 lines
4.3 KiB

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