mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-29 18:36:30 +02:00
Merge pull request #327 from desimone/tests/logs-improve-coverage
internal/log: add unit tests
This commit is contained in:
commit
5df0ff500c
2 changed files with 61 additions and 0 deletions
34
internal/log/example_test.go
Normal file
34
internal/log/example_test.go
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
// Adapted from https://golang.org/src/log/example_test.go
|
||||||
|
// Copyright 2013 The Go Authors. See 3RD-PARTY file for license.
|
||||||
|
|
||||||
|
package log_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
stdlog "log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/pomerium/pomerium/internal/log"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleLogger() {
|
||||||
|
log.Logger = zerolog.New(os.Stdout).With().Str("level-logging?", "yep!").Logger()
|
||||||
|
|
||||||
|
var (
|
||||||
|
buf bytes.Buffer
|
||||||
|
logger = stdlog.New(&log.StdLogWrapper{Logger: &log.Logger}, "", 0)
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.Print("Hello logger!")
|
||||||
|
log.SetDebugMode()
|
||||||
|
|
||||||
|
logger.Print("Debug")
|
||||||
|
|
||||||
|
fmt.Print(&buf)
|
||||||
|
// Output:
|
||||||
|
// {"level":"error","level-logging?":"yep!","message":"Hello logger!"}
|
||||||
|
//[90m<nil>[0m [1m[31mERR[0m[0m Debug [36mlevel-logging?=[0myep!
|
||||||
|
|
||||||
|
}
|
|
@ -10,7 +10,9 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -268,3 +270,28 @@ func TestForwardedAddrHandler(t *testing.T) {
|
||||||
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue