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.

183 lines
4.4 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. err := WriteRPCResponseHTTP(w, true, types.NewRPCSuccessResponse(id, &sampleResult{"hello"}))
  100. require.NoError(t, err)
  101. resp := w.Result()
  102. body, err := ioutil.ReadAll(resp.Body)
  103. _ = resp.Body.Close()
  104. require.NoError(t, err)
  105. assert.Equal(t, 200, resp.StatusCode)
  106. assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
  107. assert.Equal(t, "max-age=31536000", resp.Header.Get("Cache-control"))
  108. assert.Equal(t, `{
  109. "jsonrpc": "2.0",
  110. "id": -1,
  111. "result": {
  112. "value": "hello"
  113. }
  114. }`, string(body))
  115. // multiple arguments
  116. w = httptest.NewRecorder()
  117. err = WriteRPCResponseHTTP(w,
  118. false,
  119. types.NewRPCSuccessResponse(id, &sampleResult{"hello"}),
  120. types.NewRPCSuccessResponse(id, &sampleResult{"world"}))
  121. require.NoError(t, err)
  122. resp = w.Result()
  123. body, err = ioutil.ReadAll(resp.Body)
  124. _ = resp.Body.Close()
  125. require.NoError(t, err)
  126. assert.Equal(t, 200, resp.StatusCode)
  127. assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
  128. assert.Equal(t, `[
  129. {
  130. "jsonrpc": "2.0",
  131. "id": -1,
  132. "result": {
  133. "value": "hello"
  134. }
  135. },
  136. {
  137. "jsonrpc": "2.0",
  138. "id": -1,
  139. "result": {
  140. "value": "world"
  141. }
  142. }
  143. ]`, string(body))
  144. }
  145. func TestWriteRPCResponseHTTPError(t *testing.T) {
  146. w := httptest.NewRecorder()
  147. err := WriteRPCResponseHTTPError(w, types.RPCInternalError(types.JSONRPCIntID(-1), errors.New("foo")))
  148. require.NoError(t, err)
  149. resp := w.Result()
  150. body, err := ioutil.ReadAll(resp.Body)
  151. _ = resp.Body.Close()
  152. require.NoError(t, err)
  153. assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
  154. assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
  155. assert.Equal(t, `{
  156. "jsonrpc": "2.0",
  157. "id": -1,
  158. "error": {
  159. "code": -32603,
  160. "message": "Internal error",
  161. "data": "foo"
  162. }
  163. }`, string(body))
  164. }