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"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net/http/httptest"
|
2016-11-02 22:08:51 +02:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2015-05-20 23:38:51 +02:00
|
|
|
"testing"
|
2015-09-18 01:17:54 +02:00
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
2015-10-21 22:39:51 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-05-20 23:38:51 +02:00
|
|
|
)
|
|
|
|
|
2016-02-19 08:50:40 +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()
|
2016-09-23 07:53:44 +02:00
|
|
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c := e.NewContext(req, rec)
|
2016-04-02 23:19:39 +02:00
|
|
|
h := Logger()(func(c echo.Context) error {
|
2015-09-18 01:17:54 +02:00
|
|
|
return c.String(http.StatusOK, "test")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2016-02-08 09:02:37 +02:00
|
|
|
|
|
|
|
// Status 2xx
|
2016-04-02 23:19:39 +02:00
|
|
|
h(c)
|
2015-09-18 01:17:54 +02:00
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
// Status 3xx
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c = e.NewContext(req, rec)
|
2016-04-02 23:19:39 +02:00
|
|
|
h = Logger()(func(c echo.Context) error {
|
2015-05-30 19:54:55 +02:00
|
|
|
return c.String(http.StatusTemporaryRedirect, "test")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
|
|
|
h(c)
|
2015-05-30 19:54:55 +02:00
|
|
|
|
2015-05-20 23:38:51 +02:00
|
|
|
// Status 4xx
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c = e.NewContext(req, rec)
|
2016-04-02 23:19:39 +02:00
|
|
|
h = Logger()(func(c echo.Context) error {
|
2015-05-20 23:38:51 +02:00
|
|
|
return c.String(http.StatusNotFound, "test")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
|
|
|
h(c)
|
2015-05-20 23:38:51 +02:00
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
// Status 5xx with empty path
|
2016-09-23 07:53:44 +02:00
|
|
|
req, _ = http.NewRequest(echo.GET, "/", nil)
|
|
|
|
rec = httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c = e.NewContext(req, rec)
|
2016-04-02 23:19:39 +02:00
|
|
|
h = Logger()(func(c echo.Context) error {
|
2015-05-30 19:54:55 +02:00
|
|
|
return errors.New("error")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
|
|
|
h(c)
|
2015-05-20 23:38:51 +02:00
|
|
|
}
|
2015-10-21 22:39:51 +02:00
|
|
|
|
2016-02-19 08:50:40 +02:00
|
|
|
func TestLoggerIPAddress(t *testing.T) {
|
2015-10-21 22:39:51 +02:00
|
|
|
e := echo.New()
|
2016-09-23 07:53:44 +02:00
|
|
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c := e.NewContext(req, rec)
|
2015-12-01 21:22:45 +02:00
|
|
|
buf := new(bytes.Buffer)
|
2016-09-23 07:53:44 +02:00
|
|
|
e.Logger.SetOutput(buf)
|
2015-12-01 21:22:45 +02:00
|
|
|
ip := "127.0.0.1"
|
2016-04-02 23:19:39 +02:00
|
|
|
h := Logger()(func(c echo.Context) error {
|
2015-10-21 22:39:51 +02:00
|
|
|
return c.String(http.StatusOK, "test")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2015-10-21 22:39:51 +02:00
|
|
|
|
|
|
|
// With X-Real-IP
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Header.Add(echo.HeaderXRealIP, ip)
|
2016-04-02 23:19:39 +02:00
|
|
|
h(c)
|
2016-03-19 07:02:10 +02:00
|
|
|
assert.Contains(t, ip, buf.String())
|
2015-10-21 22:39:51 +02:00
|
|
|
|
|
|
|
// With X-Forwarded-For
|
|
|
|
buf.Reset()
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Header.Del(echo.HeaderXRealIP)
|
|
|
|
req.Header.Add(echo.HeaderXForwardedFor, ip)
|
2016-04-02 23:19:39 +02:00
|
|
|
h(c)
|
2016-03-19 07:02:10 +02:00
|
|
|
assert.Contains(t, ip, buf.String())
|
2015-10-21 22:39:51 +02:00
|
|
|
|
|
|
|
buf.Reset()
|
2016-04-02 23:19:39 +02:00
|
|
|
h(c)
|
2016-03-19 07:02:10 +02:00
|
|
|
assert.Contains(t, ip, buf.String())
|
2015-10-21 22:39:51 +02:00
|
|
|
}
|
2016-11-02 22:08:51 +02:00
|
|
|
|
2017-01-12 06:07:51 +02:00
|
|
|
func TestLoggerFields(t *testing.T) {
|
2016-11-02 22:08:51 +02:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
e := echo.New()
|
|
|
|
e.Use(LoggerWithConfig(LoggerConfig{
|
2017-01-12 06:07:51 +02:00
|
|
|
Fields: []string{
|
|
|
|
"time",
|
|
|
|
"remote_ip",
|
|
|
|
"host",
|
|
|
|
"user_agent",
|
|
|
|
"method",
|
|
|
|
"uri",
|
|
|
|
"path",
|
|
|
|
"referer",
|
|
|
|
"status",
|
|
|
|
"latency",
|
|
|
|
"latency_human",
|
|
|
|
"bytes_in",
|
|
|
|
"bytes_out",
|
|
|
|
"header:X-Custom-Header",
|
|
|
|
"query:user",
|
|
|
|
"form:user",
|
|
|
|
},
|
|
|
|
Output: &Stream{buf},
|
2016-11-02 22:08:51 +02:00
|
|
|
}))
|
|
|
|
|
|
|
|
e.GET("/", func(c echo.Context) error {
|
2017-01-12 06:07:51 +02:00
|
|
|
return c.String(http.StatusOK, "OK")
|
2016-11-02 22:08:51 +02:00
|
|
|
})
|
|
|
|
|
2017-01-12 06:07:51 +02:00
|
|
|
req, _ := http.NewRequest(echo.GET, "/?user=joe", nil)
|
2016-11-02 22:08:51 +02:00
|
|
|
req.RequestURI = "/"
|
|
|
|
req.Header.Add(echo.HeaderXRealIP, "127.0.0.1")
|
|
|
|
req.Header.Add("Referer", "google.com")
|
2017-01-12 06:07:51 +02:00
|
|
|
req.Header.Add("User-Agent", "test-agent")
|
2016-11-02 22:08:51 +02:00
|
|
|
req.Header.Add("X-Custom-Header", "AAA-CUSTOM-VALUE")
|
|
|
|
req.Form = url.Values{
|
2017-01-12 06:07:51 +02:00
|
|
|
"user": []string{"jon"},
|
2016-11-02 22:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
cases := map[string]bool{
|
2017-01-12 06:07:51 +02:00
|
|
|
"time": true,
|
|
|
|
"joe": true,
|
|
|
|
"jon": true,
|
2016-11-02 22:08:51 +02:00
|
|
|
"AAA-CUSTOM-VALUE": true,
|
|
|
|
"GET": true,
|
|
|
|
"127.0.0.1": true,
|
|
|
|
"\"path\":\"/\"": true,
|
|
|
|
"\"uri\":\"/\"": true,
|
|
|
|
"\"status\":200": true,
|
|
|
|
"\"bytes_in\":0": true,
|
|
|
|
"google.com": true,
|
2017-01-12 06:07:51 +02:00
|
|
|
"test-agent": true,
|
2016-11-02 22:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for token, present := range cases {
|
|
|
|
assert.True(t, strings.Contains(buf.String(), token) == present, "Case: "+token)
|
|
|
|
}
|
|
|
|
}
|