package logging_test import ( "context" "io" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "tildegit.org/tjp/gus" "tildegit.org/tjp/gus/logging" ) func TestLogRequests(t *testing.T) { logger := logRecorder{} handler := logging.LogRequests(&logger)(func(_ context.Context, _ *gus.Request) *gus.Response { return &gus.Response{} }) response := handler(context.Background(), &gus.Request{}) _, err := io.ReadAll(response.Body) assert.Nil(t, err) require.Equal(t, 1, len(logger)) keyvals := map[string]any{} for i := 0; i < len(logger[0])-1; i += 2 { keyvals[logger[0][i].(string)] = logger[0][i+1] } if assert.Contains(t, keyvals, "msg") { assert.Equal(t, keyvals["msg"], "request") } assert.Contains(t, keyvals, "ts") assert.Contains(t, keyvals, "dur") assert.Contains(t, keyvals, "url") assert.Contains(t, keyvals, "status") assert.Contains(t, keyvals, "bodylen") } type logRecorder [][]any func (lr *logRecorder) Log(keyvals ...any) error { *lr = append(*lr, keyvals) return nil }