Skip to content

Commit

Permalink
Move the checks in the http handler for timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilija Matoski committed Jan 4, 2021
1 parent c69860a commit 5f78d62
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions middleware/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,35 +147,31 @@ func TestTimeoutTestRequestClone(t *testing.T) {
rec := httptest.NewRecorder()

m := TimeoutWithConfig(TimeoutConfig{
// Timeout has to be defined or the whole flow for timeout middleware will be skipped
Timeout: time.Second,
ErrorHandler: func(err error, c echo.Context) error {
assert.EqualError(t, err, context.DeadlineExceeded.Error())

// Cookie test
cookie, err := c.Request().Cookie("cookie")
if assert.NoError(t, err) {
assert.EqualValues(t, "cookie", cookie.Name)
assert.EqualValues(t, "value", cookie.Value)
}

// Form values
if assert.NoError(t, c.Request().ParseForm()) {
assert.EqualValues(t, "value", c.Request().FormValue("form"))
}

// Query string
assert.EqualValues(t, "value", c.Request().URL.Query()["query"][0])
return errors.New("err")
},
})

e := echo.New()
c := e.NewContext(req, rec)

err := m(func(c echo.Context) error {
time.Sleep(time.Minute)
// Cookie test
cookie, err := c.Request().Cookie("cookie")
if assert.NoError(t, err) {
assert.EqualValues(t, "cookie", cookie.Name)
assert.EqualValues(t, "value", cookie.Value)
}

// Form values
if assert.NoError(t, c.Request().ParseForm()) {
assert.EqualValues(t, "value", c.Request().FormValue("form"))
}

// Query string
assert.EqualValues(t, "value", c.Request().URL.Query()["query"][0])
return nil
})(c)

assert.Error(t, err)
assert.NoError(t, err)

}

0 comments on commit 5f78d62

Please sign in to comment.