|
| 1 | +// +build go1.13 |
| 2 | + |
| 3 | +package middleware |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "github.com/labstack/echo/v4" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "net/url" |
| 13 | + "reflect" |
| 14 | + "strings" |
| 15 | + "testing" |
| 16 | + "time" |
| 17 | +) |
| 18 | + |
| 19 | +func TestTimeoutSkipper(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + m := TimeoutWithConfig(TimeoutConfig{ |
| 22 | + Skipper: func(context echo.Context) bool { |
| 23 | + return true |
| 24 | + }, |
| 25 | + }) |
| 26 | + |
| 27 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 28 | + rec := httptest.NewRecorder() |
| 29 | + |
| 30 | + e := echo.New() |
| 31 | + c := e.NewContext(req, rec) |
| 32 | + |
| 33 | + err := m(func(c echo.Context) error { |
| 34 | + assert.NotEqual(t, "*context.timerCtx", reflect.TypeOf(c.Request().Context()).String()) |
| 35 | + return nil |
| 36 | + })(c) |
| 37 | + |
| 38 | + assert.NoError(t, err) |
| 39 | +} |
| 40 | + |
| 41 | +func TestTimeoutWithTimeout0(t *testing.T) { |
| 42 | + t.Parallel() |
| 43 | + m := TimeoutWithConfig(TimeoutConfig{ |
| 44 | + Timeout: 0, |
| 45 | + }) |
| 46 | + |
| 47 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 48 | + rec := httptest.NewRecorder() |
| 49 | + |
| 50 | + e := echo.New() |
| 51 | + c := e.NewContext(req, rec) |
| 52 | + |
| 53 | + err := m(func(c echo.Context) error { |
| 54 | + assert.NotEqual(t, "*context.timerCtx", reflect.TypeOf(c.Request().Context()).String()) |
| 55 | + return nil |
| 56 | + })(c) |
| 57 | + |
| 58 | + assert.NoError(t, err) |
| 59 | +} |
| 60 | + |
| 61 | +func TestTimeoutIsCancelable(t *testing.T) { |
| 62 | + t.Parallel() |
| 63 | + m := TimeoutWithConfig(TimeoutConfig{ |
| 64 | + Timeout: time.Minute, |
| 65 | + }) |
| 66 | + |
| 67 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 68 | + rec := httptest.NewRecorder() |
| 69 | + |
| 70 | + e := echo.New() |
| 71 | + c := e.NewContext(req, rec) |
| 72 | + |
| 73 | + err := m(func(c echo.Context) error { |
| 74 | + assert.EqualValues(t, "*context.timerCtx", reflect.TypeOf(c.Request().Context()).String()) |
| 75 | + return nil |
| 76 | + })(c) |
| 77 | + |
| 78 | + assert.NoError(t, err) |
| 79 | +} |
| 80 | + |
| 81 | +func TestTimeoutErrorOutInHandler(t *testing.T) { |
| 82 | + t.Parallel() |
| 83 | + m := Timeout() |
| 84 | + |
| 85 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 86 | + rec := httptest.NewRecorder() |
| 87 | + |
| 88 | + e := echo.New() |
| 89 | + c := e.NewContext(req, rec) |
| 90 | + |
| 91 | + err := m(func(c echo.Context) error { |
| 92 | + return errors.New("err") |
| 93 | + })(c) |
| 94 | + |
| 95 | + assert.Error(t, err) |
| 96 | +} |
| 97 | + |
| 98 | +func TestTimeoutTimesOutAfterPredefinedTimeoutWithErrorHandler(t *testing.T) { |
| 99 | + t.Parallel() |
| 100 | + m := TimeoutWithConfig(TimeoutConfig{ |
| 101 | + Timeout: time.Second, |
| 102 | + ErrorHandler: func(err error, e echo.Context) error { |
| 103 | + assert.EqualError(t, err, context.DeadlineExceeded.Error()) |
| 104 | + return errors.New("err") |
| 105 | + }, |
| 106 | + }) |
| 107 | + |
| 108 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 109 | + rec := httptest.NewRecorder() |
| 110 | + |
| 111 | + e := echo.New() |
| 112 | + c := e.NewContext(req, rec) |
| 113 | + |
| 114 | + err := m(func(c echo.Context) error { |
| 115 | + time.Sleep(time.Minute) |
| 116 | + return nil |
| 117 | + })(c) |
| 118 | + |
| 119 | + assert.EqualError(t, err, errors.New("err").Error()) |
| 120 | +} |
| 121 | + |
| 122 | +func TestTimeoutTimesOutAfterPredefinedTimeout(t *testing.T) { |
| 123 | + t.Parallel() |
| 124 | + m := TimeoutWithConfig(TimeoutConfig{ |
| 125 | + Timeout: time.Second, |
| 126 | + }) |
| 127 | + |
| 128 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 129 | + rec := httptest.NewRecorder() |
| 130 | + |
| 131 | + e := echo.New() |
| 132 | + c := e.NewContext(req, rec) |
| 133 | + |
| 134 | + err := m(func(c echo.Context) error { |
| 135 | + time.Sleep(time.Minute) |
| 136 | + return nil |
| 137 | + })(c) |
| 138 | + |
| 139 | + assert.EqualError(t, err, context.DeadlineExceeded.Error()) |
| 140 | +} |
| 141 | + |
| 142 | +func TestTimeoutTestRequestClone(t *testing.T) { |
| 143 | + t.Parallel() |
| 144 | + req := httptest.NewRequest(http.MethodPost, "/uri?query=value", strings.NewReader(url.Values{"form": {"value"}}.Encode())) |
| 145 | + req.AddCookie(&http.Cookie{Name: "cookie", Value: "value"}) |
| 146 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 147 | + rec := httptest.NewRecorder() |
| 148 | + |
| 149 | + m := TimeoutWithConfig(TimeoutConfig{ |
| 150 | + // Timeout has to be defined or the whole flow for timeout middleware will be skipped |
| 151 | + Timeout: time.Second, |
| 152 | + }) |
| 153 | + |
| 154 | + e := echo.New() |
| 155 | + c := e.NewContext(req, rec) |
| 156 | + |
| 157 | + err := m(func(c echo.Context) error { |
| 158 | + // Cookie test |
| 159 | + cookie, err := c.Request().Cookie("cookie") |
| 160 | + if assert.NoError(t, err) { |
| 161 | + assert.EqualValues(t, "cookie", cookie.Name) |
| 162 | + assert.EqualValues(t, "value", cookie.Value) |
| 163 | + } |
| 164 | + |
| 165 | + // Form values |
| 166 | + if assert.NoError(t, c.Request().ParseForm()) { |
| 167 | + assert.EqualValues(t, "value", c.Request().FormValue("form")) |
| 168 | + } |
| 169 | + |
| 170 | + // Query string |
| 171 | + assert.EqualValues(t, "value", c.Request().URL.Query()["query"][0]) |
| 172 | + return nil |
| 173 | + })(c) |
| 174 | + |
| 175 | + assert.NoError(t, err) |
| 176 | + |
| 177 | +} |
0 commit comments