timeouthandler_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package handler
  2. import (
  3. "context"
  4. "io"
  5. "log"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/zeromicro/go-zero/rest/internal/response"
  12. )
  13. func init() {
  14. log.SetOutput(io.Discard)
  15. }
  16. func TestTimeout(t *testing.T) {
  17. timeoutHandler := TimeoutHandler(time.Millisecond)
  18. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  19. time.Sleep(time.Minute)
  20. }))
  21. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  22. resp := httptest.NewRecorder()
  23. handler.ServeHTTP(resp, req)
  24. assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
  25. }
  26. func TestWithinTimeout(t *testing.T) {
  27. timeoutHandler := TimeoutHandler(time.Second)
  28. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  29. time.Sleep(time.Millisecond)
  30. }))
  31. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  32. resp := httptest.NewRecorder()
  33. handler.ServeHTTP(resp, req)
  34. assert.Equal(t, http.StatusOK, resp.Code)
  35. }
  36. func TestWithTimeoutTimedout(t *testing.T) {
  37. timeoutHandler := TimeoutHandler(time.Millisecond)
  38. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  39. time.Sleep(time.Millisecond * 10)
  40. w.Write([]byte(`foo`))
  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. w.Write([]byte(`hello`))
  84. w.Header().Set("foo", "bar")
  85. w.WriteHeader(http.StatusOK)
  86. }))
  87. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  88. resp := httptest.NewRecorder()
  89. handler.ServeHTTP(resp, req)
  90. assert.Equal(t, http.StatusOK, resp.Code)
  91. }
  92. func TestTimeoutWriteBadCode(t *testing.T) {
  93. timeoutHandler := TimeoutHandler(time.Minute)
  94. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  95. w.WriteHeader(1000)
  96. }))
  97. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  98. resp := httptest.NewRecorder()
  99. assert.Panics(t, func() {
  100. handler.ServeHTTP(resp, req)
  101. })
  102. }
  103. func TestTimeoutClientClosed(t *testing.T) {
  104. timeoutHandler := TimeoutHandler(time.Minute)
  105. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  106. w.WriteHeader(http.StatusServiceUnavailable)
  107. }))
  108. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  109. ctx, cancel := context.WithCancel(context.Background())
  110. req = req.WithContext(ctx)
  111. cancel()
  112. resp := httptest.NewRecorder()
  113. handler.ServeHTTP(resp, req)
  114. assert.Equal(t, statusClientClosedRequest, resp.Code)
  115. }
  116. func TestTimeoutHijack(t *testing.T) {
  117. resp := httptest.NewRecorder()
  118. writer := &timeoutWriter{
  119. w: &response.WithCodeResponseWriter{
  120. Writer: resp,
  121. },
  122. }
  123. assert.NotPanics(t, func() {
  124. writer.Hijack()
  125. })
  126. writer = &timeoutWriter{
  127. w: &response.WithCodeResponseWriter{
  128. Writer: mockedHijackable{resp},
  129. },
  130. }
  131. assert.NotPanics(t, func() {
  132. writer.Hijack()
  133. })
  134. }
  135. func TestTimeoutPusher(t *testing.T) {
  136. handler := &timeoutWriter{
  137. w: mockedPusher{},
  138. }
  139. assert.Panics(t, func() {
  140. handler.Push("any", nil)
  141. })
  142. handler = &timeoutWriter{
  143. w: httptest.NewRecorder(),
  144. }
  145. assert.Equal(t, http.ErrNotSupported, handler.Push("any", nil))
  146. }
  147. type mockedPusher struct{}
  148. func (m mockedPusher) Header() http.Header {
  149. panic("implement me")
  150. }
  151. func (m mockedPusher) Write(bytes []byte) (int, error) {
  152. panic("implement me")
  153. }
  154. func (m mockedPusher) WriteHeader(statusCode int) {
  155. panic("implement me")
  156. }
  157. func (m mockedPusher) Push(target string, opts *http.PushOptions) error {
  158. panic("implement me")
  159. }