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.

180 lines
4.3 KiB

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