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.

174 lines
4.2 KiB

  1. package server
  2. import (
  3. "crypto/tls"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net"
  9. "net/http"
  10. "net/http/httptest"
  11. "sync"
  12. "sync/atomic"
  13. "testing"
  14. "time"
  15. "github.com/stretchr/testify/assert"
  16. "github.com/stretchr/testify/require"
  17. "github.com/tendermint/tendermint/libs/log"
  18. types "github.com/tendermint/tendermint/rpc/jsonrpc/types"
  19. )
  20. type sampleResult struct {
  21. Value string `json:"value"`
  22. }
  23. func TestMaxOpenConnections(t *testing.T) {
  24. const max = 5 // max simultaneous connections
  25. // Start the server.
  26. var open int32
  27. mux := http.NewServeMux()
  28. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  29. if n := atomic.AddInt32(&open, 1); n > int32(max) {
  30. t.Errorf("%d open connections, want <= %d", n, max)
  31. }
  32. defer atomic.AddInt32(&open, -1)
  33. time.Sleep(10 * time.Millisecond)
  34. fmt.Fprint(w, "some body")
  35. })
  36. config := DefaultConfig()
  37. config.MaxOpenConnections = max
  38. l, err := Listen("tcp://127.0.0.1:0", config)
  39. require.NoError(t, err)
  40. defer l.Close()
  41. go Serve(l, mux, log.TestingLogger(), config) //nolint:errcheck // ignore for tests
  42. // Make N GET calls to the server.
  43. attempts := max * 2
  44. var wg sync.WaitGroup
  45. var failed int32
  46. for i := 0; i < attempts; i++ {
  47. wg.Add(1)
  48. go func() {
  49. defer wg.Done()
  50. c := http.Client{Timeout: 3 * time.Second}
  51. r, err := c.Get("http://" + l.Addr().String())
  52. if err != nil {
  53. t.Log(err)
  54. atomic.AddInt32(&failed, 1)
  55. return
  56. }
  57. defer r.Body.Close()
  58. _, err = io.Copy(ioutil.Discard, r.Body)
  59. require.NoError(t, err)
  60. }()
  61. }
  62. wg.Wait()
  63. // We expect some Gets to fail as the server's accept queue is filled,
  64. // but most should succeed.
  65. if int(failed) >= attempts/2 {
  66. t.Errorf("%d requests failed within %d attempts", failed, attempts)
  67. }
  68. }
  69. func TestServeTLS(t *testing.T) {
  70. ln, err := net.Listen("tcp", "localhost:0")
  71. require.NoError(t, err)
  72. defer ln.Close()
  73. mux := http.NewServeMux()
  74. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  75. fmt.Fprint(w, "some body")
  76. })
  77. go ServeTLS(ln, mux, "test.crt", "test.key", log.TestingLogger(), DefaultConfig())
  78. tr := &http.Transport{
  79. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  80. }
  81. c := &http.Client{Transport: tr}
  82. res, err := c.Get("https://" + ln.Addr().String())
  83. require.NoError(t, err)
  84. defer res.Body.Close()
  85. assert.Equal(t, http.StatusOK, res.StatusCode)
  86. body, err := ioutil.ReadAll(res.Body)
  87. require.NoError(t, err)
  88. assert.Equal(t, []byte("some body"), body)
  89. }
  90. func TestWriteRPCResponseHTTP(t *testing.T) {
  91. id := types.JSONRPCIntID(-1)
  92. // one argument
  93. w := httptest.NewRecorder()
  94. WriteRPCResponseHTTP(w, types.NewRPCSuccessResponse(id, &sampleResult{"hello"}))
  95. resp := w.Result()
  96. body, err := ioutil.ReadAll(resp.Body)
  97. _ = resp.Body.Close()
  98. require.NoError(t, err)
  99. assert.Equal(t, 200, resp.StatusCode)
  100. assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
  101. assert.Equal(t, `{
  102. "jsonrpc": "2.0",
  103. "id": -1,
  104. "result": {
  105. "value": "hello"
  106. }
  107. }`, string(body))
  108. // multiple arguments
  109. w = httptest.NewRecorder()
  110. WriteRPCResponseHTTP(w,
  111. types.NewRPCSuccessResponse(id, &sampleResult{"hello"}),
  112. types.NewRPCSuccessResponse(id, &sampleResult{"world"}))
  113. resp = w.Result()
  114. body, err = ioutil.ReadAll(resp.Body)
  115. _ = resp.Body.Close()
  116. require.NoError(t, err)
  117. assert.Equal(t, 200, resp.StatusCode)
  118. assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
  119. assert.Equal(t, `[
  120. {
  121. "jsonrpc": "2.0",
  122. "id": -1,
  123. "result": {
  124. "value": "hello"
  125. }
  126. },
  127. {
  128. "jsonrpc": "2.0",
  129. "id": -1,
  130. "result": {
  131. "value": "world"
  132. }
  133. }
  134. ]`, string(body))
  135. }
  136. func TestWriteRPCResponseHTTPError(t *testing.T) {
  137. w := httptest.NewRecorder()
  138. WriteRPCResponseHTTPError(w,
  139. http.StatusInternalServerError,
  140. types.RPCInternalError(types.JSONRPCIntID(-1), errors.New("foo")))
  141. resp := w.Result()
  142. body, err := ioutil.ReadAll(resp.Body)
  143. _ = resp.Body.Close()
  144. require.NoError(t, err)
  145. assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
  146. assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
  147. assert.Equal(t, `{
  148. "jsonrpc": "2.0",
  149. "id": -1,
  150. "error": {
  151. "code": -32603,
  152. "message": "Internal error",
  153. "data": "foo"
  154. }
  155. }`, string(body))
  156. }