|
@@ -1,6 +1,7 @@
|
|
package handler
|
|
package handler
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "context"
|
|
"io/ioutil"
|
|
"io/ioutil"
|
|
"log"
|
|
"log"
|
|
"net/http"
|
|
"net/http"
|
|
@@ -39,6 +40,20 @@ func TestWithinTimeout(t *testing.T) {
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func TestWithTimeoutTimedout(t *testing.T) {
|
|
|
|
+ timeoutHandler := TimeoutHandler(time.Millisecond)
|
|
|
|
+ handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ time.Sleep(time.Millisecond * 10)
|
|
|
|
+ w.Write([]byte(`foo`))
|
|
|
|
+ w.WriteHeader(http.StatusOK)
|
|
|
|
+ }))
|
|
|
|
+
|
|
|
|
+ req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
|
|
|
+ resp := httptest.NewRecorder()
|
|
|
|
+ handler.ServeHTTP(resp, req)
|
|
|
|
+ assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
|
|
|
|
+}
|
|
|
|
+
|
|
func TestWithoutTimeout(t *testing.T) {
|
|
func TestWithoutTimeout(t *testing.T) {
|
|
timeoutHandler := TimeoutHandler(0)
|
|
timeoutHandler := TimeoutHandler(0)
|
|
handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
@@ -50,3 +65,91 @@ func TestWithoutTimeout(t *testing.T) {
|
|
handler.ServeHTTP(resp, req)
|
|
handler.ServeHTTP(resp, req)
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func TestTimeoutPanic(t *testing.T) {
|
|
|
|
+ timeoutHandler := TimeoutHandler(time.Minute)
|
|
|
|
+ handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ panic("foo")
|
|
|
|
+ }))
|
|
|
|
+
|
|
|
|
+ req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
|
|
|
+ resp := httptest.NewRecorder()
|
|
|
|
+ assert.Panics(t, func() {
|
|
|
|
+ handler.ServeHTTP(resp, req)
|
|
|
|
+ })
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func TestTimeoutWroteHeaderTwice(t *testing.T) {
|
|
|
|
+ timeoutHandler := TimeoutHandler(time.Minute)
|
|
|
|
+ handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ w.Write([]byte(`hello`))
|
|
|
|
+ w.Header().Set("foo", "bar")
|
|
|
|
+ w.WriteHeader(http.StatusOK)
|
|
|
|
+ }))
|
|
|
|
+
|
|
|
|
+ req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
|
|
|
+ resp := httptest.NewRecorder()
|
|
|
|
+ handler.ServeHTTP(resp, req)
|
|
|
|
+ assert.Equal(t, http.StatusOK, resp.Code)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func TestTimeoutWriteBadCode(t *testing.T) {
|
|
|
|
+ timeoutHandler := TimeoutHandler(time.Minute)
|
|
|
|
+ handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ w.WriteHeader(1000)
|
|
|
|
+ }))
|
|
|
|
+
|
|
|
|
+ req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
|
|
|
+ resp := httptest.NewRecorder()
|
|
|
|
+ assert.Panics(t, func() {
|
|
|
|
+ handler.ServeHTTP(resp, req)
|
|
|
|
+ })
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func TestTimeoutClientClosed(t *testing.T) {
|
|
|
|
+ timeoutHandler := TimeoutHandler(time.Minute)
|
|
|
|
+ handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ w.WriteHeader(1000)
|
|
|
|
+ }))
|
|
|
|
+
|
|
|
|
+ req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
|
|
|
+ ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
+ req = req.WithContext(ctx)
|
|
|
|
+ cancel()
|
|
|
|
+ resp := httptest.NewRecorder()
|
|
|
|
+ handler.ServeHTTP(resp, req)
|
|
|
|
+ assert.Equal(t, statusClientClosedRequest, resp.Code)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func TestTimeoutPusher(t *testing.T) {
|
|
|
|
+ handler := &timeoutWriter{
|
|
|
|
+ w: mockedPusher{},
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ assert.Panics(t, func() {
|
|
|
|
+ handler.Push("any", nil)
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ handler = &timeoutWriter{
|
|
|
|
+ w: httptest.NewRecorder(),
|
|
|
|
+ }
|
|
|
|
+ assert.Equal(t, http.ErrNotSupported, handler.Push("any", nil))
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type mockedPusher struct{}
|
|
|
|
+
|
|
|
|
+func (m mockedPusher) Header() http.Header {
|
|
|
|
+ panic("implement me")
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (m mockedPusher) Write(bytes []byte) (int, error) {
|
|
|
|
+ panic("implement me")
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (m mockedPusher) WriteHeader(statusCode int) {
|
|
|
|
+ panic("implement me")
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (m mockedPusher) Push(target string, opts *http.PushOptions) error {
|
|
|
|
+ panic("implement me")
|
|
|
|
+}
|