2015-05-11 15:43:54 -07:00
|
|
|
package middleware
|
2015-05-20 14:38:51 -07:00
|
|
|
|
|
|
|
import (
|
2015-10-21 16:39:51 -04:00
|
|
|
"bytes"
|
2015-05-30 15:20:36 -07:00
|
|
|
"errors"
|
2015-05-20 14:38:51 -07:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
2015-09-17 19:17:54 -04:00
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
2016-01-28 23:46:11 -08:00
|
|
|
"github.com/labstack/echo/test"
|
2015-10-21 16:39:51 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-05-20 14:38:51 -07:00
|
|
|
)
|
|
|
|
|
2016-02-18 22:50:40 -08:00
|
|
|
func TestLogger(t *testing.T) {
|
2015-09-17 19:17:54 -04:00
|
|
|
// Note: Just for the test coverage, not a real test.
|
2015-05-20 14:38:51 -07:00
|
|
|
e := echo.New()
|
2016-04-24 10:21:23 -07:00
|
|
|
req := test.NewRequest(echo.GET, "/", nil)
|
|
|
|
rec := test.NewResponseRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
2016-04-02 14:19:39 -07:00
|
|
|
h := Logger()(func(c echo.Context) error {
|
2015-09-17 19:17:54 -04:00
|
|
|
return c.String(http.StatusOK, "test")
|
2016-04-02 14:19:39 -07:00
|
|
|
})
|
2016-02-07 23:02:37 -08:00
|
|
|
|
|
|
|
// Status 2xx
|
2016-04-02 14:19:39 -07:00
|
|
|
h(c)
|
2015-09-17 19:17:54 -04:00
|
|
|
|
2015-05-30 10:54:55 -07:00
|
|
|
// Status 3xx
|
2016-04-24 10:21:23 -07:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = e.NewContext(req, rec)
|
2016-04-02 14:19:39 -07:00
|
|
|
h = Logger()(func(c echo.Context) error {
|
2015-05-30 10:54:55 -07:00
|
|
|
return c.String(http.StatusTemporaryRedirect, "test")
|
2016-04-02 14:19:39 -07:00
|
|
|
})
|
|
|
|
h(c)
|
2015-05-30 10:54:55 -07:00
|
|
|
|
2015-05-20 14:38:51 -07:00
|
|
|
// Status 4xx
|
2016-04-24 10:21:23 -07:00
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = e.NewContext(req, rec)
|
2016-04-02 14:19:39 -07:00
|
|
|
h = Logger()(func(c echo.Context) error {
|
2015-05-20 14:38:51 -07:00
|
|
|
return c.String(http.StatusNotFound, "test")
|
2016-04-02 14:19:39 -07:00
|
|
|
})
|
|
|
|
h(c)
|
2015-05-20 14:38:51 -07:00
|
|
|
|
2015-05-30 10:54:55 -07:00
|
|
|
// Status 5xx with empty path
|
2016-04-24 10:21:23 -07:00
|
|
|
req = test.NewRequest(echo.GET, "", nil)
|
|
|
|
rec = test.NewResponseRecorder()
|
|
|
|
c = e.NewContext(req, rec)
|
2016-04-02 14:19:39 -07:00
|
|
|
h = Logger()(func(c echo.Context) error {
|
2015-05-30 10:54:55 -07:00
|
|
|
return errors.New("error")
|
2016-04-02 14:19:39 -07:00
|
|
|
})
|
|
|
|
h(c)
|
2015-05-20 14:38:51 -07:00
|
|
|
}
|
2015-10-21 16:39:51 -04:00
|
|
|
|
2016-02-18 22:50:40 -08:00
|
|
|
func TestLoggerIPAddress(t *testing.T) {
|
2015-10-21 16:39:51 -04:00
|
|
|
e := echo.New()
|
2016-04-24 10:21:23 -07:00
|
|
|
req := test.NewRequest(echo.GET, "/", nil)
|
|
|
|
rec := test.NewResponseRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
2015-12-01 11:22:45 -08:00
|
|
|
buf := new(bytes.Buffer)
|
2016-03-06 11:43:40 -08:00
|
|
|
e.Logger().SetOutput(buf)
|
2015-12-01 11:22:45 -08:00
|
|
|
ip := "127.0.0.1"
|
2016-04-02 14:19:39 -07:00
|
|
|
h := Logger()(func(c echo.Context) error {
|
2015-10-21 16:39:51 -04:00
|
|
|
return c.String(http.StatusOK, "test")
|
2016-04-02 14:19:39 -07:00
|
|
|
})
|
2015-10-21 16:39:51 -04:00
|
|
|
|
|
|
|
// With X-Real-IP
|
2016-04-24 10:21:23 -07:00
|
|
|
req.Header().Add(echo.HeaderXRealIP, ip)
|
2016-04-02 14:19:39 -07:00
|
|
|
h(c)
|
2016-03-18 22:02:10 -07:00
|
|
|
assert.Contains(t, ip, buf.String())
|
2015-10-21 16:39:51 -04:00
|
|
|
|
|
|
|
// With X-Forwarded-For
|
|
|
|
buf.Reset()
|
2016-04-24 10:21:23 -07:00
|
|
|
req.Header().Del(echo.HeaderXRealIP)
|
|
|
|
req.Header().Add(echo.HeaderXForwardedFor, ip)
|
2016-04-02 14:19:39 -07:00
|
|
|
h(c)
|
2016-03-18 22:02:10 -07:00
|
|
|
assert.Contains(t, ip, buf.String())
|
2015-10-21 16:39:51 -04:00
|
|
|
|
|
|
|
buf.Reset()
|
2016-04-02 14:19:39 -07:00
|
|
|
h(c)
|
2016-03-18 22:02:10 -07:00
|
|
|
assert.Contains(t, ip, buf.String())
|
2015-10-21 16:39:51 -04:00
|
|
|
}
|