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"
|
2018-02-21 20:44:17 +02:00
|
|
|
"encoding/json"
|
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"
|
2022-07-05 06:57:39 +02:00
|
|
|
"strconv"
|
2016-11-02 22:08:51 +02:00
|
|
|
"strings"
|
2015-05-20 23:38:51 +02:00
|
|
|
"testing"
|
2018-02-21 20:44:17 +02:00
|
|
|
"time"
|
|
|
|
"unsafe"
|
2015-09-18 01:17:54 +02:00
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
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()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
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-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
|
2018-10-14 17:16:58 +02:00
|
|
|
req = httptest.NewRequest(http.MethodGet, "/", nil)
|
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 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()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
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)
|
2019-07-18 06:34:31 +02:00
|
|
|
assert.Contains(t, buf.String(), ip)
|
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)
|
2019-07-18 06:34:31 +02:00
|
|
|
assert.Contains(t, buf.String(), ip)
|
2015-10-21 22:39:51 +02:00
|
|
|
|
|
|
|
buf.Reset()
|
2016-04-02 23:19:39 +02:00
|
|
|
h(c)
|
2019-07-18 06:34:31 +02:00
|
|
|
assert.Contains(t, buf.String(), ip)
|
2015-10-21 22:39:51 +02:00
|
|
|
}
|
2016-11-02 22:08:51 +02:00
|
|
|
|
2017-01-12 19:09:51 +02:00
|
|
|
func TestLoggerTemplate(t *testing.T) {
|
2016-11-02 22:08:51 +02:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
e := echo.New()
|
|
|
|
e.Use(LoggerWithConfig(LoggerConfig{
|
2017-03-06 23:16:05 +02:00
|
|
|
Format: `{"time":"${time_rfc3339_nano}","id":"${id}","remote_ip":"${remote_ip}","host":"${host}","user_agent":"${user_agent}",` +
|
2017-01-12 19:09:51 +02:00
|
|
|
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
|
2022-12-04 20:38:45 +02:00
|
|
|
`"latency_human":"${latency_human}","bytes_in":${bytes_in}, "path":"${path}", "route":"${route}", "referer":"${referer}",` +
|
2018-07-11 08:04:36 +02:00
|
|
|
`"bytes_out":${bytes_out},"ch":"${header:X-Custom-Header}", "protocol":"${protocol}"` +
|
2017-05-08 17:10:11 +02:00
|
|
|
`"us":"${query:username}", "cf":"${form:username}", "session":"${cookie:session}"}` + "\n",
|
2017-01-12 19:09:51 +02:00
|
|
|
Output: buf,
|
2016-11-02 22:08:51 +02:00
|
|
|
}))
|
|
|
|
|
2022-12-04 20:38:45 +02:00
|
|
|
e.GET("/users/:id", func(c echo.Context) error {
|
2017-01-12 19:09:51 +02:00
|
|
|
return c.String(http.StatusOK, "Header Logged")
|
2016-11-02 22:08:51 +02:00
|
|
|
})
|
|
|
|
|
2022-12-04 20:38:45 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/users/1?username=apagano-param&password=secret", 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 19:09:51 +02:00
|
|
|
req.Header.Add("User-Agent", "echo-tests-agent")
|
2016-11-02 22:08:51 +02:00
|
|
|
req.Header.Add("X-Custom-Header", "AAA-CUSTOM-VALUE")
|
2017-02-10 09:40:54 +02:00
|
|
|
req.Header.Add("X-Request-ID", "6ba7b810-9dad-11d1-80b4-00c04fd430c8")
|
2017-05-08 17:10:11 +02:00
|
|
|
req.Header.Add("Cookie", "_ga=GA1.2.000000000.0000000000; session=ac08034cd216a647fc2eb62f2bcf7b810")
|
2016-11-02 22:08:51 +02:00
|
|
|
req.Form = url.Values{
|
2017-01-12 19:09:51 +02:00
|
|
|
"username": []string{"apagano-form"},
|
|
|
|
"password": []string{"secret-form"},
|
2016-11-02 22:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
cases := map[string]bool{
|
2017-02-10 09:40:54 +02:00
|
|
|
"apagano-param": true,
|
|
|
|
"apagano-form": true,
|
|
|
|
"AAA-CUSTOM-VALUE": true,
|
|
|
|
"BBB-CUSTOM-VALUE": false,
|
|
|
|
"secret-form": false,
|
|
|
|
"hexvalue": false,
|
|
|
|
"GET": true,
|
|
|
|
"127.0.0.1": true,
|
2022-12-04 20:38:45 +02:00
|
|
|
"\"path\":\"/users/1\"": true,
|
|
|
|
"\"route\":\"/users/:id\"": true,
|
2017-02-10 09:40:54 +02:00
|
|
|
"\"uri\":\"/\"": true,
|
|
|
|
"\"status\":200": true,
|
|
|
|
"\"bytes_in\":0": true,
|
|
|
|
"google.com": true,
|
|
|
|
"echo-tests-agent": true,
|
|
|
|
"6ba7b810-9dad-11d1-80b4-00c04fd430c8": true,
|
2017-05-08 17:10:11 +02:00
|
|
|
"ac08034cd216a647fc2eb62f2bcf7b810": 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)
|
|
|
|
}
|
|
|
|
}
|
2018-02-19 18:05:09 +02:00
|
|
|
|
|
|
|
func TestLoggerCustomTimestamp(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
customTimeFormat := "2006-01-02 15:04:05.00000"
|
|
|
|
e := echo.New()
|
|
|
|
e.Use(LoggerWithConfig(LoggerConfig{
|
|
|
|
Format: `{"time":"${time_custom}","id":"${id}","remote_ip":"${remote_ip}","host":"${host}","user_agent":"${user_agent}",` +
|
|
|
|
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
|
|
|
|
`"latency_human":"${latency_human}","bytes_in":${bytes_in}, "path":"${path}", "referer":"${referer}",` +
|
|
|
|
`"bytes_out":${bytes_out},"ch":"${header:X-Custom-Header}",` +
|
|
|
|
`"us":"${query:username}", "cf":"${form:username}", "session":"${cookie:session}"}` + "\n",
|
|
|
|
CustomTimeFormat: customTimeFormat,
|
2018-02-21 20:44:17 +02:00
|
|
|
Output: buf,
|
2018-02-19 18:05:09 +02:00
|
|
|
}))
|
|
|
|
|
|
|
|
e.GET("/", func(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "custom time stamp test")
|
|
|
|
})
|
|
|
|
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2018-02-19 18:05:09 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
var objs map[string]*json.RawMessage
|
2021-02-26 12:04:34 +02:00
|
|
|
if err := json.Unmarshal(buf.Bytes(), &objs); err != nil {
|
2018-02-19 18:05:09 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
loggedTime := *(*string)(unsafe.Pointer(objs["time"]))
|
|
|
|
_, err := time.Parse(customTimeFormat, loggedTime)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2021-09-14 19:57:47 +02:00
|
|
|
|
2022-11-30 15:47:23 +02:00
|
|
|
func TestLoggerCustomTagFunc(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
e.Use(LoggerWithConfig(LoggerConfig{
|
|
|
|
Format: `{"method":"${method}",${custom}}` + "\n",
|
|
|
|
CustomTagFunc: func(c echo.Context, buf *bytes.Buffer) (int, error) {
|
|
|
|
return buf.WriteString(`"tag":"my-value"`)
|
|
|
|
},
|
|
|
|
Output: buf,
|
|
|
|
}))
|
|
|
|
|
|
|
|
e.GET("/", func(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "custom time stamp test")
|
|
|
|
})
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
assert.Equal(t, `{"method":"GET","tag":"my-value"}`+"\n", buf.String())
|
|
|
|
}
|
|
|
|
|
2021-09-14 19:57:47 +02:00
|
|
|
func BenchmarkLoggerWithConfig_withoutMapFields(b *testing.B) {
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
mw := LoggerWithConfig(LoggerConfig{
|
|
|
|
Format: `{"time":"${time_rfc3339_nano}","id":"${id}","remote_ip":"${remote_ip}","host":"${host}","user_agent":"${user_agent}",` +
|
|
|
|
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
|
|
|
|
`"latency_human":"${latency_human}","bytes_in":${bytes_in}, "path":"${path}", "referer":"${referer}",` +
|
|
|
|
`"bytes_out":${bytes_out}, "protocol":"${protocol}"}` + "\n",
|
|
|
|
Output: buf,
|
|
|
|
})(func(c echo.Context) error {
|
|
|
|
c.Request().Header.Set(echo.HeaderXRequestID, "123")
|
|
|
|
c.FormValue("to force parse form")
|
|
|
|
return c.String(http.StatusTeapot, "OK")
|
|
|
|
})
|
|
|
|
|
|
|
|
f := make(url.Values)
|
|
|
|
f.Set("csrf", "token")
|
|
|
|
f.Add("multiple", "1")
|
|
|
|
f.Add("multiple", "2")
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/test?lang=en&checked=1&checked=2", strings.NewReader(f.Encode()))
|
|
|
|
req.Header.Set("Referer", "https://echo.labstack.com/")
|
|
|
|
req.Header.Set("User-Agent", "curl/7.68.0")
|
|
|
|
req.Header.Add(echo.HeaderContentType, echo.MIMEApplicationForm)
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
mw(c)
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLoggerWithConfig_withMapFields(b *testing.B) {
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
mw := LoggerWithConfig(LoggerConfig{
|
|
|
|
Format: `{"time":"${time_rfc3339_nano}","id":"${id}","remote_ip":"${remote_ip}","host":"${host}","user_agent":"${user_agent}",` +
|
|
|
|
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
|
|
|
|
`"latency_human":"${latency_human}","bytes_in":${bytes_in}, "path":"${path}", "referer":"${referer}",` +
|
|
|
|
`"bytes_out":${bytes_out},"ch":"${header:X-Custom-Header}", "protocol":"${protocol}"` +
|
|
|
|
`"us":"${query:username}", "cf":"${form:csrf}", "Referer2":"${header:Referer}"}` + "\n",
|
|
|
|
Output: buf,
|
|
|
|
})(func(c echo.Context) error {
|
|
|
|
c.Request().Header.Set(echo.HeaderXRequestID, "123")
|
|
|
|
c.FormValue("to force parse form")
|
|
|
|
return c.String(http.StatusTeapot, "OK")
|
|
|
|
})
|
|
|
|
|
|
|
|
f := make(url.Values)
|
|
|
|
f.Set("csrf", "token")
|
|
|
|
f.Add("multiple", "1")
|
|
|
|
f.Add("multiple", "2")
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/test?lang=en&checked=1&checked=2", strings.NewReader(f.Encode()))
|
|
|
|
req.Header.Set("Referer", "https://echo.labstack.com/")
|
|
|
|
req.Header.Set("User-Agent", "curl/7.68.0")
|
|
|
|
req.Header.Add(echo.HeaderContentType, echo.MIMEApplicationForm)
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
mw(c)
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
}
|
2022-07-05 06:57:39 +02:00
|
|
|
|
|
|
|
func TestLoggerTemplateWithTimeUnixMilli(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
e := echo.New()
|
|
|
|
e.Use(LoggerWithConfig(LoggerConfig{
|
|
|
|
Format: `${time_unix_milli}`,
|
|
|
|
Output: buf,
|
|
|
|
}))
|
|
|
|
|
|
|
|
e.GET("/", func(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "OK")
|
|
|
|
})
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
unixMillis, err := strconv.ParseInt(buf.String(), 10, 64)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.WithinDuration(t, time.Unix(unixMillis/1000, 0), time.Now(), 3*time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoggerTemplateWithTimeUnixMicro(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
e := echo.New()
|
|
|
|
e.Use(LoggerWithConfig(LoggerConfig{
|
|
|
|
Format: `${time_unix_micro}`,
|
|
|
|
Output: buf,
|
|
|
|
}))
|
|
|
|
|
|
|
|
e.GET("/", func(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "OK")
|
|
|
|
})
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
unixMicros, err := strconv.ParseInt(buf.String(), 10, 64)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.WithinDuration(t, time.Unix(unixMicros/1000000, 0), time.Now(), 3*time.Second)
|
|
|
|
}
|