2021-12-21 18:02:25 -08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2024-06-14 18:01:48 -07:00
|
|
|
"context"
|
|
|
|
"log/slog"
|
2021-12-21 18:02:25 -08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
2024-06-14 18:01:48 -07:00
|
|
|
echomw "github.com/labstack/echo/v4/middleware"
|
2024-07-09 17:57:05 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-06-14 18:01:48 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-12-21 18:02:25 -08:00
|
|
|
|
2024-07-09 17:57:05 -07:00
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/log"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/tests"
|
2021-12-21 18:02:25 -08:00
|
|
|
)
|
|
|
|
|
2024-06-14 18:01:48 -07:00
|
|
|
type mockLogHandler struct {
|
|
|
|
msg string
|
|
|
|
level string
|
|
|
|
group string
|
|
|
|
attr []slog.Attr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockLogHandler) Enabled(_ context.Context, l slog.Level) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockLogHandler) Handle(_ context.Context, r slog.Record) error {
|
|
|
|
m.level = r.Level.String()
|
|
|
|
m.msg = r.Message
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockLogHandler) WithAttrs(as []slog.Attr) slog.Handler {
|
|
|
|
if m.attr == nil {
|
|
|
|
m.attr = make([]slog.Attr, 0)
|
|
|
|
}
|
|
|
|
m.attr = append(m.attr, as...)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockLogHandler) WithGroup(name string) slog.Handler {
|
|
|
|
m.group = name
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockLogHandler) GetAttr(key string) string {
|
|
|
|
if m.attr == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
for _, attr := range m.attr {
|
|
|
|
if attr.Key == key {
|
|
|
|
return attr.Value.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-12-21 18:02:25 -08:00
|
|
|
func TestLogRequestID(t *testing.T) {
|
|
|
|
ctx, _ := tests.NewContext(c.Web, "/")
|
|
|
|
|
2024-06-14 18:01:48 -07:00
|
|
|
h := new(mockLogHandler)
|
|
|
|
logger := slog.New(h)
|
|
|
|
log.Set(ctx, logger)
|
|
|
|
|
|
|
|
require.NoError(t, tests.ExecuteMiddleware(ctx, echomw.RequestID()))
|
|
|
|
require.NoError(t, tests.ExecuteMiddleware(ctx, SetLogger()))
|
|
|
|
|
|
|
|
log.Ctx(ctx).Info("test")
|
2021-12-21 18:02:25 -08:00
|
|
|
rID := ctx.Response().Header().Get(echo.HeaderXRequestID)
|
2024-06-14 18:01:48 -07:00
|
|
|
assert.Equal(t, rID, h.GetAttr("request_id"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogRequest(t *testing.T) {
|
|
|
|
statusCode := 200
|
|
|
|
h := new(mockLogHandler)
|
|
|
|
|
|
|
|
exec := func() {
|
2024-06-19 06:32:22 -07:00
|
|
|
ctx, _ := tests.NewContext(c.Web, "http://test.localhost/abc?d=1&e=2")
|
2024-06-14 18:01:48 -07:00
|
|
|
logger := slog.New(h).With("previous", "param")
|
|
|
|
log.Set(ctx, logger)
|
|
|
|
ctx.Request().Header.Set("Referer", "ref.com")
|
|
|
|
ctx.Request().Header.Set(echo.HeaderXRealIP, "21.12.12.21")
|
|
|
|
|
|
|
|
require.NoError(t, tests.ExecuteHandler(ctx, func(ctx echo.Context) error {
|
|
|
|
return ctx.String(statusCode, "hello")
|
|
|
|
},
|
|
|
|
SetLogger(),
|
|
|
|
LogRequest(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
exec()
|
|
|
|
assert.Equal(t, "param", h.GetAttr("previous"))
|
|
|
|
assert.Equal(t, "21.12.12.21", h.GetAttr("ip"))
|
|
|
|
assert.Equal(t, "test.localhost", h.GetAttr("host"))
|
|
|
|
assert.Equal(t, "ref.com", h.GetAttr("referer"))
|
|
|
|
assert.Equal(t, "200", h.GetAttr("status"))
|
|
|
|
assert.Equal(t, "0", h.GetAttr("bytes_in"))
|
|
|
|
assert.Equal(t, "5", h.GetAttr("bytes_out"))
|
|
|
|
assert.NotEmpty(t, h.GetAttr("latency"))
|
|
|
|
assert.Equal(t, "INFO", h.level)
|
2024-06-19 06:32:22 -07:00
|
|
|
assert.Equal(t, "GET /abc?d=1&e=2", h.msg)
|
2024-06-14 18:01:48 -07:00
|
|
|
|
|
|
|
statusCode = 500
|
|
|
|
exec()
|
|
|
|
assert.Equal(t, "ERROR", h.level)
|
2021-12-21 18:02:25 -08:00
|
|
|
}
|