2020-11-07 11:59:33 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-12-25 00:47:12 +00:00
|
|
|
"io"
|
2020-11-07 11:59:33 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLogger(t *testing.T) {
|
|
|
|
logHandler := func(entry LogEntry) {
|
|
|
|
assert.Equal(t, entry.Method, "GET")
|
|
|
|
assert.Equal(t, entry.RawURL, "/")
|
|
|
|
assert.Equal(t, entry.Body, "")
|
|
|
|
assert.Equal(t, entry.RemoteIP, "127.0.0.1")
|
|
|
|
assert.Equal(t, entry.StatusCode, http.StatusOK)
|
|
|
|
assert.Equal(t, entry.Written, len([]byte("resp")))
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := Middleware(
|
|
|
|
WithoutBody(),
|
|
|
|
SetLogHandler(logHandler),
|
|
|
|
)
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, err := w.Write([]byte("resp"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
server := httptest.NewServer(logger(handler))
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
res, err := http.Get(server.URL)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer res.Body.Close()
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
|
2023-12-25 00:47:12 +00:00
|
|
|
b, err := io.ReadAll(res.Body)
|
2020-11-07 11:59:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "resp", string(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoggerBody(t *testing.T) {
|
|
|
|
logHandler := func(entry LogEntry) {
|
|
|
|
assert.Equal(t, entry.Method, "POST")
|
|
|
|
assert.Equal(t, entry.Body, "1234567890...")
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := Middleware(
|
|
|
|
SetMaxBodySize(10),
|
|
|
|
SetLogHandler(logHandler),
|
|
|
|
)
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2023-12-25 00:47:12 +00:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2020-11-07 11:59:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "12345678901234567890", string(body))
|
|
|
|
|
|
|
|
_, err = w.Write([]byte("resp"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
server := httptest.NewServer(logger(handler))
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
res, err := http.Post(server.URL, "", bytes.NewBufferString("12345678901234567890"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer res.Body.Close()
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoggerUser(t *testing.T) {
|
|
|
|
logHandler := func(entry LogEntry) {
|
|
|
|
assert.Equal(t, entry.User, "user")
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := Middleware(
|
|
|
|
SetUserFn(func(req *http.Request) string { return "user" }),
|
|
|
|
SetLogHandler(logHandler),
|
|
|
|
)
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, err := w.Write([]byte("resp"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
server := httptest.NewServer(logger(handler))
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
res, err := http.Get(server.URL)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer res.Body.Close()
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoggerSanitize(t *testing.T) {
|
|
|
|
logHandler := func(entry LogEntry) {
|
|
|
|
assert.Equal(t, entry.RawURL, "/?param=*****")
|
|
|
|
assert.Equal(t, entry.Body, "body|*****")
|
|
|
|
}
|
|
|
|
|
|
|
|
sanitizeFn := func(input string) string {
|
|
|
|
return strings.ReplaceAll(input, "password", "*****")
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := Middleware(
|
|
|
|
SetSanitizeFn(sanitizeFn),
|
|
|
|
SetLogHandler(logHandler),
|
|
|
|
)
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Equal(t, r.URL.Query().Get("param"), "password")
|
|
|
|
|
2023-12-25 00:47:12 +00:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2020-11-07 11:59:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "body|password", string(body))
|
|
|
|
|
|
|
|
_, err = w.Write([]byte("resp"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
server := httptest.NewServer(logger(handler))
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
res, err := http.Post(server.URL+"?param=password", "", bytes.NewBufferString("body|password"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer res.Body.Close()
|
|
|
|
assert.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
}
|