internal/log: add unit tests

Signed-off-by: Bobby DeSimone <bobbydesimone@gmail.com>
This commit is contained in:
Bobby DeSimone 2019-09-28 13:38:44 -07:00
parent 1fa45c6ec2
commit aa0182008f
No known key found for this signature in database
GPG key ID: AEE4CF12FE86D07E
2 changed files with 61 additions and 0 deletions

View file

@ -10,7 +10,9 @@ import (
"reflect"
"regexp"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/rs/zerolog"
)
@ -268,3 +270,28 @@ func TestForwardedAddrHandler(t *testing.T) {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestAccessHandler(t *testing.T) {
out := &bytes.Buffer{}
r := httptest.NewRequest(http.MethodGet, "/", nil)
h := AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
l := FromRequest(r)
l.Log().Int("status", status).Int("size", size).Msg("info")
})(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("some inner logging")
w.Write([]byte("Add something to the request of non-zero size"))
}))
h = NewHandler(zerolog.New(out))(h)
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
want := "{\"message\":\"some inner logging\"}\n{\"status\":200,\"size\":45,\"message\":\"info\"}\n"
got := decodeIfBinary(out)
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("TestAccessHandler: %s", diff)
}
}