Skip to content

Commit

Permalink
split test into two
Browse files Browse the repository at this point in the history
  • Loading branch information
jlsherrill committed May 2, 2024
1 parent 771439f commit cf88dc3
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,36 @@ func TestMiddleware(t *testing.T) {
RequestLatencyLevel: zerolog.WarnLevel,
})

// Slow request should be logged at the escalated level
next := func(c echo.Context) error {
time.Sleep(5 * time.Millisecond)
return nil
}
handler := m(next)
err := handler(c)
assert.NoError(t, err, "should not return error")

str := b.String()
assert.Contains(t, str, `"level":"warn"`)
assert.NotContains(t, str, `"level":"info"`)
})

t.Run("shouldn't escalate log level for fast requests", func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)

b := &bytes.Buffer{}
l := lecho.New(b)
l.SetLevel(log.INFO)
m := lecho.Middleware(lecho.Config{
Logger: l,
RequestLatencyLimit: 5 * time.Millisecond,
RequestLatencyLevel: zerolog.WarnLevel,
})

// Fast request should be logged at the default level
next := func(c echo.Context) error {
time.Sleep(1 * time.Millisecond)
Expand All @@ -130,17 +160,5 @@ func TestMiddleware(t *testing.T) {
str := b.String()
assert.Contains(t, str, `"level":"info"`)
assert.NotContains(t, str, `"level":"warn"`)

// Slow request should be logged at the escalated level
next = func(c echo.Context) error {
time.Sleep(5 * time.Millisecond)
return nil
}
handler = m(next)
err = handler(c)

str = b.String()
assert.Contains(t, str, `"level":"warn"`)
})

}

0 comments on commit cf88dc3

Please sign in to comment.