diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-01-26 16:22:58 -0700 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-01-26 16:22:58 -0700 |
commit | a27b879accb191b6a6c6e76a6251ed751967f73a (patch) | |
tree | d7c5f337ca9cb4143997665fc616759fbf5e2d3f /logging/middleware_test.go | |
parent | 32e45e3557e49cc868aa4437ef0aa56ab6470be8 (diff) |
test coverage and resulting bugfixes
Diffstat (limited to 'logging/middleware_test.go')
-rw-r--r-- | logging/middleware_test.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/logging/middleware_test.go b/logging/middleware_test.go new file mode 100644 index 0000000..288c960 --- /dev/null +++ b/logging/middleware_test.go @@ -0,0 +1,47 @@ +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 +} |