timeouthandler_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package handler
  2. import (
  3. "context"
  4. "io/ioutil"
  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(ioutil.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", nil)
  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", nil)
  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", nil)
  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", nil)
  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", nil)
  63. resp := httptest.NewRecorder()
  64. assert.Panics(t, func() {
  65. handler.ServeHTTP(resp, req)
  66. })
  67. }
  68. func TestTimeoutWroteHeaderTwice(t *testing.T) {
  69. timeoutHandler := TimeoutHandler(time.Minute)
  70. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  71. w.Write([]byte(`hello`))
  72. w.Header().Set("foo", "bar")
  73. w.WriteHeader(http.StatusOK)
  74. }))
  75. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  76. resp := httptest.NewRecorder()
  77. handler.ServeHTTP(resp, req)
  78. assert.Equal(t, http.StatusOK, resp.Code)
  79. }
  80. func TestTimeoutWriteBadCode(t *testing.T) {
  81. timeoutHandler := TimeoutHandler(time.Minute)
  82. handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  83. w.WriteHeader(1000)
  84. }))
  85. req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
  86. resp := httptest.NewRecorder()
  87. assert.Panics(t, func() {
  88. handler.ServeHTTP(resp, req)
  89. })
  90. }
  91. func TestTimeoutClientClosed(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", nil)
  97. ctx, cancel := context.WithCancel(context.Background())
  98. req = req.WithContext(ctx)
  99. cancel()
  100. resp := httptest.NewRecorder()
  101. handler.ServeHTTP(resp, req)
  102. assert.Equal(t, statusClientClosedRequest, resp.Code)
  103. }
  104. func TestTimeoutPusher(t *testing.T) {
  105. handler := &timeoutWriter{
  106. w: mockedPusher{},
  107. }
  108. assert.Panics(t, func() {
  109. handler.Push("any", nil)
  110. })
  111. handler = &timeoutWriter{
  112. w: httptest.NewRecorder(),
  113. }
  114. assert.Equal(t, http.ErrNotSupported, handler.Push("any", nil))
  115. }
  116. type mockedPusher struct{}
  117. func (m mockedPusher) Header() http.Header {
  118. panic("implement me")
  119. }
  120. func (m mockedPusher) Write(bytes []byte) (int, error) {
  121. panic("implement me")
  122. }
  123. func (m mockedPusher) WriteHeader(statusCode int) {
  124. panic("implement me")
  125. }
  126. func (m mockedPusher) Push(target string, opts *http.PushOptions) error {
  127. panic("implement me")
  128. }