mirror of
https://github.com/labstack/echo.git
synced 2025-07-03 00:56:59 +02:00
@ -17,37 +17,37 @@ func TestLogger(t *testing.T) {
|
||||
rq := test.NewRequest(echo.GET, "/", nil)
|
||||
rec := test.NewResponseRecorder()
|
||||
c := echo.NewContext(rq, rec, e)
|
||||
h := Logger()(echo.HandlerFunc(func(c echo.Context) error {
|
||||
h := Logger()(func(c echo.Context) error {
|
||||
return c.String(http.StatusOK, "test")
|
||||
}))
|
||||
})
|
||||
|
||||
// Status 2xx
|
||||
h.Handle(c)
|
||||
h(c)
|
||||
|
||||
// Status 3xx
|
||||
rec = test.NewResponseRecorder()
|
||||
c = echo.NewContext(rq, rec, e)
|
||||
h = Logger()(echo.HandlerFunc(func(c echo.Context) error {
|
||||
h = Logger()(func(c echo.Context) error {
|
||||
return c.String(http.StatusTemporaryRedirect, "test")
|
||||
}))
|
||||
h.Handle(c)
|
||||
})
|
||||
h(c)
|
||||
|
||||
// Status 4xx
|
||||
rec = test.NewResponseRecorder()
|
||||
c = echo.NewContext(rq, rec, e)
|
||||
h = Logger()(echo.HandlerFunc(func(c echo.Context) error {
|
||||
h = Logger()(func(c echo.Context) error {
|
||||
return c.String(http.StatusNotFound, "test")
|
||||
}))
|
||||
h.Handle(c)
|
||||
})
|
||||
h(c)
|
||||
|
||||
// Status 5xx with empty path
|
||||
rq = test.NewRequest(echo.GET, "", nil)
|
||||
rec = test.NewResponseRecorder()
|
||||
c = echo.NewContext(rq, rec, e)
|
||||
h = Logger()(echo.HandlerFunc(func(c echo.Context) error {
|
||||
h = Logger()(func(c echo.Context) error {
|
||||
return errors.New("error")
|
||||
}))
|
||||
h.Handle(c)
|
||||
})
|
||||
h(c)
|
||||
}
|
||||
|
||||
func TestLoggerIPAddress(t *testing.T) {
|
||||
@ -58,24 +58,24 @@ func TestLoggerIPAddress(t *testing.T) {
|
||||
buf := new(bytes.Buffer)
|
||||
e.Logger().SetOutput(buf)
|
||||
ip := "127.0.0.1"
|
||||
h := Logger()(echo.HandlerFunc(func(c echo.Context) error {
|
||||
h := Logger()(func(c echo.Context) error {
|
||||
return c.String(http.StatusOK, "test")
|
||||
}))
|
||||
})
|
||||
|
||||
// With X-Real-IP
|
||||
rq.Header().Add(echo.XRealIP, ip)
|
||||
h.Handle(c)
|
||||
h(c)
|
||||
assert.Contains(t, ip, buf.String())
|
||||
|
||||
// With X-Forwarded-For
|
||||
buf.Reset()
|
||||
rq.Header().Del(echo.XRealIP)
|
||||
rq.Header().Add(echo.XForwardedFor, ip)
|
||||
h.Handle(c)
|
||||
h(c)
|
||||
assert.Contains(t, ip, buf.String())
|
||||
|
||||
// with rq.RemoteAddr
|
||||
buf.Reset()
|
||||
h.Handle(c)
|
||||
h(c)
|
||||
assert.Contains(t, ip, buf.String())
|
||||
}
|
||||
|
Reference in New Issue
Block a user