1
0
mirror of https://github.com/labstack/echo.git synced 2025-07-03 00:56:59 +02:00

Logging middleware fixes

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana
2016-03-21 17:27:14 -07:00
parent 703174e58f
commit a66162a3d2
16 changed files with 153 additions and 155 deletions

View File

@ -14,9 +14,9 @@ import (
func TestLogger(t *testing.T) {
// Note: Just for the test coverage, not a real test.
e := echo.New()
req := test.NewRequest(echo.GET, "/", nil)
rq := test.NewRequest(echo.GET, "/", nil)
rec := test.NewResponseRecorder()
c := echo.NewContext(req, rec, e)
c := echo.NewContext(rq, rec, e)
h := Logger()(echo.HandlerFunc(func(c echo.Context) error {
return c.String(http.StatusOK, "test")
}))
@ -26,7 +26,7 @@ func TestLogger(t *testing.T) {
// Status 3xx
rec = test.NewResponseRecorder()
c = echo.NewContext(req, rec, e)
c = echo.NewContext(rq, rec, e)
h = Logger()(echo.HandlerFunc(func(c echo.Context) error {
return c.String(http.StatusTemporaryRedirect, "test")
}))
@ -34,16 +34,16 @@ func TestLogger(t *testing.T) {
// Status 4xx
rec = test.NewResponseRecorder()
c = echo.NewContext(req, rec, e)
c = echo.NewContext(rq, rec, e)
h = Logger()(echo.HandlerFunc(func(c echo.Context) error {
return c.String(http.StatusNotFound, "test")
}))
h.Handle(c)
// Status 5xx with empty path
req = test.NewRequest(echo.GET, "", nil)
rq = test.NewRequest(echo.GET, "", nil)
rec = test.NewResponseRecorder()
c = echo.NewContext(req, rec, e)
c = echo.NewContext(rq, rec, e)
h = Logger()(echo.HandlerFunc(func(c echo.Context) error {
return errors.New("error")
}))
@ -52,9 +52,9 @@ func TestLogger(t *testing.T) {
func TestLoggerIPAddress(t *testing.T) {
e := echo.New()
req := test.NewRequest(echo.GET, "/", nil)
rq := test.NewRequest(echo.GET, "/", nil)
rec := test.NewResponseRecorder()
c := echo.NewContext(req, rec, e)
c := echo.NewContext(rq, rec, e)
buf := new(bytes.Buffer)
e.Logger().SetOutput(buf)
ip := "127.0.0.1"
@ -63,18 +63,18 @@ func TestLoggerIPAddress(t *testing.T) {
}))
// With X-Real-IP
req.Header().Add(echo.XRealIP, ip)
rq.Header().Add(echo.XRealIP, ip)
h.Handle(c)
assert.Contains(t, ip, buf.String())
// With X-Forwarded-For
buf.Reset()
req.Header().Del(echo.XRealIP)
req.Header().Add(echo.XForwardedFor, ip)
rq.Header().Del(echo.XRealIP)
rq.Header().Add(echo.XForwardedFor, ip)
h.Handle(c)
assert.Contains(t, ip, buf.String())
// with req.RemoteAddr
// with rq.RemoteAddr
buf.Reset()
h.Handle(c)
assert.Contains(t, ip, buf.String())