middleware/logger/logger_test.go
2020-11-07 14:59:33 +03:00

135 lines
3.2 KiB
Go

package logger
import (
"bytes"
"io/ioutil"
"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)
b, err := ioutil.ReadAll(res.Body)
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) {
body, err := ioutil.ReadAll(r.Body)
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")
body, err := ioutil.ReadAll(r.Body)
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)
}