1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-18 16:20:53 +02:00
echo/middleware/logger_test.go
Mark Bates b02e78b749 Fixed bug where IP address wasn't showing up in log files. Also added
some tests for the Logger middleware.
2015-10-21 16:39:51 -04:00

87 lines
1.8 KiB
Go

package middleware
import (
"bytes"
"errors"
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
)
func TestLogger(t *testing.T) {
// Note: Just for the test coverage, not a real test.
e := echo.New()
req, _ := http.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := echo.NewContext(req, echo.NewResponse(rec), e)
// Status 2xx
h := func(c *echo.Context) error {
return c.String(http.StatusOK, "test")
}
Logger()(h)(c)
// Status 3xx
rec = httptest.NewRecorder()
c = echo.NewContext(req, echo.NewResponse(rec), e)
h = func(c *echo.Context) error {
return c.String(http.StatusTemporaryRedirect, "test")
}
Logger()(h)(c)
// Status 4xx
rec = httptest.NewRecorder()
c = echo.NewContext(req, echo.NewResponse(rec), e)
h = func(c *echo.Context) error {
return c.String(http.StatusNotFound, "test")
}
Logger()(h)(c)
// Status 5xx with empty path
req, _ = http.NewRequest(echo.GET, "", nil)
rec = httptest.NewRecorder()
c = echo.NewContext(req, echo.NewResponse(rec), e)
h = func(c *echo.Context) error {
return errors.New("error")
}
Logger()(h)(c)
}
func TestLogger_IPAddress(t *testing.T) {
buf := &bytes.Buffer{}
log.SetOutput(buf)
ip := "127.0.0.1"
e := echo.New()
req, _ := http.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := echo.NewContext(req, echo.NewResponse(rec), e)
h := func(c *echo.Context) error {
return c.String(http.StatusOK, "test")
}
mw := Logger()
// With X-Real-IP
req.Header.Add(echo.XRealIP, ip)
mw(h)(c)
assert.Contains(t, buf.String(), ip)
// With X-Forwarded-For
buf.Reset()
req.Header.Del(echo.XRealIP)
req.Header.Add(echo.XForwardedFor, ip)
mw(h)(c)
assert.Contains(t, buf.String(), ip)
// with req.RemoteAddr
buf.Reset()
mw(h)(c)
assert.Contains(t, buf.String(), ip)
}