timeouthandler_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. )
  12. func init() {
  13. log.SetOutput(io.Discard)
  14. }
  15. func TestTimeout(t *testing.T) {
  16. timeoutHandler := TimeoutHandler(time.Millisecond)
  17. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  18. time.Sleep(time.Minute)
  19. }))
  20. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  21. resp := httptest.NewRecorder()
  22. handler.ServeHTTP(resp, req)
  23. assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
  24. }
  25. func TestWithinTimeout(t *testing.T) {
  26. timeoutHandler := TimeoutHandler(time.Second)
  27. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  28. time.Sleep(time.Millisecond)
  29. }))
  30. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  31. resp := httptest.NewRecorder()
  32. handler.ServeHTTP(resp, req)
  33. assert.Equal(t, http.StatusOK, resp.Code)
  34. }
  35. func TestWithTimeoutTimedout(t *testing.T) {
  36. timeoutHandler := TimeoutHandler(time.Millisecond)
  37. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  38. time.Sleep(time.Millisecond * 10)
  39. w.Write([]byte(`foo`))
  40. w.WriteHeader(http.StatusOK)
  41. }))
  42. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  43. resp := httptest.NewRecorder()
  44. handler.ServeHTTP(resp, req)
  45. assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
  46. }
  47. func TestWithoutTimeout(t *testing.T) {
  48. timeoutHandler := TimeoutHandler(0)
  49. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  50. time.Sleep(100 * time.Millisecond)
  51. }))
  52. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  53. resp := httptest.NewRecorder()
  54. handler.ServeHTTP(resp, req)
  55. assert.Equal(t, http.StatusOK, resp.Code)
  56. }
  57. func TestTimeoutPanic(t *testing.T) {
  58. timeoutHandler := TimeoutHandler(time.Minute)
  59. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  60. panic("foo")
  61. }))
  62. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  63. resp := httptest.NewRecorder()
  64. assert.Panics(t, func() {
  65. handler.ServeHTTP(resp, req)
  66. })
  67. }
  68. func TestTimeoutWebsocket(t *testing.T) {
  69. timeoutHandler := TimeoutHandler(time.Millisecond)
  70. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  71. time.Sleep(time.Millisecond * 10)
  72. }))
  73. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  74. req.Header.Set(headerUpgrade, valueWebsocket)
  75. resp := httptest.NewRecorder()
  76. handler.ServeHTTP(resp, req)
  77. assert.Equal(t, http.StatusOK, resp.Code)
  78. }
  79. func TestTimeoutWroteHeaderTwice(t *testing.T) {
  80. timeoutHandler := TimeoutHandler(time.Minute)
  81. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  82. w.Write([]byte(`hello`))
  83. w.Header().Set("foo", "bar")
  84. w.WriteHeader(http.StatusOK)
  85. }))
  86. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  87. resp := httptest.NewRecorder()
  88. handler.ServeHTTP(resp, req)
  89. assert.Equal(t, http.StatusOK, resp.Code)
  90. }
  91. func TestTimeoutWriteBadCode(t *testing.T) {
  92. timeoutHandler := TimeoutHandler(time.Minute)
  93. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  94. w.WriteHeader(1000)
  95. }))
  96. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  97. resp := httptest.NewRecorder()
  98. assert.Panics(t, func() {
  99. handler.ServeHTTP(resp, req)
  100. })
  101. }
  102. func TestTimeoutClientClosed(t *testing.T) {
  103. timeoutHandler := TimeoutHandler(time.Minute)
  104. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  105. w.WriteHeader(http.StatusServiceUnavailable)
  106. }))
  107. req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
  108. ctx, cancel := context.WithCancel(context.Background())
  109. req = req.WithContext(ctx)
  110. cancel()
  111. resp := httptest.NewRecorder()
  112. handler.ServeHTTP(resp, req)
  113. assert.Equal(t, statusClientClosedRequest, resp.Code)
  114. }
  115. func TestTimeoutPusher(t *testing.T) {
  116. handler := &timeoutWriter{
  117. w: mockedPusher{},
  118. }
  119. assert.Panics(t, func() {
  120. handler.Push("any", nil)
  121. })
  122. handler = &timeoutWriter{
  123. w: httptest.NewRecorder(),
  124. }
  125. assert.Equal(t, http.ErrNotSupported, handler.Push("any", nil))
  126. }
  127. type mockedPusher struct{}
  128. func (m mockedPusher) Header() http.Header {
  129. panic("implement me")
  130. }
  131. func (m mockedPusher) Write(bytes []byte) (int, error) {
  132. panic("implement me")
  133. }
  134. func (m mockedPusher) WriteHeader(statusCode int) {
  135. panic("implement me")
  136. }
  137. func (m mockedPusher) Push(target string, opts *http.PushOptions) error {
  138. panic("implement me")
  139. }