timeouthandler_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package handler
  2. import (
  3. "context"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/zeromicro/go-zero/core/logx/logtest"
  10. "github.com/zeromicro/go-zero/rest/internal/response"
  11. )
  12. func TestTimeout(t *testing.T) {
  13. timeoutHandler := TimeoutHandler(time.Millisecond)
  14. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  15. time.Sleep(time.Minute)
  16. }))
  17. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  18. resp := httptest.NewRecorder()
  19. handler.ServeHTTP(resp, req)
  20. assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
  21. }
  22. func TestWithinTimeout(t *testing.T) {
  23. timeoutHandler := TimeoutHandler(time.Second)
  24. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  25. time.Sleep(time.Millisecond)
  26. }))
  27. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  28. resp := httptest.NewRecorder()
  29. handler.ServeHTTP(resp, req)
  30. assert.Equal(t, http.StatusOK, resp.Code)
  31. }
  32. func TestWithTimeoutTimedout(t *testing.T) {
  33. timeoutHandler := TimeoutHandler(time.Millisecond)
  34. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  35. time.Sleep(time.Millisecond * 10)
  36. _, err := w.Write([]byte(`foo`))
  37. if err != nil {
  38. w.WriteHeader(http.StatusInternalServerError)
  39. return
  40. }
  41. w.WriteHeader(http.StatusOK)
  42. }))
  43. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  44. resp := httptest.NewRecorder()
  45. handler.ServeHTTP(resp, req)
  46. assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
  47. }
  48. func TestWithoutTimeout(t *testing.T) {
  49. timeoutHandler := TimeoutHandler(0)
  50. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  51. time.Sleep(100 * time.Millisecond)
  52. }))
  53. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  54. resp := httptest.NewRecorder()
  55. handler.ServeHTTP(resp, req)
  56. assert.Equal(t, http.StatusOK, resp.Code)
  57. }
  58. func TestTimeoutPanic(t *testing.T) {
  59. timeoutHandler := TimeoutHandler(time.Minute)
  60. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  61. panic("foo")
  62. }))
  63. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  64. resp := httptest.NewRecorder()
  65. assert.Panics(t, func() {
  66. handler.ServeHTTP(resp, req)
  67. })
  68. }
  69. func TestTimeoutWebsocket(t *testing.T) {
  70. timeoutHandler := TimeoutHandler(time.Millisecond)
  71. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  72. time.Sleep(time.Millisecond * 10)
  73. }))
  74. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  75. req.Header.Set(headerUpgrade, valueWebsocket)
  76. resp := httptest.NewRecorder()
  77. handler.ServeHTTP(resp, req)
  78. assert.Equal(t, http.StatusOK, resp.Code)
  79. }
  80. func TestTimeoutWroteHeaderTwice(t *testing.T) {
  81. timeoutHandler := TimeoutHandler(time.Minute)
  82. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  83. _, err := w.Write([]byte(`hello`))
  84. if err != nil {
  85. w.WriteHeader(http.StatusInternalServerError)
  86. return
  87. }
  88. w.Header().Set("foo", "bar")
  89. w.WriteHeader(http.StatusOK)
  90. }))
  91. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  92. resp := httptest.NewRecorder()
  93. handler.ServeHTTP(resp, req)
  94. assert.Equal(t, http.StatusOK, resp.Code)
  95. }
  96. func TestTimeoutWriteBadCode(t *testing.T) {
  97. timeoutHandler := TimeoutHandler(time.Minute)
  98. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  99. w.WriteHeader(1000)
  100. }))
  101. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  102. resp := httptest.NewRecorder()
  103. assert.Panics(t, func() {
  104. handler.ServeHTTP(resp, req)
  105. })
  106. }
  107. func TestTimeoutClientClosed(t *testing.T) {
  108. timeoutHandler := TimeoutHandler(time.Minute)
  109. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  110. w.WriteHeader(http.StatusServiceUnavailable)
  111. }))
  112. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  113. ctx, cancel := context.WithCancel(context.Background())
  114. req = req.WithContext(ctx)
  115. cancel()
  116. resp := httptest.NewRecorder()
  117. handler.ServeHTTP(resp, req)
  118. assert.Equal(t, statusClientClosedRequest, resp.Code)
  119. }
  120. func TestTimeoutHijack(t *testing.T) {
  121. resp := httptest.NewRecorder()
  122. writer := &timeoutWriter{
  123. w: &response.WithCodeResponseWriter{
  124. Writer: resp,
  125. },
  126. }
  127. assert.NotPanics(t, func() {
  128. _, _, _ = writer.Hijack()
  129. })
  130. writer = &timeoutWriter{
  131. w: &response.WithCodeResponseWriter{
  132. Writer: mockedHijackable{resp},
  133. },
  134. }
  135. assert.NotPanics(t, func() {
  136. _, _, _ = writer.Hijack()
  137. })
  138. }
  139. func TestTimeoutFlush(t *testing.T) {
  140. timeoutHandler := TimeoutHandler(time.Minute)
  141. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  142. flusher, ok := w.(http.Flusher)
  143. if !ok {
  144. http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
  145. return
  146. }
  147. flusher.Flush()
  148. }))
  149. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  150. resp := httptest.NewRecorder()
  151. handler.ServeHTTP(resp, req)
  152. assert.Equal(t, http.StatusOK, resp.Code)
  153. }
  154. func TestTimeoutPusher(t *testing.T) {
  155. handler := &timeoutWriter{
  156. w: mockedPusher{},
  157. }
  158. assert.Panics(t, func() {
  159. _ = handler.Push("any", nil)
  160. })
  161. handler = &timeoutWriter{
  162. w: httptest.NewRecorder(),
  163. }
  164. assert.Equal(t, http.ErrNotSupported, handler.Push("any", nil))
  165. }
  166. func TestTimeoutWriter_Hijack(t *testing.T) {
  167. writer := &timeoutWriter{
  168. w: httptest.NewRecorder(),
  169. h: make(http.Header),
  170. req: httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody),
  171. }
  172. _, _, err := writer.Hijack()
  173. assert.Error(t, err)
  174. }
  175. func TestTimeoutWroteTwice(t *testing.T) {
  176. c := logtest.NewCollector(t)
  177. writer := &timeoutWriter{
  178. w: &response.WithCodeResponseWriter{
  179. Writer: httptest.NewRecorder(),
  180. },
  181. h: make(http.Header),
  182. req: httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody),
  183. }
  184. writer.writeHeaderLocked(http.StatusOK)
  185. writer.writeHeaderLocked(http.StatusOK)
  186. assert.Contains(t, c.String(), "superfluous response.WriteHeader call")
  187. }
  188. type mockedPusher struct{}
  189. func (m mockedPusher) Header() http.Header {
  190. panic("implement me")
  191. }
  192. func (m mockedPusher) Write(_ []byte) (int, error) {
  193. panic("implement me")
  194. }
  195. func (m mockedPusher) WriteHeader(_ int) {
  196. panic("implement me")
  197. }
  198. func (m mockedPusher) Push(_ string, _ *http.PushOptions) error {
  199. panic("implement me")
  200. }