2015-05-12 00:43:54 +02:00
|
|
|
package middleware
|
2015-05-20 23:38:51 +02:00
|
|
|
|
|
|
|
import (
|
2015-10-21 22:39:51 +02:00
|
|
|
"bytes"
|
2015-05-31 00:20:36 +02:00
|
|
|
"errors"
|
2015-05-20 23:38:51 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2015-09-18 01:17:54 +02:00
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
2016-01-31 09:51:12 +02:00
|
|
|
"github.com/labstack/gommon/log"
|
2015-10-21 22:39:51 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-05-20 23:38:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLogger(t *testing.T) {
|
2015-09-18 01:17:54 +02:00
|
|
|
// Note: Just for the test coverage, not a real test.
|
2015-05-20 23:38:51 +02:00
|
|
|
e := echo.New()
|
|
|
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
2015-05-30 19:54:55 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2015-12-01 21:22:45 +02:00
|
|
|
c := echo.NewContext(req, echo.NewResponse(rec, e), e)
|
2015-05-20 23:38:51 +02:00
|
|
|
|
2015-09-18 01:17:54 +02:00
|
|
|
// Status 2xx
|
2015-10-21 22:39:51 +02:00
|
|
|
h := func(c *echo.Context) error {
|
2015-09-18 01:17:54 +02:00
|
|
|
return c.String(http.StatusOK, "test")
|
|
|
|
}
|
|
|
|
Logger()(h)(c)
|
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
// Status 3xx
|
|
|
|
rec = httptest.NewRecorder()
|
2015-12-01 21:22:45 +02:00
|
|
|
c = echo.NewContext(req, echo.NewResponse(rec, e), e)
|
2015-05-30 19:54:55 +02:00
|
|
|
h = func(c *echo.Context) error {
|
|
|
|
return c.String(http.StatusTemporaryRedirect, "test")
|
|
|
|
}
|
|
|
|
Logger()(h)(c)
|
|
|
|
|
2015-05-20 23:38:51 +02:00
|
|
|
// Status 4xx
|
2015-05-30 19:54:55 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2015-12-01 21:22:45 +02:00
|
|
|
c = echo.NewContext(req, echo.NewResponse(rec, e), e)
|
2015-05-20 23:38:51 +02:00
|
|
|
h = func(c *echo.Context) error {
|
|
|
|
return c.String(http.StatusNotFound, "test")
|
|
|
|
}
|
|
|
|
Logger()(h)(c)
|
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
// Status 5xx with empty path
|
|
|
|
req, _ = http.NewRequest(echo.GET, "", nil)
|
|
|
|
rec = httptest.NewRecorder()
|
2015-12-01 21:22:45 +02:00
|
|
|
c = echo.NewContext(req, echo.NewResponse(rec, e), e)
|
2015-05-20 23:38:51 +02:00
|
|
|
h = func(c *echo.Context) error {
|
2015-05-30 19:54:55 +02:00
|
|
|
return errors.New("error")
|
2015-05-20 23:38:51 +02:00
|
|
|
}
|
|
|
|
Logger()(h)(c)
|
|
|
|
}
|
2015-10-21 22:39:51 +02:00
|
|
|
|
2015-10-22 00:11:01 +02:00
|
|
|
func TestLoggerIPAddress(t *testing.T) {
|
2015-10-21 22:39:51 +02:00
|
|
|
e := echo.New()
|
2016-01-31 09:51:12 +02:00
|
|
|
l := log.New("echo")
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
l.SetOutput(buf)
|
|
|
|
e.SetLogger(l)
|
2015-10-21 22:39:51 +02:00
|
|
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
2015-12-01 21:22:45 +02:00
|
|
|
c := echo.NewContext(req, echo.NewResponse(rec, e), e)
|
|
|
|
ip := "127.0.0.1"
|
2015-10-21 22:39:51 +02:00
|
|
|
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)
|
|
|
|
}
|