package logging_test import ( "context" "io" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" sr "tildegit.org/tjp/sliderule" "tildegit.org/tjp/sliderule/logging" ) func TestLogRequests(t *testing.T) { logger := logRecorder{} handler := logging.LogRequests(&logger)(sr.HandlerFunc(func(_ context.Context, _ *sr.Request) *sr.Response { return &sr.Response{} })) response := handler.Handle(context.Background(), &sr.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 }